2016-09-26 3 views
2

파일을 직접 보내지 않고 FormBodyPart에서 컨트롤러를 사용하여 파일을 보내려고합니다. 다음은 파일 모음을 만들기위한 코드입니다.Spring org.springframework.web.multipart.support.MissingServletRequestPartException, Required 요청 부분 'file'이 없습니다.

private void addFile(Collection<FormBodyPart> parts, File inputFile, String fileType) 
     throws ClassificationException { 
    if (inputFile == null) { 
     throw new ClassificationException("Null input file provided"); 
    } 
    if (!inputFile.exists()) { 
     throw new ClassificationException("Input file not found: " + inputFile.getAbsolutePath()); 
    } 
    if (fileType != null) { 

     String charset = "UTF-8"; 
     parts.add(new FormBodyPart("file", new FileBody(inputFile, fileType, charset))); 

    } else { 
     parts.add(new FormBodyPart("file", new FileBody(inputFile, inputFile.getName()))); 
    } 
} 

파트 콜렉션은 파일을 포함하는 arraylist입니다. 여기

에서 HTTP 엔터티를 설정하기위한 내 코드입니다

HttpPost httppost = new HttpPost("http://localhost:9000/upload1"); 
      MultipartEntity reqEntity1 = new MultipartEntity(); 
      FormBodyPart part1; 
      for (Iterator i$ = parts.iterator(); i$.hasNext(); reqEntity1.addPart(part1)) { 
       part1 = (FormBodyPart) i$.next(); 
       System.out.println(part1.getHeader()); 
      } 

      httppost.setEntity(reqEntity1); 
      HttpResponse response = httpclient.execute(httppost); 
      System.out.println(response); 

컨트롤러의 내 메소드 선언은

String index(@RequestParam("file") MultipartFile uploadfile)

내가 진술 서버에서 오류를 얻고있다

[ 400] { "타임 스탬프": 1474898550131, "상태": 400, "오류": "잘못된 요청", "예외": "o rg.springframework.web.multipart.support.MissingServletRequestPartException ","메시지 ":"필수 요청 부분 '파일'경로 ","존재하지 않는 ":"/ upload1 "}

내 dispatcher.xml 이미 포함 multipartResolver의 bean.

저는 웹 서비스에 익숙하지 않고 어리석은 실수를 저지르고있을 수도 있습니다. 제발 도와주세요. 미리 감사드립니다.

+0

변수 이름에'$'을 사용하지 마십시오. [Java 언어 사양 §3.8] (https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.8) : *'$'기호는 기계적으로 생성 된 소스 코드이거나 드물게 레거시 시스템의 기존 이름에 액세스 할 수 없습니다. * – Andreas

+0

'part'는 비어 있지 않습니까? – Andreas

+0

"[[email protected]]"부분을 인쇄 할 때 비어 있다고 생각하지 않습니다. –

답변

1
verify if you have this items: 
@Bean 
public CommonsMultipartResolver multipartResolver() { 
CommonsMultipartResolver multipart = new CommonsMultipartResolver(); 
multipart.setMaxUploadSize(3 * 1024 * 1024); 
return multipart;} 

@Bean 
@Order(0) 
public MultipartFilter multipartFilter() { 
MultipartFilter multipartFilter = new MultipartFilter(); 
multipartFilter.setMultipartResolverBeanName("multipartResolver"); 
return multipartFilter; 
} 


and in the pplications.properties 


# MULTIPART (MultipartProperties) 
spring.http.multipart.enabled=true 
# Enable support of multi-part uploads. 
# spring.http.multipart.file-size-threshold=3 # Threshold after which files will be written to disk. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size. 
spring.http.multipart.location=/
# Intermediate location of uploaded files. 
spring.http.multipart.max-file-size=10MB 
# Max file size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size. 
spring.http.multipart.max-request-size=10MB 
# Max request size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size. 
spring.http.multipart.resolve-lazily=false 
# Whether to resolve the multipart request lazily at the time of file or parameter access. 
+0

Thanks @pablo! application.properties는 검색하는 날로부터 정말로 나를 돕습니다 ... – Hongliang