2017-05-12 38 views
5

我想爲我的彈簧啓動應用程序添加上傳功能; 這是我上傳的休息控制器上傳文件springboot所需的部分「文件」不存在

package org.sid.web; 

import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.List; 

import javax.servlet.ServletContext; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.http.HttpEntity; 
import org.springframework.http.HttpHeaders; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.MediaType; 
import org.springframework.http.ResponseEntity; 
import org.springframework.stereotype.Controller; 
import org.springframework.util.LinkedMultiValueMap; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PostMapping; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.bind.annotation.RestController; 
import org.springframework.web.client.RestTemplate; 
import org.springframework.web.multipart.MultipartFile; 
import org.springframework.web.servlet.mvc.support.RedirectAttributes; 
import org.springframework.core.env.Environment; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.FileSystemResource; 
import org.sid.entities.FileInfo; 

@RestController 
public class UploadController { 
    @Autowired 
    ServletContext context; 

    @RequestMapping(value = "/fileupload/file", headers = ("content-type=multipart/*"), method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 
    public ResponseEntity<FileInfo> upload(@RequestParam("file") MultipartFile inputFile) { 
    FileInfo fileInfo = new FileInfo(); 
    HttpHeaders headers = new HttpHeaders(); 
    if (!inputFile.isEmpty()) { 
     try { 
     String originalFilename = inputFile.getOriginalFilename(); 
     File destinationFile = new File(
      context.getRealPath("C:/Users/kamel/workspace/credit_app/uploaded") + File.separator + originalFilename); 
     inputFile.transferTo(destinationFile); 
     fileInfo.setFileName(destinationFile.getPath()); 
     fileInfo.setFileSize(inputFile.getSize()); 
     headers.add("File Uploaded Successfully - ", originalFilename); 
     return new ResponseEntity<FileInfo>(fileInfo, headers, HttpStatus.OK); 
     } catch (Exception e) { 
     return new ResponseEntity<FileInfo>(HttpStatus.BAD_REQUEST); 
     } 
    } else { 
     return new ResponseEntity<FileInfo>(HttpStatus.BAD_REQUEST); 
    } 
    } 
} 

但郵遞員與插入http://localhost:8082/fileupload/file和將文件添加到身體 測試這個當我得到這個錯誤:「異常」:「org.springframework.web.multipart.support .MissingServletRequestPartException」, ‘消息’:‘需要請求部分‘文件’不存在’,

+0

可能,這將有助於http://stackoverflow.com/questions/43864826/integration-test-case-and-file-upload/43866552#43866552 – pvpkiran

+0

Unfortunely這樣做沒有解決我的問題。錯誤仍然出現 – Wintern

回答

5

這是你的郵差要求應該怎麼樣子:

enter image description here

我的示例代碼:

application.properties

#max file and request size 
spring.http.multipart.max-file-size=10MB 
spring.http.multipart.max-request-size=11MB 

主應用程序類:

Application.java

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

休息控制器類:

import org.springframework.http.MediaType; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.springframework.web.multipart.MultipartFile; 


    @Controller 
    @RequestMapping("/fileupload") 
    public class MyRestController { 

    @RequestMapping(value = "/file", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) 
     public @ResponseBody String myService(@RequestParam("file") MultipartFile file, 
       @RequestParam("id") String id) throws Exception { 

    if (!file.isEmpty()) { 

      //your logic 
         } 
return "some json"; 

       } 
    } 

的pom.xml

//... 

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.2.RELEASE</version> 
     <relativePath /> <!-- lookup parent from repository --> 
    </parent> 

.... 



<dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web-services</artifactId> 
</dependency> 

//... 
+0

我正在嘗試相同的測試,但我通過文件作爲關鍵並選擇一個文件,但錯誤仍然存​​在。那爲什麼我很困惑 – Wintern

+0

我已經嘗試在我的機器上覆制上述異常。我可以重現的唯一方法是在請求時給出錯誤的鍵名。你能分享更多的信息嗎?像pom.xml,代碼等。 –

+4

我已經將此添加到我的應用文件中:@Bean public CommonsMultipartResolver multipartResolver(){CommonsMultipartResolver multipart = new CommonsMultipartResolver(); multipart.setMaxUploadSize(3 * 1024 * 1024);返回multipart;} @Bean @Order(0)public MultipartFilter multipartFilter(){MultipartFilter multipartFilter = new MultipartFilter(); multipartFilter.setMultipartResolverBeanName( 「multipartReso率爾」);返回multipartFilter; }' – Wintern

1

在你的方法,你有這樣的
@RequestParam("file")規定。因此它期望的關鍵是file。在例外信息中非常明顯。上傳文件時,在郵遞員的Key字段中使用此名稱。
更多的信息在這裏integration test case and file upload

+0

感謝您的幫助,但不幸的是,這就是我正在做的。我通過文件作爲密鑰並上傳文件,它不起作用。 – Wintern

0

除其他公佈答案,這個問題可能會realated缺少爲servlet處理請求(Spring的DispatcherServlet在Spring的應用程序的情況下),多支持。

這可以通過(在基於註釋的配置的情況下)

一個)基於網絡的XML配置

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
      version="3.0"> 

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class> 
    org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring/dispatcher-config.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    <multipart-config> 
     <max-file-size>10485760</max-file-size> 
     <max-request-size>20971520</max-request-size> 
     <file-size-threshold>5242880</file-size-threshold> 
    </multipart-config> 
</servlet> 

</web-app> 

在web.xml聲明或初始化過程中加入的多部分支持調度的servlet被固定b)對於基於註解的配置,這將是繼:

public class AppInitializer implements WebApplicationInitializer { 

@Override 
public void onStartup(ServletContext servletContext) { 
    final AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); 

    final ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet(appContext)); dispatcher.setLoadOnStartup(1); 

    registration.addMapping("/"); 

    File uploadDirectory = new File(System.getProperty("java.io.tmpdir"));     
    MultipartConfigElement multipartConfigElement = new MultipartConfigElement(uploadDirectory.getAbsolutePath(), 100000, 100000 * 2, 100000/2); 

    registration.setMultipartConfig(multipartConfigElement); 
} } 

然後,我們需要提供multipart解析器可以解析的文件發送,multipar叔請求。

@Configuration 
public class MyConfig { 

@Bean 
public MultipartResolver multipartResolver() { 
    return new StandardServletMultipartResolver(); 
} 
} 

基於XML的配置方式,你需要通過標記聲明聲明這個bean添加到上下文:

<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver" /> 

或者春天的標準多爲註釋配置這可以在下面的方式進行解析器,你可以使用公共實現。然而額外的依賴,需要這樣:

<dependency> 
    <groupId>commons-fileupload</groupId> 
    <artifactId>commons-fileupload</artifactId> 
    <version>1.3.3</version> 
</dependency> 

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
<property name="maxUploadSize" value="100000000"/> 
</bean> 
相關問題