2011-08-16 3 views
2

1-6969875-2644-t.jpg을 업로드하면 이미지가 /home/xrcwrn/jvm/apache-tomcat-6.0.14/domains/xyz.com/ROOT/img/1-6969875-2644-t.jpg으로 올바르게 업로드됩니다.sturuts 2의 사진을 tomcat의 디렉토리 대신 public_html 디렉토리에 업로드하려면 어떻게해야합니까?

하지만 실제로이 이미지를 /home/xrcwrn/public_html/img 폴더에 업로드하고 싶습니다.

나는 스트럿츠 2에서이 코드를 사용하고 있습니다 :

public String execute() { 

    try { 
     ServletContext servletContext = ServletActionContext.getServletContext(); 
     String path =servletContext.getRealPath("/img"); 
     System.out.println("Server path:" + path); 
     String filePath = servletContext.getRealPath(path); 
     File uploadDir = new File(filePath); 
     String relPath = uploadDir.getAbsolutePath(); 
     //if the folder does not exits, creating it 
     if (uploadDir.exists() == false) { 
      uploadDir.mkdirs(); 
     } 
     File fileToCreate = new File(path, this.userImageFileName); 
     FileUtils.copyFile(this.userImage, fileToCreate); 
     String pt = path + "/" + getUserImageFileName(); 
     System.out.println("image path is :" + pt); 
     setImagePath(pt); 
     } catch (Exception e) { 

     e.printStackTrace(); 
     addActionError(e.getMessage()); 
     return INPUT; 
    } 
    System.out.println(" **************inside image upload***********"); 

    return SUCCESS; 
} 

답변

4

ServletContext#getRealPath()를 사용하지 마십시오. 업로드 된 파일을 거기에 쓰지 않으려 고합니다. 웹 응용 프로그램을 재배포 할 때마다 또는 서버를 다시 시작할 때도 손실됩니다. 당신이이 구성 가능을 시스템 속성 또는 등록 정보 파일의 설정으로 제공 고려할 경우

String filePath = "/home/xrcwrn/public_html/img"; 

에 의해

String path =servletContext.getRealPath("/img"); 
System.out.println("Server path:" + path); 
String filePath = servletContext.getRealPath(path); 

를 교체합니다.

관련 문제