- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在使用 Spring Boot,并希望使用 Controller 来接收分段文件上传。发送文件时,我不断收到 error 415 unsupported content type 响应,并且永远无法到达 Controller
There was an unexpected error (type=Unsupported Media Type, status=415).
Content type 'multipart/form-data;boundary=----WebKitFormBoundary1KvzQ1rt2V1BBbb8' not supported
我尝试在 html/jsp 页面以及使用 RestTemplate 的独立客户端应用程序中使用 form:action 发送。所有尝试都给出相同的结果
multipart/form-data;boundary=XXXXX 不支持。
从多部分文档看来,必须将边界参数添加到多部分上传中,但这似乎与接收 "multipart/form-data"
我的 Controller 方法设置如下
@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
使用 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;
}
如您所见,我已将消费类型设置为“multipart/form-data”,但是当发送多部分时,它必须有一个边界参数并放置一个随机边界字符串。
谁能告诉我如何设置 Controller 中的内容类型以匹配或更改我的请求以匹配我的 Controller 设置?
我尝试发送...尝试 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>
尝试 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());
}
尝试 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());
最佳答案
@RequestBody MultipartFile[] submissions
应该是
@RequestParam("file") MultipartFile[] submissions
文件不是请求主体,它们是请求主体的一部分,并且没有内置的 HttpMessageConverter
可以将请求转换为 MultiPartFile
数组。
您还可以将 HttpServletRequest
替换为 MultipartHttpServletRequest
,这样您就可以访问各个部分的 header 。
关于java - 多部分文件上传 Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25699727/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!