이 기사에서는 참조에 대한 Spring Boot 파일 업로드의 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
1. Maven 웹 프로젝트를 작성한 다음 pom.xml 파일을 구성하고 종속성을 추가하십시오.
<pectionency> <groupId> org.springframework.boot </groupid> <artifactid> 스프링 부트 스타터-web </artifactid> <bersion> 1.0.2.20.20 </version> </dependency>
2. WebApp 디렉토리의 index.jsp 파일에 양식을 입력하십시오.
<html> <body> <form method = "post"encType = "multipart /form-data"action = " /upload"> 파일 업로드 : <input type = "file"name = "file"> <br /> name : <입력 유형 = "text"name = "name"> <br /> <br /> <input type = "value ="upload "를 눌러 올리십시오! </form> </body>
이 양식은 시뮬레이션 업로드 페이지입니다
3.이 양식을 처리하는 컨트롤러를 작성하십시오.
import java.io.bufferedOutputStream; import java.io.file; import java.io.fileoutputStream; org.springframework.stereotyp.controller import; org.springframework.web.bind.annotation.requestmapping; org.springframework.web.bind.annotation.requestmethod; org.springframework.web.bind.annotation.requestparam; org.springframework.web.bind.annotation.responsebody; org.springframework.web.multipart.multipartfile; @controller public class fileUploadController {@requestMapping (value = "/upload", method = requestMethod.get) public @ResponseBody String upplyUploadInfo () {return "이 같은 URL에 게시하여 파일을 업로드 할 수 있습니다."; } @requestmapping (value = "/upload", method = requestmethod.post) public @ResponseBody String handleFileUpload (@RequestParam ( "name") String Name, @requestParam ( "file") multipartFile 파일) {if (! file.isempty ()) {tryte [] bytes = bytes = bytes = bytes =; BufferedOutputStream 스트림 = 새 BufferedOutputStream (새 파일 OutputStream (새 파일 (name + "-uploaded")); stream.write (바이트); stream.close (); " + name +"를 성공적으로 업로드 한 " + name +"-Uploaded! "; } catch (예외 e) {return "당신은 업로드하지 못했습니다" + name + "=>" + e.getMessage (); }} else {return "파일이 비어 있었기 때문에" + name + "를 업로드하지 못했습니다."; }}}4. 그런 다음 업로드 된 파일에 대한 제한 사항이 있으며 웹을 시작하는 주요 방법을 작성합니다.
org.springframework.boot.springApplication import; org.springframework.boot.autoconfigure.enableautoconfiguration; org.springframework.boot.context.embedded.multipartConfigFactory; import org.springframework.context.annotation.bean; org.springframework.context.annotation.componentscan; org.springframework.context.annotation.configuration; javax.servlet.multipartconfigelement import; @configuration @componentscan @enableautoconfiguration public class 응용 프로그램 {@bean public multipartconfigelement multipartconfigelement () {multipartConfigFactory factory = new multipartConfigFactory (); factory.setmaxfilesize ( "128kb"); Factory.SetMaxRequestSize ( "128KB"); return factory.createmultipartconfig (); } public static void main (String [] args) {springApplication.run (application.class, args); }}5. 그런 다음 http : // localhost : 8080/업로드를 방문하여 페이지를보십시오.
위의 예는 단일 파일을 업로드하는 기능을 구현합니다. 파일의 배치 업로드 기능을 구현하려고한다고 가정하면 위의 코드 만 단순히 수정하면됩니다. 길이 문제를 고려하면 다음은 위의 코드와 다른 코드이며 설명은 위와 같이 게시되지 않습니다. :
1. batchupload.jsp 파일을 추가하십시오
<html> <body> <form 메소드 = "post"enctype = "multipart/form-data"action = "/batch/upload"> 업로드하려면 파일 : <input type = "file"name = "file"> <br/> 파일 업로드 : <input type = "file" "file"> <br/> <value ""value = "value"> upload "를 눌렀습니다. </form> </body> </html>
2. BatchFileUploadController.java 파일 추가 :
org.springframework.steretype.controller import; org.springframework.web.bind.annotation.requestmapping; org.springframework.web.bind.annotation.requestmethod; org.springframework.web.bind.annotation.responsebody; org.springframework.web.multipart.multipartfile; org.springframework.web.multipart.multiparthttpservletrequest; import javax.servlet.http.httpservletrequest; import java.io.bufferedOutputStream; import java.io.file; import java.io.fileoutputStream; Java.util.list 가져 오기; /*** 2014/4/26에 wenchao.ren에 의해 만들어졌습니다. */@controller public class batchfileUploadController {@requestMapping (value = "/batch/upload", method = requestMethod.post) public @ResponseBody String handleUpload (httpservletRequest request) {list <ToptArttPile> files = ((multiptarthtTpservletRequest) 요청). for (int i = 0; i <files.size (); ++ i) {multipartfile file = files.get (i); 문자열 이름 = file.getName (); if (! file.isempty ()) {try {byte [] bytes = file.getBytes (); bufferedOutputStream 스트림 = 새 bufferedOutputStream (새 파일 OutputStream (새 파일 (name + i))); stream.write (바이트); stream.close (); } catch (예외 e) {return "당신은 업로드하지 못했습니다" + name + "=>" + e.getMessage (); }} else {return "파일이 비어 있었기 때문에" + name + "를 업로드하지 못했습니다."; }} "성공적으로 업로드"리턴; }}이러한 간단한 배치 업로드 기능은 괜찮습니다. 매우 간단하지 않습니까?
참고 : 위의 코드는 데모를위한 것이므로 인코딩 스타일은 캐주얼 방식을 채택하며 모든 사람이 모방하는 것이 좋습니다.
참조 : 멀티 패트 레졸버는 파일 업로드 기능을 구현합니다
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.
1. Multipartresolver는 파일 업로드 기능을 구현할 수도 있습니다. 참조 기사 :