我想,
1)使用 Spring Rest API 实施一些 REST服务方法,以便从我的远程Web客户端上传一些文件。
2)使用基于 RestTemplate 的远程Web客户端进行测试。
如果有人有任何想法请帮助我。感谢。
我的一些Spring REST API基本方法如下,
@RequestMapping(value="user/create/{userRoleName}", method=RequestMethod.POST)
public @ResponseBody User create(@RequestBody User user, @PathVariable String userRoleName, HttpServletResponse response) { }
我的一些远程客户端的Spring RestTemplate基本代码如下,
Map<String, String> vars = new HashMap<String, String>();
vars.put("userRoleName", userRoleName);
ResponseEntity<User> REcreateUser = restTemplate.postForEntity(IMC_LAB_SKELETON_URL + "/user/create/{userRoleName}", newUser, User.class, vars);
User createUser = REcreateUser.getBody();
答案 0 :(得分:0)
试试这个:
public class FileUploadService {
@POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(Attachment attachment,@Context HttpServletRequest request) {
DataHandler handler = attachment.getDataHandler();
try {
InputStream stream = handler.getInputStream();
MultivaluedMap map = attachment.getHeaders();
OutputStream out = new FileOutputStream(new File(getFileName(map)));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = stream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
stream.close();
out.flush();
out.close();
} catch(Exception e) {
e.printStackTrace();
}
}
return Response.ok("file uploaded").build();
}
可以使用RestTemplate
轻松测试。
<强>进口:强>
答案 1 :(得分:0)
CHARACTER SET