JSR-286 + Struts 2.2.0 + PortletPlugin 2.2.0 を使用しています
ユーザーがダウンロードしたいファイルの名前を設定できません。ユーザーはファイルを取得できますが、その名前は壊れています。「myImage.png」の代わりに、ユーザーは「241883e9」または「241563a2」を取得します。ユーザーがダウンロードしたファイルの名前を変更して開くと、ファイルが破損していないことがわかります。私のコードを見てください:
file-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 行を見たことがありません: LOG.trace("#getFileName {}", attachement.getName());
でもわかる
[25.01.11 23:26:46:582 MSK] 00000052 srt W com.ibm.ws.webcontainer.srt.SRTServletResponse setHeader 警告: ヘッダーを設定できません。応答はすでにコミットされています。
応答のヘッダーを設定できないようです... :(
私は何を間違っていますか?助けてください。
UPD:部分的な解決策を見つけました:このコードをアクションに追加しました:
PortletActionContext.getResponse().setProperty("content-Disposition", "attachment;filename=\""+attachement.getName()+"\"");
PortletActionContext.getResponse().setProperty("content-Type", attachement.getMimeType());
現在、問題はファイル名にあります。ASCII 文字以外のファイル名が含まれていると、ファイル名が破損します。「my file.doc」、「02.png」などのファイル名は正常に機能します。