Created
January 18, 2012 08:00
-
-
Save yongboy/1631875 to your computer and use it in GitHub Desktop.
DownloadFileAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.servlet3.demo; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.Writer; | |
import javax.servlet.ServletException; | |
import javax.servlet.ServletOutputStream; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.apache.commons.io.FilenameUtils; | |
import org.apache.log4j.Logger; | |
/** | |
* 文件下载演示 | |
* | |
* @author yongboy | |
* @time 2012-1-18 | |
* @version 1.0 | |
*/ | |
@WebServlet("/down") | |
public class DownloadFileAction extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
private final Logger log = Logger.getLogger(DownloadFileAction.class); | |
protected void doGet(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
// 传递路径用于下载 | |
String path = request.getParameter("path"); | |
long start = System.currentTimeMillis(); | |
downloadFile(path, null, response); | |
log.info("use time : " + (System.currentTimeMillis() - start)); | |
} | |
/** | |
* 提供一个下载的公共函数 | |
* | |
* @param filePath | |
* 文件的访问路径 | |
* @param fileName | |
* 要显示的文件名称 | |
* @param response | |
*/ | |
private static void downloadFile(String filePath, String fileName, | |
HttpServletResponse response) { | |
if (filePath == null || filePath.trim().equals("") || response == null) { | |
output(response, "请求参数有误!"); | |
return; | |
} | |
File file = new File(filePath); | |
if (!file.exists() || file.isDirectory()) { | |
output(response, "您请求的文件不存在或者路径有误!"); | |
return; | |
} | |
if (fileName == null || fileName.trim().equals("")) { | |
fileName = FilenameUtils.getName(filePath); | |
} | |
try { | |
// 对下载的文件名称进行编码,避免出现中文乱码问题 | |
response.setHeader("Content-disposition", "attachment; filename=" | |
+ new String(fileName.getBytes("GBK"), "ISO-8859-1")); | |
InputStream is = new FileInputStream(file); | |
int len = -1; | |
ServletOutputStream out = response.getOutputStream(); | |
while ((len = is.read()) != -1) { | |
out.write(len); | |
} | |
is.close(); | |
out.flush(); | |
out.close(); | |
} catch (FileNotFoundException notFound) { | |
output(response, "您所请求的文件不存在!"); | |
} catch (IOException ioe) { | |
ioe.printStackTrace(); | |
output(response, "您所请求的文件出现异常!"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
output(response, "您所请求的文件出现异常!"); | |
} | |
} | |
private static void output(HttpServletResponse response, String message) { | |
try { | |
response.setContentType("text/html; charset=UTF-8"); | |
Writer out = response.getWriter(); | |
out.write(message); | |
out.flush(); | |
out.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment