为什么uploadFile好像传不了文件到后台?
发布于 6 年前 作者 ping99 627 次浏览 来自 官方Issues

前端调用代码

selectImg: function(e){
   var that=this
   wx.chooseImage({
     count: 3,
     sizeType: ['original', 'compressed'],
     sourceType: ['album', 'camera'],
     success(res) {
       const tempFilePaths = res.tempFilePaths
       console.log(tempFilePaths)
       that.setData({
         imgUrl: tempFilePaths
       })
       wx.uploadFile({
         url: app.serverUrl+"/upload/XZImg?openid="+app.openID, //仅为示例,非真实的接口地址
         filePath: tempFilePaths[0],
         name: 'files',
         success(res) {
           const data = res.data
           //do something
           console.log(data)
         },
       })
      
     }
   })

后端代码

[@RestController](/user/RestController)
[@RequestMapping](/user/RequestMapping)("/upload")
public class uploadImage {
 
    [@PostMapping](/user/PostMapping)("/XZImg")
    public void uploadFace(String openid, [@RequestParam](/user/RequestParam)("files") MultipartFile[] files ) throws Exception {
        System.out.println(openid);
 
        if (StringUtils.isNotBlank(openid)) {
            // 文件保存的命名空间
            String fileSpace = "C:/Users/Administrator/ssm_images";
            // 保存到数据库中的相对路径
            String uploadPathDB = "/" + openid;
            System.out.println(uploadPathDB);
            FileOutputStream fileOutputStream = null;
            InputStream inputStream = null;
            try {
                if (files != null &&files.length>0) {
                    System.out.println(files);
                    String fileName = files[0].getOriginalFilename();
                    if (StringUtils.isNotBlank(fileName)) {
                        // 文件上传的最终保存路径
                        String finalFacePath = fileSpace + uploadPathDB + "/" + fileName;
                        // 设置数据库保存的路径
                        uploadPathDB += ("/" + fileName);
 
                        File outFile = new File(finalFacePath);
                        if (outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()) {
                            // 创建父文件夹
                            outFile.getParentFile().mkdirs();
                        }
 
                        fileOutputStream = new FileOutputStream(outFile);
                        inputStream = files[0].getInputStream();
                        IOUtils.copy(inputStream, fileOutputStream);
                    }
 
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fileOutputStream != null) {
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }
            }
        }
 
    }
}

一直进不去啊,用HttpServltRequest看请求体参数files是null。是哪里出问题了呢?好像文件根本就没传过去,都是null

回到顶部