2015-02-03 3 views
1

나는 휴식의 API는 컨트롤러가 스프링 MVC 빌드가제한 JSON 데이터 요청

@RequestMapping(value = "/test", method = RequestMethod.POST) 
public void postTest(@RequestBody FileUpload fileUpload) { 
    System.out.println(fileUpload.getFilename()); 
    System.out.println(fileUpload.getFiletype()); 
    System.out.println(fileUpload.getFilesize()); 
} 

내 질문에 내 서버가 충돌 질수 있도록 업로드되는 파일의 크기 제한을 넣어 어떻게입니다 1GB의 업로드로 나는 당신의 qustion 보정을 followig하고있는 경우

+0

자바 스크립트에서 파일 크기를 읽고 그곳에서 업로드를 중단하십시오. – Guanxi

+0

그래,하지만 봄이나 바람둥이에서이 구멍을 서버 쪽에서 닫고 싶다. – Vinchenzo

+0

이 질문에 대한 답변입니다. 'Tuning file upload limits' 섹션을보십시오. https://spring.io/guides/gs/uploading-files/ –

답변

0

, 당신은 모델 객체에 업로드되는 파일을 넣어 크기를 확인할 수 있습니다 : 당신은 거기에 멀티 파트 파일로 MyModel 개체를 만들어야합니다

@RequestMapping(value="/upload", method=RequestMethod.POST) 
public String processUploadWithModelAttribute(@ModelAttribute("myModelAttribute") 
final MyModelAttribute myModelAttribute, final BindingResult result, 
final Model model) throws IOException {   

    // CHECK FOR file.getSize() 
} 

합니다.

와 유사 How do I display validation errors about an uploaded multipart file using Spring MVC

+1

그러나 이것은 서버가 먼저 파일을 완성한 다음 크기를 확인하십시오. 그러면 누군가 내 웹 서버에 1GB 파일을 업로드 할 수 있습니다. 웹 서버가 요청의 게시물 크기를 읽 자마자 연결을 끊으려면 xxxMB – Vinchenzo

+0

이상이어야합니다. 나는 당신이 http://commons.apache.org/proper/commons-fileupload/를 사용함으로써 그것을 성취 할 수있을 것이라고 생각한다. 또한 유사한 게시물 http://stackoverflow.com/questions/14075287/does-maxpostsize-apply-to-multipart-form-data-file-uploads가 있습니다. 희망이 도움이됩니다. –

+0

MultiPartFile을 업로드하지 않았기 때문에 필자의 상황에서는 잘 작동하지 않는다. 내가하고있는 일은 요청 본문에서 프런트 엔드에서 보낸 데이터를 직렬화하는 것입니다. 따라서 content-type이 application/json으로 설정된 텍스트 만 포함됩니다. – Vinchenzo