2011-08-16 5 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に正しくアップロードされます。私はtomcatのディレクトリの代わりに私のpublic_htmlディレクトリにstruts 2の写真をアップロードできますか?

実際にはこの画像を/home/xrcwrn/public_html/imgフォルダにアップロードします。

私は、Struts 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()を使用しないでください。あなたはそこにアップロードされたファイルを書いたくありません。 Webアプリケーションを再デプロイするたびに、またはサーバを再起動しても失われます。

は、この構成可能にしたい場合は、システムプロパティまたはプロパティファイルの設定として、それを提供することを検討

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

によって

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

を交換してください。

関連する問題