SpringMVC 는 제3자 구성 요 소 를 사용 하여 파일 업 로드 를 실현 합 니 다.

1.파일 업로드 에 필요 한 전제
A form 폼 의 enctype 값 은 다음 과 같 아야 합 니 다:multipart/form-data
(기본 값 은:application/x-ww-form-urlencoded)enctype:폼 요청 본문 형식 입 니 다.
B method 속성 값 은 Post 여야 합 니 다.
C 파일 선택 영역 input type=file 제공
2.제3자 구성 요 소 를 통 해 파일 업로드 실현

셋째,

/**
 * springmvc        
 * @return
 */
@RequestMapping("/testFileUpload1")
public String testFileUpload1(MultipartFile upload) throws IOException {
  System.out.println("        ...");

  //            
  String path = "http://localhost:9090/uploads/";

  //        
  //          
  String filename = upload.getOriginalFilename();
  //            ,uuid
  String uuid = UUID.randomUUID().toString().replace("-", "");
  filename = uuid+"_"+filename;

  //         
  Client client = Client.create();
  //           
  WebResource webResource = client.resource(path + filename);
  //     
  webResource.put(upload.getBytes());
  return "success";
}




/**
 *
 *    upload   index.jsp upload  
 *
 * springmvc    
 * @return
 */
@RequestMapping("/testFileUpload")
public String testFileUpload(HttpServletRequest request, MultipartFile upload) throws IOException {
  //   fileupload        
  //      (              )
  String path = request.getSession().getServletContext().getRealPath("/uploads/");
  //   ,       
  File file = new File(path);
  if(!file.exists()){
    file.mkdir();
  }
  //        
  //          
  String filename = upload.getOriginalFilename();
  //            ,uuid
  String uuid = UUID.randomUUID().toString().replace("-", "");
  filename = uuid+"_"+filename;
  //       
  upload.transferTo(new File(path,filename));
  return "success";
}
4.springmvc.xml 설정 파일 해상도 기

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기