2014-09-06 84 views
16

林,並希望使用一個控制器接收多文件上傳。 當發送我不斷收到錯誤415不支持的內容類型響應和控制器從未達到分段文件使用Spring啓動上傳春季啓動

文件
There was an unexpected error (type=Unsupported Media Type, status=415). 
Content type 'multipart/form-data;boundary=----WebKitFormBoundary1KvzQ1rt2V1BBbb8' not supported 

香港專業教育學院嘗試使用形式發送:行動HTML/JSP頁面,並在一個獨立的客戶端應用程序,使用RestTemplate。所有試圖給出相同的結果

multipart/form-data;boundary=XXXXX not supported.

它從邊界PARAM已被添加到多載然而,這似乎給控制器接收"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 

豆安裝

@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的」,但是當發送多就必須有一個邊界參數,並將隨機邊界線。

誰能告訴我怎麼可以設置內容類型的控制器來匹配或改變了我的請求,以配合我控制器設置?

我試圖發送... 嘗試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>&nbsp;</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()); 
+0

你是怎麼寫域名的? – TheTechGuy 2017-11-23 10:01:25

回答

12
@RequestBody MultipartFile[] submissions 

應該是

@RequestParam("file") MultipartFile[] submissions 

文件沒有請求主體,他們是其中的一部分,也沒有內置HttpMessageConverter,可以轉換請求的MultiPartFile數組。

您也可以用MultipartHttpServletRequest替換HttpServletRequest,它可以讓您訪問各個部分的標題。

+0

工作表示感謝。我的所有客戶端嘗試現在都在服務器端對此修復進行工作。原來,我不需要MultipartConfigElement和MultipartResolver bean。可能啓動正在爲我創建這些。 – 2014-09-07 08:54:57

17

您可以簡單地用一個控制器方法是這樣的:

@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 

沒有春季啓動任何額外的配置。

使用以下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> 

如果你想設置的文件大小限制你可以在application.properties做到這一點:

# File size limit 
multipart.maxFileSize = 3Mb 

# Total request size for a multipart/form-data 
multipart.maxRequestSize = 20Mb 

而且送與的文件Ajax看看這裏: http://blog.netgloo.com/2015/02/08/spring-boot-file-upload-with-ajax/

3
@Bean 
    MultipartConfigElement multipartConfigElement() { 
     MultipartConfigFactory factory = new MultipartConfigFactory(); 
     factory.setMaxFileSize("5120MB"); 
     factory.setMaxRequestSize("5120MB"); 
     return factory.createMultipartConfig(); 
    } 

把它放在課堂,要定義豆

5

最新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); 
} 

這些都是棘手的部分。也就是說,知道創建多個名爲「files」的輸入元素,並且知道使用MultipartFile [](array)作爲請求參數是需要了解的棘手問題,但這很簡單。我不會介紹如何處理MultipartFile條目,因爲已經有很多文檔了。

1
@RequestMapping(value="/add/image", method=RequestMethod.POST) 
public ResponseEntity upload(@RequestParam("id") Long id, HttpServletResponse response, HttpServletRequest request) 
{ 
    try { 
     MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest)request; 
     Iterator<String> it=multipartRequest.getFileNames(); 
     MultipartFile multipart=multipartRequest.getFile(it.next()); 
     String fileName=id+".png"; 
     String imageName = fileName; 

     byte[] bytes=multipart.getBytes(); 
     BufferedOutputStream stream= new BufferedOutputStream(new FileOutputStream("src/main/resources/static/image/book/"+fileName));; 

     stream.write(bytes); 
     stream.close(); 
     return new ResponseEntity("upload success", HttpStatus.OK); 

    } catch (Exception e) { 
     e.printStackTrace(); 
     return new ResponseEntity("Upload fialed", HttpStatus.BAD_REQUEST); 
    } 
} 
1

在控制器中,你的方法應該是;

@RequestMapping(value = "/upload", method = RequestMethod.POST) 
    public ResponseEntity<SaveResponse> uploadAttachment(@RequestParam("file") MultipartFile file, HttpServletRequest request) { 
.... 

此外,您需要更新application.yml(或application.properties)以支持最大文件大小和請求大小。

spring: 
    http: 
     multipart: 
      max-file-size: 5MB 
      max-request-size: 20MB