gpt4 book ai didi

java - 下载的附件不显示正确的扩展名

转载 作者:行者123 更新时间:2023-12-01 12:32:20 25 4
gpt4 key购买 nike

我使用 postgresql 作为后端。我已将附件和附件标题保存在 case_attachments 表中。当我点击下载附件时,它确实下载了它。但附件名称是这样的downloadAttachFile 并将类型设置为 action 。我会保存多种类型的附件,例如 jpg、pdf、doc 等。因此,在下载时,文件名和扩展名正确非常重要。

这是我的 struts.xml

        <action name="downloadAttachFile" class="abc.actions.mypackage" method="downloadAttachFile">
<result name="success" type="stream">
<param name="contentType">application/octet-stream/text/plain/doc</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename=%{AttachFileName}</param>
<param name="bufferSize">1024</param>
</result>
</action>

及 Action 方法

    public String downloadAttachFile() throws FileNotFoundException {
String AttachFileName = ServletActionContext.getRequest().getParameter("myFileFileName");
fileInputStream = new FileInputStream(new File(AttachFileName));
return SUCCESS;
}

当我右键单击并使用正确的工具打开它时,它会正确打开。所以这里唯一的问题是名称和扩展类型。

最佳答案

目前尚不清楚为什么,如果您从数据库中检索它,那么您正在从请求中读取它......顺便说一句:

  1. 您的操作属性必须是私有(private)的,在类级别(而不是像您那样在方法级别声明),具有 getter,并以小写字符开头以遵循约定:

    private String attachFileName;

    public String getAttachFileName(){
    return attachFileName;
    }

    public String downloadAttachFile() throws FileNotFoundException {
    attachFileName = ServletActionContext.getRequest().getParameter("myFileFileName");
    fileInputStream = new FileInputStream(new File(AttachFileName));
    return SUCCESS;
    }
  2. 您需要存储、检索和设置真实、正确的contentType。这:

    <param name="contentType">application/octet-stream/text/plain/doc</param>

    是一个怪物,可能会将您的浏览器变成噩梦发生器。

关于java - 下载的附件不显示正确的扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25842642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com