1. 程式人生 > >微信小程式上傳圖片檔案 小程式+Java

微信小程式上傳圖片檔案 小程式+Java

小程式程式碼:

  chooseImage(){
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'http://127.0.0.1:8080/xxxx', // 後臺上傳檔案url 
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test' //需要傳入的其它引數給後臺
          },
          success: function (res) {
            var data = res.data
            //do something
          },fail:function(err){
            console.log(err)
          }
        })
      }
    })
  }

Java程式碼:

package com.core.controller;

import com.core.entity.Massage;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @ClassName WxUploadController
 * @Description 微信檔案上傳
 * @Author PengKe
 * @Wx VIP694046220
 * @Date 2018/9/19 0019 20:45
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "wxUpload")
public class WxUploadController {

    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);

    @RequestMapping("/upload")
        public ModelAndView uploadImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
            System.out.println("檔案上傳!");

            MultipartHttpServletRequest req =(MultipartHttpServletRequest)request;
            PrintWriter out = response.getWriter();
        Massage massage = new Massage();
            MultipartFile multipartFile =  req.getFile("file");
            String uId = request.getParameter("uId");//這就是獲取微信傳過來的其它引數
            String imgType = request.getParameter("imgType");
            System.out.println(uId+"---"+imgType);

            String realPath = request.getRealPath("/upload") + "/";
            try {
                File dir = new File(realPath);
                if (!dir.exists()) {
                    dir.mkdir();
                }
                File file  =  new File(realPath,uId+"_"+imgType+".jpg");// 檔案儲存位置和檔名
                multipartFile.transferTo(file);
                massage.setCode(1);
                massage.setDatas("");
                massage.setMsg("上傳成功");
            } catch (IOException e) {
                massage.setCode(0);
                massage.setDatas(e);
                massage.setMsg("上傳失敗");
                e.printStackTrace();
            } catch (IllegalStateException e) {
                massage.setCode(0);
                massage.setDatas(e);
                massage.setMsg("上傳失敗!");
                e.printStackTrace();
            }
        JSONObject jsonObject = JSONObject.fromObject(massage);
        out.print(jsonObject.toString());
        out.close();
            return null;
        }

}

可以了~