3

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;
    }
}
Barett
  • 5,826
  • 6
  • 51
  • 55
abhinav singh
  • 856
  • 6
  • 23
  • 45
  • Have you checked that the individual character fields are ccsid 37? – WarrenT Apr 03 '14 at 03:32
  • I am trying to copy a ifs file text,pdfs etc – abhinav singh Apr 03 '14 at 04:09
  • Ah yes. I could have seen that if I had read through your code. – WarrenT Apr 03 '14 at 04:17
  • 1
    How do you "display it on AS400 server"? That can lead to more questions. – user2338816 Apr 03 '14 at 11:55
  • 1
    a PDF in the IFS should't have a CCSID of 37. 5348 or 1252 would make more sense. A plain .TXT could have CCSID of 37. But your download process would have to translate it. For .TXT files, the system can be configured to automatically translate from certain interfaces. But I don't know what STRUTS is using. My guess is it's doing a binary download. The best solution would be to output the file a ASCII in the first place. – Charles Apr 03 '14 at 14:50
  • Yes .. I have to change the ccsid to 819 and then download it . – abhinav singh Apr 08 '14 at 04:33
  • Side note: you should remove or modify this line; it conflicts with your contentType page attribute. – Barett Jun 13 '14 at 22:01

1 Answers1

1

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.

Community
  • 1
  • 1
Barett
  • 5,826
  • 6
  • 51
  • 55