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"; //실패 보기 되 돌리 기
}
}
}

좋은 웹페이지 즐겨찾기