package com.upload.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = -6748091423171929746L;
static final private String CONTENT_TYPE = "text/html; charset=UTF-8";
// Initialize global variables
public void init() throws ServletException {
}
// Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
// Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
String downloadfile = new String(
(request.getParameter("downFile")).getBytes("iso8859-1"),
"UTF-8");
String downloadfileName = request.getParameter("downName");
ServletContext context = getServletContext();
String downloadpath = context.getRealPath(File.separator);
// 构建下载文件
java.io.File file = new java.io.File(downloadpath + downloadfile);
if (!file.exists()) {
response.sendRedirect("no_file.jsp");
} else {
long filesize = file.length();
// 设置输出格式
String fileType = "." + getFiletype(downloadfile);
String type = "";
if (downloadfileName.lastIndexOf(".") != -1) {
type = downloadfileName.substring(
downloadfileName.lastIndexOf("."),
downloadfileName.length());
}
response.addHeader("content-type", "application/x-msdownload;");
if (type != null && fileType.equals(type)) {
response.addHeader(
"Content-Disposition",
"attachment; filename="
+ new String(downloadfileName
.getBytes("utf-8"), "iso8859-1"));
} else {
response.addHeader(
"Content-Disposition",
"attachment; filename="
+ new String((downloadfileName + fileType)
.getBytes("utf-8"), "iso8859-1"));
}
response.addHeader("content-length", Long.toString(filesize));
// 向客户端写入文件
FileInputStream fin = new FileInputStream(file);
byte[] b = new byte[1];
int j = 0;
while ((j = fin.read(b)) > 0) {
response.getOutputStream().write(b, 0, j);
}
fin.close();
}
}
// Clean up resources
public void destroy() {
}
// 取文件名后缀
private static String getFiletype(String fileName) {
String type = "";
if (fileName == null || fileName.equals(""))
return type;
int position = fileName.lastIndexOf(".");
if (position != -1) {
type = fileName.substring(position + 1, fileName.length());
}
return type;
}
}
在jsp页面中调用 :
下载的脚本
function downLoad(path,downName)
{
document.getElementById("downFile").value=path;
document.getElementById("downName").value=downName;
document.downLoadFrm.submit();
}
<下面是一个form表单>
<form action="<%=downloadServlet%>" id="downLoadFrm" name="downLoadFrm" method="get" target="downIframe" style="margin: 0px;padding: 0px;height: 0px;width: 0px;">
<input type="hidden" id="downFile" name="downFile" />
<input type="hidden" id="downName" name="downName" />
</form>