上传后, 后台解析request内容为空,是什么问题?
wx.uploadFile({
url: 'http://192.168.1.102:8888/base_service/file/upload',
filePath: that.data.tempFilePaths[0],
method: 'POST',
name: 'fileName', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
header: { "content-type": "multipart/form-data", 'accept': 'application/json', }, //HTTP 请求 Header , header 中不能设置 Referer
formData: {
'user': 'test',
},
success: function (res) {
console.log(statusCode, res);
wx.showModal({
title: '上传文件返回状态',
content: statusCode+'成功' + res.msg,
success: function (res) {
if (res.confirm) {
console.log('用户点击确定')
}
}
})
// App.HttpService.savFiles(params)
// .then(data => {
// console.log(data)
// if (data.ret == 0) {
// this.showToast(data.msg)
// }
// })
}, fail: function (err) {
console.log(statusCode, err);
}
});
-----------------------------------------后台代码--------------
@RequestMapping(value ="file/upload", method = RequestMethod.POST)
@ResponseBody
public BaseResult fileUpload(HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
BaseResult result=BaseResult.createPrepareResult();
Map<String,Object> map=result.getMap();
List<Map<String,Object>> listMap= new ArrayList<Map<String, Object>>(); ;
try{
request.setCharacterEncoding("UTF-8");
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
//解决上传文件名的中文乱码
upload.setHeaderEncoding("UTF-8");
//3、判断提交上来的数据是否是上传表单的数据
if(!ServletFileUpload.isMultipartContent(request)){
//按照传统方式获取数据
System.out.println("传统方式上传文件 ");
//return;
}
List<FileItem> items = upload.parseRequest(request); // 得到所有的文件 这里为空???
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(new String(fi.getName().getBytes(), "utf-8")); // 解决文件名乱码问题
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
既然都用Spring了,还用 HttpServlet**这种乱代码,我要是你领导,一定开掉你。