2011-01-25 36 views
0

我使用JSR-286 + Struts的2.2.0 + PortletPlugin 2.2.0下載文件pFrom中門戶網站,Struts的2.2.0 + PortletPlugin 2.2.0在IBM WP 6.1.5

我可以」 t爲用戶想要下載的文件設置名稱。 用戶可以獲取文件,但名稱已損壞。而不是「myImage.png」用戶獲得「241883e9」或「241563a2」。 如果用戶重命名下載的文件並打開它,他可以看到文件沒有損壞。請參閱我的代碼:

文件listing.jsp:

<li onclick="goToAction('<s:url action="downloadattachement" portletUrlType="resource" />', {'attachementId':<s:property value="id" />}, 'POST')"><s:property value="name"/></li> 

功能 「goToAction」 從動態生成並提交(我都試過:POST和GET,它不幫助):

<form action="/wps/myportal/!VERY_LONG_PORTAL_URL_GOES_HERE/" method="POST" id="actionUrlTemporaryForm1295987206509"> <input type="hidden" name="attachementId" value="2" /> </form> 

我的Struts的xml配置文件:

<!-- Download attached file by attachementId --> 
    <action name="downloadattachement" class="ru.portal.DownloadAttachementAction"> 
     <result name="success" type="stream"> 
      <param name="allowCaching">false</param> 
      <param name="contentType">${contentType}</param> 
      <param name="inputName">attachementContents</param> 
      <param name="contentDisposition">>attachment;filename="${fileName}"</param> 
      <param name="bufferSize">1024</param> 
     </result> 
    </action> 

和動作代碼:

@Override 
    protected String bareExecute() throws Exception { 
     String result = Action.SUCCESS;  
     Attachement attachement = EJBUtil.lookup(IAttachementManager.class).get(attachementId); 
     LOG.info("Trying to download Attachement[{}]", attachement); 
     File attachementFile = new File(attachement.getPath());  
     if(attachementFile.exists()){ 
      attachementContents = new FileInputStream(attachementFile); 
     }else{ 
      LOG.error("There is no attachement[{}] file here[{}]",attachementId, attachement.getPath()); 
     } 


     return result; 
    } 

    public String getContentType(){ 
     return attachement.getMimeType(); 
    } 

    public String getFileName(){ 
     LOG.trace("#getFileName {}", attachement.getName()); 
     return attachement.getName(); 
    } 

    public Integer getAttachementId() { 
     return attachementId; 
    } 

    public void setAttachementId(Integer attachementId) { 
     this.attachementId = attachementId; 
    } 

    public Attachement getAttachement() { 
     return attachement; 
    } 

    public InputStream getAttachementContents() { 
     return attachementContents; 
    } 

    @Override 
    public String getCurrentActionName() {  
     return "downloadattachement"; 
    } 

我從來沒有見過在我的日誌文件此日誌行: LOG.trace( 「#{的getFileName}」,attachement.getName());

但我看到

[11年1月25日23:26:46:582 MSK] 00000052 SRTW¯¯com.ibm.ws.webcontainer.srt.SRTServletResponse的setHeader警告:無法設置報頭。 Resse onse已經提交。

好像我不能爲響應設置頭... :(

,該怎麼辦錯了,請幫

UPD:我已經找到了部分解決方案: 我已經添加該代碼到我的行動:

PortletActionContext.getResponse().setProperty("content-Disposition", "attachment;filename=\""+attachement.getName()+"\"");      
PortletActionContext.getResponse().setProperty("content-Type", attachement.getMimeType()); 

問題出在文件名稱現在:如果包含非ASCII字符的文件名損壞像 文件名:「我FILE.DOC」,「02.png」工作正常。

回答

1

問題出現在result type =「stream」中,也出現在「Content-disposition」頭的文件名屬性值中。 對於FF我已經使用了ISO-8859-1,對於IE6-8我已經使用了url編碼。 我已經使用用戶代理頭來確定瀏覽器。我的解決方案只有一個問題,但對我來說它是可以接受的:IE8用下劃線替換文件名中的空格。例如 「my fav image.png」將是「my_fav_image.png」是IE8。 FF確實爲HTTP默認編碼,並且不會嘗試破壞文件名屬性值。 你可以在這裏找到更多的信息,StackOverflow。