简体   繁体   English

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

[英]Characters not displaying properly with Struts2 file download

I am using Struts2 file download for downloading a file from AS400 server.The file which i am downloading has CCSID 37(ebcdic) and when it is downloaded to PC it doesnot show anything just junk characters. 我正在使用Struts2文件下载从AS400服务器下载文件。我正在下载的文件有CCSID 37(ebcdic),当它下载到PC时它不会显示任何垃圾字符。 When i display it on AS400 server its shows fine . 当我在AS400服务器上显示它时显示正常。 Please suggest ! 请指教! Help is appreciated! 感谢帮助!

jsp form : 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.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> 

Action class: 行动类:

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;
    }
}

The page contentType and meta contentType tags don't actually do any conversion. 页面contentType和meta contentType标签实际上不进行任何转换。 They simply declare to the client the expected encoding of the HTTP response as-is. 它们只是向客户端声明HTTP响应的预期编码。 It's up to you to translate/format the output in the appropriate encoding. 您可以使用适当的编码来转换/格式化输出。

I'm assuming that /execFileDownload is returning EBCDIC correctly. 我假设/ execFileDownload正确返回EBCDIC。 Try just hitting /execFileDownload.do in your browser. 尝试在浏览器中点击/execFileDownload.do。 That will return whatever bytes your action has decided to stream. 这将返回您的操作决定流式传输的任何字节。 You should also consider setting the appropriate headers in that Action. 您还应该考虑在该Action中设置适当的标头。 (Again, this just declares which encoding your code is already using!) (同样,这只是声明代码已经使用的编码!)

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

or 要么

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

If you do this, then hit /execFileDownload.do, supporting browsers SHOULD display the text correctly. 如果你这样做,那么点击/execFileDownload.do,支持浏览器应该正确显示文本。 Internet Explorer works. Internet Explorer有效。 Firefox doesn't. Firefox没有。 Chrome/others dunno. Chrome /其他人不知道。

I don't understand the purpose of the JSP file at all. 我根本不了解JSP文件的用途。 In your example, no one is responsible for converting the bytes from EBCDIC into ASCII. 在您的示例中,没有人负责将字节从EBCDIC转换为ASCII。 There isn't a built-in feature to do this automatically in Struts or Java. 在Struts或Java中没有自动执行此功能的内置功能。

The recommendation in comments to convert this into a "normal" character encoding such as cp819/iso-8859-1 is a good idea. 评论中建议将其转换为“正常”字符编码,如cp819 / iso-8859-1,这是一个好主意。 That will give you many more options on rendering and support multiple browsers. 这将为您提供更多关于渲染和支持多个浏览器的选项。

Need help converting the byte stream? 需要帮助转换字节流? See question 18170601 for a start. 请参阅问题18170601了解一下。

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

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