Java 下载的附件不';无法显示正确的扩展名

Java 下载的附件不';无法显示正确的扩展名,java,postgresql,struts2,download,attachment,Java,Postgresql,Struts2,Download,Attachment,我使用postgresql作为后端。我已在case_attachments表中保存了附件和附件标题。 当我点击下载附件时,它会下载。但附件名称如下所示 下载附件文件,类型为操作。我会保存多种类型的附件,如jpg、pdf、doc等。因此,在下载时,文件名和扩展名必须正确 这是我的struts.xml <action name="downloadAttachFile" class="abc.actions.mypackage" method="downloadAttachFil

我使用postgresql作为后端。我已在case_attachments表中保存了附件和附件标题。 当我点击下载附件时,它会下载。但附件名称如下所示 下载附件文件,类型为操作。我会保存多种类型的附件,如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属性必须是私有的,在类级别(而不是像您那样在方法级别声明),带有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;
    }
    
  • 您需要存储、检索和设置真实、正确的
    contentType
    。这:

    application/octet-stream/text/plain/doc
    
    是一个可能会把你的浏览器变成噩梦发生器的怪物


  • 我更改了内容类型:应用程序/八位字节流和rest,正如u所说。它工作得非常棒。非常感谢,不客气。您的contentType正在工作,因为您正在将其作为
    附件
    下载,如果尝试在浏览器中使用
    内联
    打开它,它将无法正常工作(尽管
    应用程序/octet流
    是在源未知的情况下使用的正确contentType…)。在保存文件时考虑存储正确的内容类型,或者第一次尝试在浏览器中显示文件时,您将遇到许多Mime Types的问题。再次感谢你的帮助。
    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;
    }