转载—从tomcat中下载文件
1、在tomcat 安装目录\conf\Catalina\localhost下建立任意文件名xml文件,比如:download_file.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <Context reloadable="true" docBase="D://myfiles" crossContext="true"> </Context>
2、配置web.xml(tomcat的配置文件):
<servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
改为:
<servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
然后重启tomcat,访问localhost:8080/download_file。
访问的工程路径就是刚才新建的那个xml文件的名字。
这样直接在图上点击文件名,或者对着浏览器输入文件的地址就能看到文件内容。连接另存为就能下载。如果文件名包含中文,在{tomcat_home}/conf/server.xml中加上URIEncoding="UTF-8,如下:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
方案二
1.设置Tomcat的虚拟目录
{tomcat_home}/conf/serer.xml中的server/service/engine/host下增加标签:
<Context docBase="D://myfiles" path="/demo" reloadable="true" />
2、配置web.xml(tomcat的配置文件):
<servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
改为:
<servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
访问:localhost:8080/demo
方案三:
使用servlet
/** * This class is used for ... * @author dlf(460795365@qq.com) * @version 1.0, 2017年4月8日 下午11:53:35 */ @WebServlet(urlPatterns="/module/book/downtest") public class DownloadFile extends HttpServlet{ /** * */ private static final long serialVersionUID = -8663217572193783988L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //获得请求文件名 String filenameISO = (String) request.getParameter("filename"); String filenameUTF= new String(filenameISO.getBytes("ISO-8859-1"), "UTF-8"); System.out.println(filenameUTF); //设置文件MIME类型 response.setContentType(getServletContext().getMimeType(filenameUTF)); //设置Content-Disposition //filename是ISO-8859-1的 如果改成utf-8 下载的文件名中就不能出现中文了 //大家可以试试 response.setHeader("Content-Disposition", "attachment;filename="+filenameISO); response.setHeader("Content-type", "charset=UTF-8"); response.setCharacterEncoding("UTF-8"); //读取目标文件,通过response将目标文件写到客户端 //System.out.println(fullFileName); //读取文件 InputStream in = new FileInputStream(Config.FAILED_FILE_PATH+filenameUTF); OutputStream out = response.getOutputStream(); //写文件 int b; while((b=in.read())!= -1) { out.write(b); } in.close(); out.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
本文来自博客园,作者:King-DA,转载请注明原文链接:https://www.cnblogs.com/qingmuchuanqi48/p/13721467.html