2016-06-11 10 views
0

struts 액션에서 파일을 업로드하고 싶습니다. 내 폴더에 대한 해당 작업의 경로가 필요합니다 : 나는 struts 2의 특정 폴더에 파일을 업로드하는 방법

String contextPath = request.getContextPath(); 

를 사용하여 시도

하지만 난 당신의 프로젝트 폴더에 상위 폴더입니다 java.lang.NullPointerException

+0

파일을 다운로드 하시겠습니까? –

+0

아니요, WebContent의 특정 폴더에 업로드하려고합니다. –

+2

구체적인 문제를 명확히하거나 추가 정보를 추가하여 필요한 것을 정확하게 강조하십시오. 현재 작성된 내용이므로 귀하가 원하는 내용을 정확하게 말하기는 어렵습니다. How to Ask 페이지에서이 질문에 대한 설명을 참조하십시오. –

답변

0

어느 카탈리나에 저장납니다

 String rootPath = System.getProperty("catalina.home"); 
     File dir = new File(rootPath + File.separator + "yourfolderName"); 
     if (!dir.exists()) 
      dir.mkdirs(); 

     // Create the file on server 
     java.util.Date date= new java.util.Date(); 
     String Path = dir.getAbsolutePath() + File.separator + (new Timestamp(date.getTime())).toString().replace(":", "").toString().replace(".", ".").toString().replace(" ","").toString().replace("-","").toString()+".pdf"; 

또는 프로젝트에 폴더를 만들고 거기에 저장하십시오.

if (!file.isEmpty()) { 
    //filter for checking file extewnsion 
    if(file.getContentType().equalsIgnoreCase("image/jpg") || file.getContentType().equalsIgnoreCase("image/jpeg")){ 
     //if file is >2 MB or < 2MB 
     double size = file.getSize(); 
     double kilobytes = (size/1024); 
     double megabytes = (kilobytes/1024); 
     if(megabytes<2){ 
    try { 
     byte[] bytes = file.getBytes(); 
     String filePath = request.getRealPath("/")+"yourFolderName\\ProfileImages\\"+SessionManagement.getUserName()+".jpg"; 
     BufferedOutputStream stream = 
       new BufferedOutputStream(new FileOutputStream(new File(filePath))); 
     stream.write(bytes); 
     stream.close(); 

     //console call 
    } 
    else{ 
     model.put("error", "Please select File less than 2 MB"); 
     return new ModelAndView("uploadPhotoTile"); 
    } 
    }else{ 
     model.put("error", "Please select JPEG File"); 
     return new ModelAndView("uploadPhotoTile"); 
    } 
} else { 
    model.put("error", "Please select File"); 
    return new ModelAndView("uploadPhotoTile"); 
} 
관련 문제