用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载,将一个完整的项目进行展示,主要有以下几个部分:
1、servlet部分 Export
2、工具类:TxtFileUtil
3、web.xml
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.util.TxtFileUtil;
/**
* 导出文件
* @author ouyangyu
* 如何下载tomcat的项目工程下面的文件
*下载地址:http://localhost:8080/ytxtest/ex.hts?filepath=ss.rar
*http://localhost:8080/ytxtest/ex.hts?filepath=fj.docx
*/
public class Export extends HttpServlet
{
private static final long serialVersionUID = 342590465839109906L;
private String contentType = "application/x-msdownload";
private String enc = "utf-8";
private String fileRoot = "";
/**
* 初使化方法
*/
public void init(ServletConfig config) throws ServletException
{
String tempStr = config.getInitParameter("contentType");
if (tempStr != null && !tempStr.equals(""))
{
contentType = tempStr;
}
tempStr = config.getInitParameter("enc");
if (tempStr != null && !tempStr.equals(""))
{
enc = tempStr;
}
tempStr = config.getInitParameter("fileRoot");
if (tempStr != null && !tempStr.equals(""))
{
fileRoot = tempStr;
}
}
/**
* doget请求
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//realUrl = /D:/ruanjian/tomcat/apache-tomcat-6.0.37/webapps/ytxtest/
String realUrl = new TxtFileUtil().getWebRoot();
//String path = realUrl+"/";
//path += StaticValue.FILE_UPLOAD_PATH;
//filepath= ss.rar
String filepath = request.getParameter("filepath");
String fullFilePath = realUrl + filepath;
//fullFilePath = /D:/ruanjian/tomcat/apache-tomcat-6.0.37/webapps/ytxtest/ss.rar
//file = D:\ruanjian\tomcat\apache-tomcat-6.0.37\webapps\ytxtest\ss.rar
File file = new File(fullFilePath);
if (file.exists())
{
//filename = ss.rar
String filename = URLEncoder.encode(file.getName(), enc);
response.reset();
response.setContentType(contentType);
response.addHeader("Content-Disposition", "attachment; filename=\""
+ filename + "\"");
//fileLength = 54325070
int fileLength = (int) file.length();
response.setContentLength(fileLength);
if (fileLength != 0)
{
InputStream inStream = new FileInputStream(file);
byte[] buf = new byte[4096];
ServletOutputStream servletOS = response.getOutputStream();
int readLength;
while (((readLength = inStream.read(buf)) != -1))
{
servletOS.write(buf, 0, readLength);
}
inStream.close();
servletOS.flush();
servletOS.close();
}
}
}
/**
* dopost请求
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
/**
* 读写文件的工具类
*
* @author Administrator
*/
public class TxtFileUtil
{
/**
* 获取路径
*
* @return
*/
public String getWebRoot()
{
String realUrl = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
String newUrl = "";
if(realUrl.contains("/WEB-INF/"))
{
newUrl = realUrl.substring(0, realUrl.lastIndexOf("WEB-INF/"));
}
realUrl = newUrl.replace("%20", " ");// 此路径不兼容jboss
return realUrl;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>导出文件</description>
<display-name>导出文件</display-name>
<servlet-name>Export</servlet-name>
<servlet-class>com.test.servlet.Export</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Export</servlet-name>
<url-pattern>/ex.hts</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>