2013-03-12 148 views
-1

我能夠上傳我的web應用程序與struts2框架中的圖片,但我無法理解路徑。 如何獲取圖像的路徑作爲URL,以便我可以使用它在<img src="url"/>中進一步處理。上傳後無法獲取正確的文件路徑

這是我的動作類源代碼,我提到了評論中返回的URL,但是URL對我沒有任何意義。我如何解密到實際的網址?

public class AddItemAction extends ActionSupport implements 
     ServletContextAware { 

    @Override 
    public void setServletContext(ServletContext arg0) { 
     // TODO Auto-generated method stub 

    } 

    File pic; 
    String picContentType; 
    String picFileName; 

    public File getPic() { 
     return pic; 
    } 

    public void setPic(File pic) { 
     this.pic = pic; 
    } 

    public String getPicContentType() { 
     return picContentType; 
    } 

    void setPicContentType(String picContentType) { 
     System.out.println("Setting conteent tuype" + picContentType); 
     this.picContentType = picContentType; 
    } 

    public void setPicFileName(String picFileName) { 
     this.picFileName = picFileName; 
    } 

    public String getPicFileName() { 
     return picFileName; 
    } 

    public String execute() { 
     File file = getPic(); 
     String strFinalFullPathFileName = file.getAbsolutePath() + File.separator + picFileName; 
     System.out.println(strFinalFullPathFileName); 

     // This is the path returned 
     /* 
     * /Users/..../Catalina/localhost/.../upload_584d2719_13d5fdf593d__8000_00000000.tmp/IMG_20120526_083438.jpg 
     * 
     * 
     */ 

     return SUCCESS; 
    } 
} 
+1

創建其獲取圖像的操作和使用,作爲URL的路徑。 – Quaternion 2013-03-12 18:46:06

+0

@Quaternion你的意思是getPic();但是當我嘗試執行getPic()。getPath()時,它給了我os文件路徑,即/ Users/... – 2013-03-12 18:48:08

+0

不要在註釋中提及實際的URL,請更新問題。 – 2013-03-12 18:52:51

回答

0

上傳工件應該被存儲該網絡應用程序的結構。

另外,默認情況下,文件上傳攔截器會刪除上傳過程中創建的臨時文件。這應該被關閉,或者應該將文件複製到已知位置,以便它們可以(a)通過動作流回,或者(b)如果您設置了容器以提供靜態資產正常的網頁結構。

+0

我們如何關閉它?我們能做到嗎? – 2013-03-12 18:56:45

+0

@ArunAbraham不會不定製上傳攔截器;我認爲我們已經爲此添加了開關,但我想我們沒有。 – 2013-03-12 19:18:14

+0

現在才意識到它。尚未解決問題,但想法是它只是暫時存儲。 – 2013-03-12 20:43:36

0

好像你是上傳文件到臨時文件夾,你應該做的是這個文件移動到文件夾您的Web應用程序內

你使用的情況下request.getServletContext()。getRealPath(YOUR_PATH)獲得其中,直接移動文件

YOUR_PATH是像「/uploadimage/img.png」 => uploadimage是一個文件夾中,你的web應用

+0

然後使用的URL是YOUR_CONTEXT_PATH_IF_ANY +「/uploadimage/img.png」 – 2013-03-12 18:55:39

+0

將上傳的工件*放入* web應用程序結構中幾乎不是一個好主意。事實上,如果你正在部署一個戰爭文件(最常見的情況),你*不能*移動的東西到Web應用程序。 – 2013-03-12 18:56:20

相關問題