使用Spring处理文件上载请求

时间:2015-01-06 14:01:58

标签: spring spring-mvc file-upload multipartform-data

我正在尝试从像这样的请求中提取XML文件:

标题

Content-Type: multipart/form-data; boundary=MIME_boundary_314159265358979

--MIME_boundary_314159265358979
Content-disposition: attachment; filename="result"
Content-Type: text/xml
Content-Transfer-Encoding: 8bit

<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
</data>

--MIME_boundary_314159265358979

我在Spring文档的17.10.5会话中发现了类似的东西 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

但是根据这个要求,没有&#34;名称&#34;对于内容处置。 有关如何在Web服务上处理它的任何建议吗?

@RequestMapping(value = "/request", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE
          , consumes = "multipart/form-data")
      @ResponseBody
      @ResponseStatus(value = HttpStatus.OK)
      String handleRequest(@RequestPart MultipartFile file)

1 个答案:

答案 0 :(得分:0)

您可以尝试:

@RequestMapping(value = "/request", method = RequestMethod.POST)   
public ModelAndView dataloaderUploadFile(@RequestParam(required = true) final MultipartFile file)
{
String xml = new String(file.getBytes());
.....
}