[英]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.