简体   繁体   中英

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. When i display it on AS400 server its shows fine . Please suggest ! Help is appreciated!

jsp form :

<%@ 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> 

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. They simply declare to the client the expected encoding of the HTTP response as-is. It's up to you to translate/format the output in the appropriate encoding.

I'm assuming that /execFileDownload is returning EBCDIC correctly. Try just hitting /execFileDownload.do in your browser. That will return whatever bytes your action has decided to stream. You should also consider setting the appropriate headers in that 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. Internet Explorer works. Firefox doesn't. Chrome/others dunno.

I don't understand the purpose of the JSP file at all. In your example, no one is responsible for converting the bytes from EBCDIC into ASCII. There isn't a built-in feature to do this automatically in Struts or Java.

The recommendation in comments to convert this into a "normal" character encoding such as cp819/iso-8859-1 is a good idea. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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