手机端上传文件路径是wxfile:// ,PC端小程序文件上传路径 http:// ,服务器我用的是spring boot 框架,只能接受PC端上传的文件,无法接受手机端上传的文件,哪位大神帮帮小弟,spring boot 这个框架能接收 手机端微信小程序传过来的文件吗,有代码帮发下呗,万分感谢,给以重谢
服务器端
package com.imooc.demo.web;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.imooc.demo.entity.Fil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.imooc.demo.util.SysUtils;
import com.imooc.demo.service.FilService;
@RestController
@RequestMapping("/re")
public class FileController {
private static final Logger log = LoggerFactory.getLogger(FileController.class);
@RequestMapping(value = "/upload" , method = RequestMethod.POST)
public String upload(@RequestParam("file") MultipartFile file ) {
try {
if (file.isEmpty()) {
//相当于 return
return "文件不为空";
}
// 获取文件名
String fileName1 = file.getOriginalFilename();
String fileName = fileName1.substring(fileName1.lastIndexOf("\\")+1);
log.info("上传的文件名为:" + fileName);
// 获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
// 设置文件ID,不让文件名重复
String id = SysUtils.getUUID();
String ide = id + suffixName;
log.info("文件的后缀名为:" + suffixName);
// 设置文件存储路径
String filePath = "C://test//";
String path = filePath + ide;
File dest = new File(path);
// 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();// 新建文件夹
}
file.transferTo(dest);// 文件写入
//相当于 return
return "上传文件成功";
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//相当于 return
return "上传失败";
}
}
小程序端
upload.js
chooseImage: function (e) {
var that = this;
wx.chooseImage({
success(res) {
wx.showToast({
title: '上传中...',
icon: 'loading',
mask: true,
duration: 500
})
const tempFilePaths = res.tempFilePaths
that.setData({
tempFilePaths: tempFilePaths
})
}
})
},
formSubmit: function (e) {
var that = this;
var formData = e.detail.value;
var tempFilePaths = this.data.tempFilePaths;
if (tempFilePaths == undefined){
wx.showToast({
title: '请点击上传文件',
icon: 'loading',
mask: true,
duration: 800
})
return true;
}
wx.uploadFile({
filePath: tempFilePaths[0],
name: 'file',
formData: {
user: 'test'
},
success(res) {
const data = res.data
}
})
},