我在Struts2中开发了一个动态文件下载程序,该程序允许用户下载存储在数据库中的文件。
我的问题是,如果文件名包含非英语字符(在本例中为“泰国字符”)。文件名将显示为URL。
这是我的JSP
<s:url id="urlTag" action="downloadAmsFile">
<s:param name="index">1</s:param>
</s:url>
<s:a href="%{urlTag}">Download</s:a>
这是我的结果参数的Java代码
setFilename(assetObj.getFileName());
setMimeType(assetObj.getContentType());
setInputStream(new ByteArrayInputStream(assetObj.getBytes()));
这是我的行动
<action name="downloadAmsFile" class="com.title.ams.action.FileUploadAllAction" method="downloadAmsFile">
<result name="success" type="stream">
<param name="contentType">%{contentType}</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename=%{contentDisposition}</param>
<param name="bufferSize">1024</param>
</result>
</action>
如果我使用非英语语言下载文件,则文件名将为“ downloadAmsFile.action?index = 1”。
找到了!我必须将文件名转换为unicode。
String result = "";
if (text != null) {
StringBuilder ascii = new StringBuilder(text);
for(int i = 0; i < text.length(); i++) {
int code = (int)text.charAt(i);
if ((0xE01<=code) && (code <= 0xE5B )){
ascii.setCharAt( i, (char)(code - 0xD60));
}
}
result = ascii.toString();
}
return result;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句