springMVC 지원 업로드 다운로드
2078 단어 spring
spring mvc web , spring MultipartResolver , org.springframework.web.multipart 。 CommonsMultipartResolver 。
web ( /WEB-INF/config/app-config.xml) :
jar (spring jar ):
com.springsource.org.apache.commons.io-1.4.0.jar
com.springsource.org.apache.commons.fileupload-1.2.0.jar
HTML :
Spring MVC 3.0
//action html , HTML , spring
파일 업로드 요청 을 처리 하기 위해 controller(컨트롤 러)를 만 듭 니 다.FileUploadController.java:@Controller//이 종 류 를 컨트롤 러 클래스 로 설명 합 니 다.
public class FileUploadController 는 ServletContextAware{/를 구현 하여 ServletContextAware 인 터 페 이 스 를 구현 하고 로 컬 경 로 를 가 져 옵 니 다.
private ServletContext servletContext;
public void setServletContext(ServletContext servletContext){/인터페이스 에 있 는 setServletContext 방법 구현
this.servletContext = servletContext;
}
@RequestMapping(value="/upload",method=RequestMethod.POST)//파일 업로드 요청 을 이 방법 에 비 추기
public String handleFormUpload(@RequestParam("name")String name,/요청 매개 변수의 이름과 종 류 를 설정 합 니 다.
@RequestParam("file")Commons MultipartFile mFile){//요청 매개 변 수 는 form 의 매개 변수 이름과 반드시 대응 해 야 합 니 다.
if (!mFile.isEmpty()) {
String path = this.servletContext.getRealPath("/tmp/"); //로 컬 저장 경로 가 져 오기
File file = new File(path + new Date().getTime() + ".jpg"); //새 파일
try {
mFile.getFileItem().write(file); //업로드 한 파일 을 새 파일 에 기록 합 니 다.
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:uploadSuccess"; //성공 보기 되 돌리 기
}else {
return "redirect:uploadFailure"; //실패 보기 되 돌리 기
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.