用Spring REST上传java图像
我想用RestTemplate客户端上传图片,用Spring base REST服务器获取POST请求并保存到服务器上。任何人都可以帮助我如何使用我的Spring base客户端和服务器。谢谢
我的一些Spring REST API基本服务器方法如下:
@RequestMapping(value="user/upload/{imageFile}", method=RequestMethod.POST)
public @ResponseBody User upload(@RequestBody User user, @PathVariable File imageFile, HttpServletResponse response) {
// TODO - How I get this image and file and save, whether I can POST this image file with User object
}
我的远程客户端Spring RestTemplate的一些基本代码如下:
User newUser = new User();
Map<String, String> vars = new HashMap<String, String>();
vars.put("imageFile", imageFile);
ResponseEntity<User> REcreateUser = restTemplate.postForEntity(IMC_LAB_SKELETON_URL + "/user/upload/{imageFile}", newUser, User.class, vars);
User createUser = REcreateUser.getBody();
// TODO - How I can POST this image file as a parameter or content of the User object
# 1 楼答案
这是我很久以前写的一段代码(可以将文件名作为@PathVariable传递):
服务器端:
使用rest模板进行测试:
这个bean是必需的(我认为它需要一些ApacheCommons库,但我不确定,现在也不记得了)