微信小程序wx.upload上传图片后台java
2017-11-30 09:47
645 查看
前台代码:
bindPhoto(e) {
var that = this;
wx.chooseImage({
count: 1,
sizeType: ['original','compressed'],// 指定原图或者压缩图
sourceType: ['album',
'camera'], // 指定图片来源
success: function (res) {
var tempFilePaths = res.tempFilePaths;
wx.uploadFile({
url: 'http://192.168.31.111:8007/goods/wx_upload.do',
filePath: tempFilePaths[0],
name: 'file',
header: { "Content-Type":
"multipart/form-data" },
formData:{
'session_token': token
},
success:function(res){
var cur_data = res.data;
console.log(cur_data.fileName);
},
fail: function (res) {
console.log('上传失败');
}
})
}
})
},
后台代码
bindPhoto(e) {
var that = this;
wx.chooseImage({
count: 1,
sizeType: ['original','compressed'],// 指定原图或者压缩图
sourceType: ['album',
'camera'], // 指定图片来源
success: function (res) {
var tempFilePaths = res.tempFilePaths;
wx.uploadFile({
url: 'http://192.168.31.111:8007/goods/wx_upload.do',
filePath: tempFilePaths[0],
name: 'file',
header: { "Content-Type":
"multipart/form-data" },
formData:{
'session_token': token
},
success:function(res){
var cur_data = res.data;
console.log(cur_data.fileName);
},
fail: function (res) {
console.log('上传失败');
}
})
}
})
},
后台代码
/*微信小程序上传图片测试*/ @RequestMapping("wx_upload.do") public void wx_upload(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setCharacterEncoding("utf-8"); //设置编码 //获得磁盘文件条目工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); String fileId = null; String json = "{\"success\":false,\"fileName\":\"" + fileId + "\"}"; String pathUrl = FSDefaultMgr.E_DEFAULT.getDefaultUploadPathUrl();//获取图片服务器路径 InputStream inStream = null; try { //可以上传多个文件 List<FileItem> list = (List<FileItem>)upload.parseRequest(request); for(FileItem item : list){ //获取表单的属性名字 String name = item.getFieldName(); //如果获取的 表单信息是普通的 文本 信息 if(item.isFormField()){ //获取用户具体输入的字符串 ,名字起得挺好,因为表单提交过来的是 字符串类型的 String value = item.getString() ; request.setAttribute(name, value); }else { //获取路径名 String filename = item.getName(); request.setAttribute(name, filename); inStream = item.getInputStream() ; } } ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = inStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } byte[] bytes = swapStream.toByteArray(); fileId = FastDFSClient.uploadFile(bytes, "20161545454.png", null); if (fileId != null) { json = "{\"success\":true,\"pathUrl\":\"" + pathUrl + "\",\"fileName\":\"" + fileId + "\"}"; } response.getWriter().write(json); response.getWriter().flush(); response.getWriter().close(); }catch (Exception e) { e.printStackTrace(); } }
注意事项:(http://blog.csdn.net/lwphk/article/details/43015829)
相关文章推荐
- 小程序的图片上传wx.uploadFile及后台PHP接收文件并存储到服务器
- 微信小程序文件上传java服务器端wx:uploadFile()
- PHP--解决微信小程序上传图片(wx.uploadFile)接口代码
- 微信小程序上传图片 后台Java+spring接收
- 微信小程序上传图片到服务器(java后台以及使用springmvc)
- 微信小程序上传图片wx.chooseImage和wx.uploadFile
- 微信小程序 上传图片(PHP后台)9张,上传到指定文件夹
- java图片上传并预览,前台用jQuery插件AjaxFileUpload,后台用FileUtils.copyFile.
- 微信小程序 wx.uploadFile无法上传解决办法
- 微信小程序开发之图片上传+Java服务端接收
- 微信小程序上传图片+java 后端接收实例
- 微信小程序商城后台修改商城配置信息提示“上传目录Data/UploadFiles不存在请手动创建”
- Apache Cordova 安卓android上传图片或者文件 调用后台服务实现java代码 ft.upload(imageURI, uri, that.uploadSuccess, that.
- 小程序之上传图片——wx.uploadFile()
- 微信小程序填坑:上传头像;wx.chooseImage,wx.uploadFile
- 通过ajaxFileUpload控件上传图片,java后台接收并保存本地
- 后台下载微信js sdk上传的图片
- php处理微信小程序wx.uploadFile(OBJECT)发送过来的文件
- 微信小程序开发之从相册获取图片 使用相机拍照 本地图片上传
- 微信小程序教程之本地图片上传(leancloud)实例详解