0

我正在使用 JSR-286 + Struts 2.2.0 + PortletPlugin 2.2.0

我无法为用户想要下载的文件设置名称。用户可以获取文件,但其名称已损坏。而不是“myImage.png”用户得到“241883e9”或“241563a2”。如果用户重命名下载的文件并打开它,他可以看到文件没有损坏。请看我的代码:

文件列表.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 char 文件名已损坏。文件名如:“my file.doc”、“02.png”可以正常工作。

4

1 回答 1

1

问题出在结果 type="stream" 以及“Content-disposition”标头的文件名属性值中。对于 FF,我使用了 ISO-8859-1,对于 IE6-8,我使用了 url-encoding。我使用用户代理标头来确定浏览器。我的解决方案只有一个问题,但对我来说这是可以接受的:IE8 将文件名中的空格替换为下划线。例如“my fav image.png”将是“my_fav_image.png”是IE8。FF 确实无法理解 HTTP 的默认编码,并且不会尝试破坏文件名属性值。您可以在 StackOverflow 上找到更多信息。

于 2011-01-31T12:15:21.260 回答