做了一个功能,火狐浏览器,支持Linux,导出人员准考证,配置的struts,导出内容成功,但是导出文件名乱码,各种方法转码都有用,还是乱码,可能是个人水平有限. 这是我的代码,
如果问题代码没必要看,可以直接看下面的解决方法。如果问题代码你有解决方式也可以评论分享。
public String dowloadWord() {
try {
//解决中文乱码
filePath = URLDecoder.decode(filePath, "UTF-8");
fileOnlyName = URLDecoder.decode(fileOnlyName, "UTF-8");
fileName = URLDecoder.decode(fileName, "UTF-8");
new FileInputStream(filePath + File.separator + fileOnlyName);
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return "dowloadWord";
}
public InputStream getWordFile(){
try {
// HttpServletResponse response = ServletActionContext.getResponse();
// response.reset(); //可以加也可以不加
// response.setContentType("application/x-download"); //设置为下载application/x-download
//解决中文乱码
//fileName = URLDecoder.decode(("导出的Word文档1.doc"), "iso-8859-1");
//this.fileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(fileName.getBytes("UTF-8")))) + "?=";
//fileName = "用freemarker导出的Word文档.doc";
//new String(u.getBytes("UTF-8"), "iso-8859-1")
String uncod=URLDecoder.decode("用freemarker导出的Word文档.doc","UTF-8");
fileName = new String(uncod.getBytes("UTF-8"), "iso-8859-1");
/** 返回最终生成的word文件流 */
return ServletActionContext.getServletContext().getResourceAsStream(filePath + File.separator + fileOnlyName);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
<action name="createWord" class="com.share.WordAction" method="createWord">
<result name="createWordSuccess" type="redirectAction">
<param name="actionName">dowloadWord</param>
<param name="filePath">${filePath}</param>
<param name="fileOnlyName">${fileOnlyName}</param>
<param name="fileName">${fileName}</param>
</result>
</action>
<action name="dowloadWord" class="com.share.WordAction" method="dowloadWord">
<result name="dowloadWord" type="stream">
<param name="contentType">application/octet-stream;</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="inputName">WordFile</param>
</result>
<result name="error">/error.jsp</result>
</action>
最后解决方法,用ifame,普通ajax没什么好说的,Urlid是获取的路径----------------------------------------------------------------------------------------------------解决方法在这里
$.ajax({
url:"",
data:{
},
type:"post",
dataType:"json",
cache:false,
beforeSend:function(){XMLHttpRequest},
success:function(data){
if(data.returnCode == 'error') {
alert(data.returnMsg);
}
else
{
alert(data.filePath);
$("#iFexam").attr("src",encodeURI(encodeURI("./pages/exportFile/ShareTemplate.jsp?UrlId="+data.filePath)));
}
},
error:function(XMLHttpRequest){}
});
<iframe id="iFexam" style="height:100%;display:none; width:100%;background-color: white;" src="" scrolling="no" frameborder="no" border="0" marginwidth="0" marginheight="0" name="iexam"></iframe>
ShareTemplate.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'ShareTemplate.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//调用上面的方法、生成文件
String UrlId=java.net.URLDecoder.decode(request.getParameter("UrlId"), "utf-8");;
String fileName =UrlId;
String[] arry = UrlId.split("/");
//获取文件名
String uName=arry[arry.length-1];
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + new String(uName.getBytes("UTF-8"), "iso-8859-1"));
response.flushBuffer();//把数据发往客户住!
response.setContentType("application/octet-stream");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
ServletOutputStream sos=response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(sos);
byte[] buffer = new byte[1024];
int len = -1;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
out.clear();
out = pageContext.pushBody();
bos.close();
sos.close();
bis.close();
//删除服务器问题
File file=new File(fileName);
file.delete();
%>
</body>
</html>
我的思路就是很长时间解决不了问题,就换其他解决方法,成功。