2018-12-26 09:15:06 卢浮宫 版权声明:本文为站长原创文章,转载请写明出处
今天做微信小程序录音数据保存,原本是在小程序端转成base64编码保存到数据库的,但是结果发现两个弊端
①base64数据量过大资源消耗严重
②反向解析到微信端不好处理
所以就采用比较稳妥的方式:保存成文件资源,小程序端进行在线资源访问
3.1、导入jar包两个commons-fileupload-1.3.3.jar和commons-io-2.6.jar
3.2、在springmvc配置文件中添加bean如下
<!-- 上传支持 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
3.3、小程序端进行url请求
//文件上传
wx.uploadFile({
url: "http://192.168.1.101:8089/Wxsynchro/voiceTest.do", // 仅为示例,非真实的接口地址
filePath: res.tempFilePath,
name: "file",
formData: {
user: "test"
},
success(res) {
const data = res.data
// do something
}
})
3.4、服务端代码如下
@RequestMapping(value = "voiceTest",method = { RequestMethod.POST,RequestMethod.GET})
public @ResponseBody String voiceTest(HttpServletRequest request, HttpServletResponse response) throws Exception{
System.out.println("上传");
MultipartHttpServletRequest req =(MultipartHttpServletRequest)request;
MultipartFile multipartFile = req.getFile("file");
System.out.println(multipartFile);
String realPath = "F:/media";
try {
File dir = new File(realPath);
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(realPath,"test.mp3");
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
return "";
}