微信小程序上传图片 后台Java+spring接收
2018-03-09 12:05
2166 查看
最近在做微信小程序图片上传,将代码上传,造福后来人,废话不多说,贴代码:
微信小程序端 chooseImage(){
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'http://127.0.0.1:8888/pesss/weChat/uploadImage.do',
filePath: tempFilePaths[0],
name: 'file',
formData: {
'user': 'test'
},
success: function (res) {
var data = res.data
//do something
},fail:function(err){
console.log(err)
}
})
}
})
}java 端:import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
@Controller
public class ImageTestWebchatController {
@RequestMapping(value = "/weChat/uploadImage", method = { RequestMethod.POST,RequestMethod.GET})
public ModelAndView uploadImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("进入get方法!");
MultipartHttpServletRequest req =(MultipartHttpServletRequest)request;
MultipartFile multipartFile = req.getFile("file");
String realPath = "F:/image";
try {
File dir = new File(realPath);
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(realPath,"aaa.jpg");
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
return null;
}
}Java端的返回参数就自己拼写吧,文件路径、名称可以自行命名 ,因为是测试,这里就不写全了。
如有问题,请加入微信小程序开发群:563752274,答案:Tony
微信小程序端 chooseImage(){
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'http://127.0.0.1:8888/pesss/weChat/uploadImage.do',
filePath: tempFilePaths[0],
name: 'file',
formData: {
'user': 'test'
},
success: function (res) {
var data = res.data
//do something
},fail:function(err){
console.log(err)
}
})
}
})
}java 端:import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
@Controller
public class ImageTestWebchatController {
@RequestMapping(value = "/weChat/uploadImage", method = { RequestMethod.POST,RequestMethod.GET})
public ModelAndView uploadImage(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("进入get方法!");
MultipartHttpServletRequest req =(MultipartHttpServletRequest)request;
MultipartFile multipartFile = req.getFile("file");
String realPath = "F:/image";
try {
File dir = new File(realPath);
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(realPath,"aaa.jpg");
multipartFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
return null;
}
}Java端的返回参数就自己拼写吧,文件路径、名称可以自行命名 ,因为是测试,这里就不写全了。
如有问题,请加入微信小程序开发群:563752274,答案:Tony
相关文章推荐
- 微信小程序wx.upload上传图片后台java
- 微信小程序上传图片+java 后端接收实例
- 微信小程序开发之图片上传+Java服务端接收
- 微信小程序上传图片到服务器(java后台以及使用springmvc)
- [转]微信小程序开发(二)图片上传+服务端接收
- 微信小程序java开发图片上传 ssm框架
- 搭建图片服务器《四》:后台java代码springMVC+spring实现图片上传
- 微信小程序开发之图片上传+服务端接收教程
- 微信小程序 上传图片(PHP后台)9张,上传到指定文件夹
- Java 后台实现上传图片的接收并保存
- base64编码上传图片java后台接收实例
- Java后台服务器接收IOS客户端上传的图片
- base64编码上传图片java后台接收实例
- 微信小程序开发(二)图片上传+服务端接收详解
- 微信小程序-从相册获取图片,视频 使用相机拍照,录像上传+服务器(nodejs版)接收
- 通过ajaxFileUpload控件上传图片,java后台接收并保存本地
- 微信小程序开发(二)图片上传+服务端接收
- 小程序的图片上传wx.uploadFile及后台PHP接收文件并存储到服务器
- 手机端上传图片及java后台接收
- java后台接收app上传的图片的示例代码