SpringMVC에서 파일을 업로드하는 세 가지 방법, 코드를 업로드하면 한눈에 알 수 있습니다.
프론트 데스크 :
<%@ page language = "java"contmenttype = "text/html; charset = utf-8"pageencoding = "utf-8"%> <! doctype html public "-// w3c // dtml 4.01 Transitional // en" "http://www.w3.org/tr/tr/tr/html4/loose.dtd"> html> html> http-quiv ="content-type "content ="text/html; charset = utf-8 "> <title> 제목 </head> <body> <comper name = comper form" action = "/springmvc006/fileUpload"method = "post"post "encType ="multipart/form-data "> <h1> 스트리밍을 사용하여 파일 업로드 </h1> <file" "file"name = "file"> <입력 유형 = "value ="upload "// form> <form> <form>"action = "action =" "" " EncType = "multipart/form-data"> <h1> file.transfer method </h1> <input type = "file"name = "file"> <input type = "value ="value = "upload"/</form> <form name = "form2"action = "/springmvc006/springupload"method = "post" ENCTYPE = "multipart/form-data"> <h1> 스프링 mvc에서 제공 한 메소드를 사용하여 파일을 업로드하십시오 </h1> <input type = "file"name = "file"> <input type = "value ="value = "upload"/> </body> </html> </html>
구성:
<!-Multipart 파일 업로드-> <bean id = "multipartresolver"> <속성 이름 = "maxUploadsize"value = "104857600" /> <property name = "maxInMemorySize"value = "4096" /> <속성 이름 = "defaultEncoding"value = "utf-8"> < /bean>
무대 뒤에서:
방법 1 : 방법 1
/** 스트림을 통해 파일을 업로드* @requestparam ( "file") 이름 = 파일 컨트롤로 얻은 파일을 commonsmultipartFile Object*/@requestMapping ( "fileUpload") public string fileUpload ( "file") commonsmultipartfile 파일)로 캡슐화합니다. STARTTIME = SYSTEM.CURRENTTIMEMILLIS (); System.out.println ( "filename :"+file.getoriginalFilename ()); 시도 {// 출력 스트림 출력 스트림 os = new FileOutputStream을 가져옵니다 ( "e :/"+new date (). gettime ()+file.getoriginalFilename ()); // 입력 스트림을 가져옵니다 CommonSmultipartFile 파일의 스트림을 직접 얻습니다. 입력 스트림 IS = file.getInputStream (); int 온도; // 바이트를 하나씩 읽고 씁니다. } os.flush (); os.close (); is.close (); } catch (filenotfoundException e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } long endtime = System.CurrentTimeMillis (); System.out.println ( "메소드 실행 시간 :"+string.valueof (endtime-starttime)+"ms"); 반품 "/성공"; }방법 2 : 방법 2.
/** 파일을 사용하여 업로드 된 파일을 저장하려면*/ @requestmapping ( "fileUpload2") public String fileUpload2 (@requestParam ( "file") CommonSmultIpartFile 파일) IoException {long starttime = system.currentTimeMillis (); System.out.println ( "filename :"+file.getoriginalFilename ()); 문자열 path = "e :/"+new date (). gettime ()+file.getoriginalFilename (); 파일 newFile = 새 파일 (Path); // commonsmultipartFile 메소드를 통해 직접 파일을 작성합니다 (이번에는 참고) 파일 .transferto (NewFile); Long Endtime = System.CurrentTimeMillis (); System.out.println ( "메소드 2의 실행 시간 :"+string.valueof (endtime-starttime)+"ms"); 반품 "/성공"; }메소드 3 :
/**Spring*/ @requestmapping ( "SpringUpload") 공개 문자열 SpringUpload (httpservletrequest request)가 제공하는 파일 업로드 방법을 사용합니다. // 현재 컨텍스트를 CommonSmutipartresolver (Multipart Parser) Commonsmultipartresolver Multipartresolver = New Commonsmultipartresolver (request.getSession (). getServletContext ())로 초기화합니다. // if (multipartresolver.ismultipart (request)) {// multiparthtpservletrequest multirequest = (multiparthttpservletrequest) 요청; // multirequest iterator iter = multirequest.getFilenames ()에서 모든 파일 이름을 가져옵니다. while (iter.hasnext ()) {// 한 번에 모든 파일을 한 번에 수송합니다. if (file! = null) {String path = "e :/springUpload"+file.getoriginalFilename (); // file.transferto (새 파일 (path)); }}} long endtime = System.CurrentTimeMillis (); System.out.println ( "메소드 3의 실행 시간 :"+string.valueof (endtime-starttime)+"ms"); 반품 "/성공"; }테스트 업로드 시간을 살펴 보겠습니다.
처음 4m 파일을 사용했을 때 :
파일 이름 : test.rar
방법 1 실행 시간 : 14712ms
파일 이름 : test.rar
방법 2 실행 시간 : 5ms
방법 3 실행 시간 : 4ms
두 번째로 : 50m 파일을 사용합니다
이 방법의 진행 상황은 매우 느리고 5 분이 걸립니다.
방법 2 실행 시간 : 67ms
방법 3 실행 시간 : 80ms
테스트 결과에서 SpringMVC를 사용하여 파일을 업로드하는 방법이 훨씬 빠릅니다!
테스트 2의 결과 : 방법 3을 하나씩 검색해야하므로 느리게해야 할 수도 있습니다. 그러나 일반적으로 말하면, 우리는 더 많은 방법을 제공 할 수 있기 때문에 방법 3이 있습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.