繁体   English   中英

使用Struts2文件下载时无法正确显示字符

[英]Characters not displaying properly with Struts2 file download

我正在使用Struts2文件下载从AS400服务器下载文件。我正在下载的文件有CCSID 37(ebcdic),当它下载到PC时它不会显示任何垃圾字符。 当我在AS400服务器上显示它时显示正常。 请指教! 感谢帮助!

jsp表格:

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Download</title>
</head>
<body>
<s:form action="/execFileDownload">
<s:textfield key="serverFiletobeDownload" label="Server File"/>
<s:submit value="Download"/>
</s:form>
</body>
</html>

在struts.xml

<struts>
    <constant name="struts.multipart.maxSize" value="2000000000" />
    <package name="default"  extends="struts-default">
    <action name="execFileDownload" class="utils.action.FileDownloadAction">
            <result name="success" type="stream">
                <param name="contentType">application/octet-stream</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">attachment;filename="${fileName}"</param>
                <param name="bufferSize">4096</param>

            </result>
    </action>
<struts> 

行动类:

public class FileDownloadAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private InputStream inputStream;
    private long contentLength;
    private String fileName;
    public InputStream getInputStream() {
        return inputStream;
    }

    public long getContentLength() {
        return contentLength;
    }

    public String getFileName() {
        return fileName;
    }

    private String serverFiletobeDownload;

    public String getServerFiletobeDownload() {
        return serverFiletobeDownload;
    }

    public void setServerFiletobeDownload(String serverFiletobeDownload) {
        this.serverFiletobeDownload = serverFiletobeDownload;
    }

    public String execute() throws FileNotFoundException {
        File fileToDownload = new File(serverFiletobeDownload.trim());
        inputStream = new FileInputStream(fileToDownload);
        fileName = fileToDownload.getName();
        contentLength = fileToDownload.length();
        return SUCCESS;
    }
}

页面contentType和meta contentType标签实际上不进行任何转换。 它们只是向客户端声明HTTP响应的预期编码。 您可以使用适当的编码来转换/格式化输出。

我假设/ execFileDownload正确返回EBCDIC。 尝试在浏览器中点击/execFileDownload.do。 这将返回您的操作决定流式传输的任何字节。 您还应该考虑在该Action中设置适当的标头。 (同样,这只是声明代码已经使用的编码!)

// declare EBCDIC encoding. be sure to do this before calling response.getWriter()
response.setCharacterEncoding("Cp1047");

要么

response.setContentType("text/plain;charset=Cp1047");

如果你这样做,那么点击/execFileDownload.do,支持浏览器应该正确显示文本。 Internet Explorer有效。 Firefox没有。 Chrome /其他人不知道。

我根本不了解JSP文件的用途。 在您的示例中,没有人负责将字节从EBCDIC转换为ASCII。 在Struts或Java中没有自动执行此功能的内置功能。

评论中建议将其转换为“正常”字符编码,如cp819 / iso-8859-1,这是一个好主意。 这将为您提供更多关于渲染和支持多个浏览器的选项。

需要帮助转换字节流? 请参阅问题18170601了解一下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM