Rob*_*ely 29 java spring spring-mvc multipart spring-boot
我使用Spring Boot并希望使用Controller来接收多部分文件上传.发送文件时,我不断收到错误415不支持的内容类型响应,并且永远不会到达控制器
There was an unexpected error (type=Unsupported Media Type, status=415).
Content type 'multipart/form-data;boundary=----WebKitFormBoundary1KvzQ1rt2V1BBbb8' not supported
Run Code Online (Sandbox Code Playgroud)
我尝试使用form:action在html/jsp页面中发送,也在使用RestTemplate的独立客户端应用程序中发送.所有尝试都给出相同的结果
multipart/form-data;boundary=XXXXX not supported.
从多部分文档看来,必须将边界参数添加到分段上传,但这似乎与控制器接收不匹配 "multipart/form-data"
我的控制器方法设置如下
@RequestMapping(value = "/things", method = RequestMethod.POST, consumes = "multipart/form-data" ,
produces = { "application/json", "application/xml" })
public ResponseEntity<ThingRepresentation> submitThing(HttpServletRequest request,
@PathVariable("domain") String domainParam,
@RequestParam(value = "type") String thingTypeParam,
@RequestBody MultipartFile[] submissions) throws Exception
Run Code Online (Sandbox Code Playgroud)
使用Bean安装程序
@Bean
public MultipartConfigElement multipartConfigElement() {
return new MultipartConfigElement("");
}
@Bean
public MultipartResolver multipartResolver() {
org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(1000000);
return multipartResolver;
}
Run Code Online (Sandbox Code Playgroud)
正如你可以看到我已经设定消耗型为"multipart/form-data的",但是当发送多就必须有一个边界参数,并将随机边界线.
任何人都可以告诉我如何在控制器中设置内容类型以匹配或更改我的请求以匹配我的控制器设置?
我尝试发送...尝试1 ...
<html lang="en">
<body>
<br>
<h2>Upload New File to this Bucket</h2>
<form action="http://localhost:8280/appname/domains/abc/things?type=abcdef00-1111-4b38-8026-315b13dc8706" method="post" enctype="multipart/form-data">
<table width="60%" border="1" cellspacing="0">
<tr>
<td width="35%"><strong>File to upload</strong></td>
<td width="65%"><input type="file" name="file" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Add" /></td>
</tr>
</table>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
尝试2 ....
RestTemplate template = new RestTemplate();
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("file", new FileSystemResource(pathToFile));
try{
URI response = template.postForLocation(url, parts);
}catch(HttpClientErrorException e){
System.out.println(e.getResponseBodyAsString());
}
Run Code Online (Sandbox Code Playgroud)
尝试3 ......
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
formHttpMessageConverter.setCharset(Charset.forName("UTF8"));
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add( formHttpMessageConverter );
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("file", new FileSystemResource(path));
HttpHeaders imageHeaders = new HttpHeaders();
imageHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> imageEntity = new HttpEntity<MultiValueMap<String, Object>>(map, imageHeaders);
ResponseEntity e= restTemplate.exchange(uri, HttpMethod.POST, imageEntity, Boolean.class);
System.out.println(e.toString());
Run Code Online (Sandbox Code Playgroud)
a b*_*ver 23
@RequestBody MultipartFile[] submissions
Run Code Online (Sandbox Code Playgroud)
应该
@RequestParam("file") MultipartFile[] submissions
Run Code Online (Sandbox Code Playgroud)
这些文件不是请求体,它们是它的一部分,并且没有内置HttpMessageConverter可以将请求转换为数组MultiPartFile.
您也可以替换HttpServletRequest为MultipartHttpServletRequest,这样您就可以访问各个部分的标题.
And*_*rea 23
你可以简单地使用这样的控制器方法:
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<?> uploadFile(
@RequestParam("file") MultipartFile file) {
try {
// Handle the received file here
// ...
}
catch (Exception e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(HttpStatus.OK);
} // method uploadFile
Run Code Online (Sandbox Code Playgroud)
没有Spring Boot的任何其他配置.
使用以下html表单客户端:
<html>
<body>
<form action="/uploadFile" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果要设置文件大小限制,可以在以下位置执行application.properties:
# File size limit
multipart.maxFileSize = 3Mb
# Total request size for a multipart/form-data
multipart.maxRequestSize = 20Mb
Run Code Online (Sandbox Code Playgroud)
此外,使用Ajax发送文件请看这里:http: //blog.netgloo.com/2015/02/08/spring-boot-file-upload-with-ajax/
小智 7
最新版本的SpringBoot也可以轻松上传多个文件.在浏览器方面,您只需要标准的HTML上传表单,但是需要多个输入元素(每个文件要上传一个,这非常重要),所有元素都具有相同的元素名称(以下示例为name ="files")
然后在服务器上的Spring @Controller类中,您需要的是这样的:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<?> upload(
@RequestParam("files") MultipartFile[] uploadFiles) throws Exception
{
...now loop over all uploadFiles in the array and do what you want
return new ResponseEntity<>(HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
这些是棘手的部分.也就是说,知道要创建多个输入元素,每个输入元素都命名为"files",并且知道使用MultipartFile [](数组)作为请求参数是很难理解的事情,但就是这么简单.我不会介绍如何处理MultipartFile条目,因为已经有很多文档.
| 归档时间: |
|
| 查看次数: |
95467 次 |
| 最近记录: |