从tomcat下载文件

发布于 2019-07-26  532 次阅读


其实最开始的需求,就是希望使用tomcat下载服务器上的一些文件。
当然如果文件就放在{tomcat_home}/webapp/myproject/下那一切都好办。
但是把自己放在这里,每次eclipse那边一启动,东西就没了。不合适。

方案一
从网上查到的资料,是这样的。
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>

要将listings的false改为true。
然后重启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" />

上面的效果其实还不错,但是也有问题。
如果我不想让用户看所有的问题,只想让用户看某些文件,怎么办。

方案二
设置Tomcat的虚拟目录
{tomcat_home}/conf/serer.xml中的server/service/engine/host下
增加

<Context docBase="D://myfiles" path="/showfile/show" reloadable="true" />

之后我访问
localhost:8080/showfile/show/a.txt
就能看的D://myfiles/a.txt的内容了。
而且我不给你地址,你就看不到文件。安全多了。
但是还有一个问题,不能下载。

我不想在浏览器里看到txt文档,我想直接下载txt,可以么?

可以。

其实还有几个问题在方案一里面,最重要的参数是xml的文件名,即使给context里面加上path也不顶用
在方案二里面,server.xml里面起作用的却是contxt里面的psth 有点奇怪哦

方案三
使用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);
} 

}

参考文章
http://blog.csdn.net/yuan882696yan/article/details/26680253
http://www.cnblogs.com/gongchenglion/archive/2016/09/06/5846818.html
---------------------
原文:https://blog.csdn.net/dlf123321/article/details/69927251