微信小程序wx.upload上传图片后台java
来源:互联网 发布:知乎精彩回答问题 编辑:程序博客网 时间:2024/05/17 06:24
前台代码:
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)
阅读全文
0 0
- 微信小程序wx.upload上传图片后台java
- 微信小程序文件上传java服务器端wx:uploadFile()
- 微信小程序wx.request后台数据交互
- 微信小程序wx.request后台数据交互
- 微信小程序填坑:上传头像;wx.chooseImage,wx.uploadFile
- 微信小程序 wx.uploadFile无法上传解决办法
- PHP--解决微信小程序上传图片(wx.uploadFile)接口代码
- 微信小程序wx.previewImage预览图片
- 微信小程序wx.getImageInfo()获取图片信息
- 微信小程序上传图片到服务器(java后台以及使用springmvc)
- java后台图片上传接口
- java upload 上传
- upload.js 文件图片上传
- Android Webview upload 图片上传
- 织梦本地后台卡以及图片上传失败upload filetype not allow!提示
- Apache Cordova 安卓android上传图片或者文件 调用后台服务实现java代码 ft.upload(imageURI, uri, that.uploadSuccess, that.
- 微信小程序-wx.uploadFile
- 微信小程序 wx:for
- 在windows下安装windows+Ubuntu16.04双系统(上)
- HDU 5918 KMP
- android项目从零开始 内存优化 SparseArray ArrayMap
- “失联”三天才恢复,A站“关停风波”原因疑惑重重
- linux下利用nohup后台运行jar文件包程序
- 微信小程序wx.upload上传图片后台java
- vue弹出框的封装
- VUE2.0全套demo讲解 基础2(列表渲染)
- 从项目角色的发展谈项目管理
- VHDL SXT函数
- 链表、队列、栈的相关应用(二)链式A+B
- SpringMVC 之 处理数据模型
- Pig Latin
- linux 卸载 mysql