jsp实现下载excel,word,pdf,jgp,gif,xml,js过滤器实现文档.doc_第1页
jsp实现下载excel,word,pdf,jgp,gif,xml,js过滤器实现文档.doc_第2页
jsp实现下载excel,word,pdf,jgp,gif,xml,js过滤器实现文档.doc_第3页
jsp实现下载excel,word,pdf,jgp,gif,xml,js过滤器实现文档.doc_第4页
jsp实现下载excel,word,pdf,jgp,gif,xml,js过滤器实现文档.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

下面代码有选择的粘贴进自己项目即可,其实现解决了tomcat、resin服务器中文下载乱码问题。web.xml配置如下index.jsp downLoad com.tangdi.DownLoadFilter downLoad *.downLoadJsp页面配置如下其下载利用a标签进行连接,前提是服务器上下载文件路径已知需要引进 el表达式和jquery. $(function() var $href=$pageContext.request.contextPath+/20159.downLoad?filePath=;/+encodeURIComponent(中国.downLoad); $(#testHref).children(td).each(function() var $id=$(this).children(:first).attr(id); var $tmpHref=; if($id=txt) $tmpHref=$href+E:/apache-tomcat-6.0.35/apache-tomcat-6.0.35/webapps/MyTry/test/+2031234_这是txt.txt; else if($id=pdf) $tmpHref=$href+E:/apache-tomcat-6.0.35/apache-tomcat-6.0.35/webapps/MyTry/test/+这是pdf.pdf; else if($id=excel) $tmpHref=$href+这是excel.xls; else if($id=word) $tmpHref=encodeURI($href+这是doc.doc,ISO8859-1); else if($id=jpg) $tmpHref=$href+这是jpg.jpg; else if($id=js) $tmpHref=$href+这是js.js; else if($id=jsp) $tmpHref=$href+这是jsp.jsp; else if($id=html) $tmpHref=$href+这是html.html; $(this).children(:first).attr(href,$tmpHref); ); ); txt pdf excel word xml jpg gif js jsp html 过滤器filter配置如下package com.tangdi;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import .URLDecoder;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;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;/request.setCharacterEncoding(UTF-8);/String filePath = new String(request.getParameter(filePath).getBytes(ISO8859-1), UTF-8);request.setCharacterEncoding(ISO8859-1);String filePath = new String(request.getParameter(filePath).getBytes(ISO8859-1),UTF-8);System.out.println(*filePath is : + filePath);String suffix = filePath.substring(filePath.lastIndexOf(.) + 1);System.out.println(*suffix is:+suffix);String contentType = getContentType(suffix);/获得尾缀设置不同contentTypeString contentDisposition = attachment; filename=+new String(filePath.substring(filePath.lastIndexOf(_)+1);System.out.println(*+contentDisposition);response.setCharacterEncoding(ISO8859-1);response.setContentType(contentType);response.setHeader(Content-disposition, new String(contentDisposition.getBytes(UTF-8),ISO8859-1);try InputStream is = new FileInputStream(filePath);OutputStream os = response.getOutputStream();int byteRead;byte buffer = new byte1024;while (byteRead = is.read(buffer) != -1) os.write(buffer, 0, byteRead);os.flush();os.close(); catch (Exception e) e.printStackTrace();/* * * param suffix 下载文件尾缀 * return 返回不同response.contentType */public String getContentType(String suffix) if (suffix.equals(txt) return text/plain; else if(suffix.equals(doc) | suffix.equals(docx) return application/msword;charset=gb2312; else if(suffix.equals(xls) | suffix.equals(xlsx) return application/-excel; else if(suffix.equals(pdf) return application/pdf;else if(suffix.equals(gif)return image/gif ;else if(suffix.equals(jpg)return image/jpeg ;else if(suffix.equals(htm)|suffix.equals(html)|suffix.equals(jsp)return text/html ;else if(suffix.equals(xml)return text/xml ;else if(suffix.equals(js)return application/x-javas

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论