admin管理员组文章数量:1567572
2024年7月25日发(作者:)
下面代码有选择的粘贴进自己项目即可,其实现解决了tomcat、resin服务器中文下载乱码
问题。
配置如下
xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_2_">
Jsp页面配置如下其下载利用a标签进行连接,前提是服务器上下载文件路径已知
需要引进 el表达式和jquery.
<%@ page language="java" import=".*" pageEncoding="utf-8"%>
<%
String path = textPath();
String basePath =
eme()+"://"+verName()+":"+verPort()+path+"/";
%>
<%@ taglib uri="/jsp/jstl/core" prefix="c"%>
过滤器filter配置如下
package ;
import edInputStream;
import edOutputStream;
import edReader;
import ;
import putStream;
import tputStream;
import ption;
import tream;
import treamReader;
import Stream;
import riter;
import oder;
import ;
import Chain;
import Config;
import tException;
import tRequest;
import tResponse;
import rvletRequest;
import rvletResponse;
public class DownLoadFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request1, ServletResponse response1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) request1;
HttpServletResponse response = (HttpServletResponse) response1;
//racterEncoding("UTF-8");
//String filePath = new String(ameter("filePath").getBytes("ISO8859-1"),
"UTF-8");
racterEncoding("ISO8859-1");
String filePath = new
String(ameter("filePath").getBytes("ISO8859-1"),"UTF-8");
n("*****************************************************filePat
h is :" + filePath);
String suffix = ing(dexOf(".") + 1);
n("********************suffix is:"+suffix);
String contentType = getContentType(suffix);//获得尾缀设置不同contentType
String contentDisposition = "attachment; filename="+new
String(ing(dexOf("_")+1));
n("********************************************"+contentDisposit
ion);
racterEncoding("ISO8859-1");
tentType(contentType);
der("Content-disposition", new
String(es("UTF-8"),"ISO8859-1"));
try {
InputStream is = new FileInputStream(filePath);
OutputStream os = putStream();
int byteRead;
byte[] buffer = new byte[1024];
while ((byteRead = (buffer)) != -1) {
(buffer, 0, byteRead);
}
();
();
} catch (Exception e) {
tackTrace();
}
}
/**
*
* @param suffix 下载文件尾缀
* @return 返回不同tType
*/
public String getContentType(String suffix) {
if (("txt")) {
return "text/plain";
} else if(("doc") || ("docx")) {
return "application/msword;charset=gb2312";
} else if(("xls") || ("xlsx")) {
return "application/-excel";
} else if(("pdf")) {
return "application/pdf";
}else if(("gif")){
return "image/gif" ;
}else if(("jpg")){
return "image/jpeg" ;
}else if(("htm")||("html")||("jsp")){
return "text/html" ;
}else if(("xml")){
return "text/xml" ;
}else if(("js")){
return "application/x-javascript" ;
}
return "application/octet-stream";
}
public void init(FilterConfig config) throws ServletException {
}
}
注:其中编码ISO8859-1不能乱改,否则出现解析路径错误或者下载文件出错。该编码利用
的是tomcat和resin服务器解析时实际所用编码。祝各位好运。花了我半天时间才搞定乱码
现象。
版权声明:本文标题:jsp实现下载excel,word,pdf,jgp,gif,xml,js过滤器实现文档 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1721892908a903495.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论