我正在尝试为文件上传公开REST服务,并编写REST客户端来调用服务。我正在使用apache fileupload来使用该文件,因为我当前的应用程序是旧的并且属于2.5的servlet api时代。当我调用ServletFileUpload的parseRequest方法时,它不会从请求中返回任何文件并抛出Error对象。此应用程序还支持Spring 4。
休息客户
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("file", new ByteArrayResource(file.getBytes()));
parts.add("path", "C:/Temp/link.txt");
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setContentDispositionFormData("filename","link.txt");
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(parts, headers);
ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:9080/contextroot/restservice.htm",requestEntity, String.class);
Servlet Rest服务
@RequestMapping(value = "restservice.htm")
public ResponseMessage restservice(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException, FileUploadException {
if(ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> fileList = upload.parseRequest(request);
System.out.println("Headers : ");
Enumeration<String> headerEnumeration = request.getHeaderNames();
while(headerEnumeration.hasMoreElements()) {
String headerName = headerEnumeration.nextElement();
System.out.println("Name : " + headerName + ", Value : " + request.getHeader(headerName));
}
System.out.println("Parameters : ");
Enumeration<String> paramEnumeration = request.getParameterNames();
while(paramEnumeration.hasMoreElements()) {
String paramName = paramEnumeration.nextElement();
System.out.println("Name : " + paramName + ", Value : " + request.getParameter(paramName));
}
if(null == fileList || fileList.isEmpty()) {
//Setting ERROR
}
else {
//Setting SUCCESS
}
}
else {
//Setting ERROR
}
return message;
}
标题的输出是
Name : Accept, Value : text/plain, application/json, application/*+json, */*
Name : Content-Type, Value : multipart/form- data;boundary=CqVx3AfV3oq7XjEPmPkWXqve9lNs3Ntjhz1kBaI
Name : Content-Disposition, Value : form-data; name="filename"; filename="link.txt"
Request参数包含路径和文件引用。
答案 0 :(得分:0)
我使用XMLRootElement创建了对象,并在对象中发送了文件引用。消费的REST服务很快就选择了文件引用。
REST客户端
File srcFile = new File("C:/Temp/hellotest.jpg");
FileInputStream inputStream = new FileInputStream(srcFile);
byte[] bytes = IOUtils.toByteArray(inputStream);
inputStream.close();
FileInfo requestEntity = new FileInfo();
requestEntity.setPath("C:/Temp/hellotest12.jpg");
requestEntity.setContent(bytes);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:9080/contextroot/restservice.htm",requestEntity, String.class);
REST服务
@RequestMapping(value = "restservice.htm")
public String restservice(@RequestBody FileInfo fileInfo, HttpServletResponse response, HttpServletRequest request) {
LOG.info("File created @ " + fileInfo.getPath());
File file = new File(fileInfo.getPath());
OutputStream outputStream = new FileOutputStream(file);
IOUtils.write(fileInfo.getContent(), outputStream);
outputStream.flush();
outputStream.close();
return message;
}