java实现文件下载的工具类-----------什么都可以下载,只要传入response和文件路径 两个参数
2013-09-11 11:46
645 查看
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
public class DownLoadUtils {
public static String getContentType(String fileName) {
String fileNameTmp = fileName.toLowerCase();
String ret = "";
if (fileNameTmp.endsWith("txt")) {
ret = "text/plain";
}
if (fileNameTmp.endsWith("gif")) {
ret = "image/gif";
}
if (fileNameTmp.endsWith("jpg")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("jpeg")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("jpe")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("zip")) {
ret = "application/zip";
}
if (fileNameTmp.endsWith("rar")) {
ret = "application/rar";
}
if (fileNameTmp.endsWith("doc")) {
ret = "application/msword";
}
if (fileNameTmp.endsWith("ppt")) {
ret = "application/vnd.ms-powerpoint";
}
if (fileNameTmp.endsWith("xls")) {
ret = "application/vnd.ms-excel";
}
if (fileNameTmp.endsWith("html")) {
ret = "text/html";
}
if (fileNameTmp.endsWith("htm")) {
ret = "text/html";
}
if (fileNameTmp.endsWith("tif")) {
ret = "image/tiff";
}
if (fileNameTmp.endsWith("tiff")) {
ret = "image/tiff";
}
if (fileNameTmp.endsWith("pdf")) {
ret = "application/pdf";
}
return ret;
}
//下载部分
public static void fileDownLoad(HttpServletResponse response,String filePath) throws ServletException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
String fileName = filePath.substring(filePath.lastIndexOf("/")+ 1, filePath.length());
try {
response.reset();
response.setContentType(getContentType(fileName));
response.setHeader("Content-disposition", "attachment;filename="+ new String(fileName.getBytes("gb2312"), "ISO8859-1"));
fis = new FileInputStream(filePath);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端 }
bos.close();
bis.close();
fos.close();
fis.close();
}
} catch (IOException e) {
response.reset();
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (bos != null) {
bos.close();
}
if (fis != null) {
fis.close();
}
if (bis != null) {
bis.close();
}
} catch (IOException e) {
System.err.print(e);
}
}
}
}
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
public class DownLoadUtils {
public static String getContentType(String fileName) {
String fileNameTmp = fileName.toLowerCase();
String ret = "";
if (fileNameTmp.endsWith("txt")) {
ret = "text/plain";
}
if (fileNameTmp.endsWith("gif")) {
ret = "image/gif";
}
if (fileNameTmp.endsWith("jpg")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("jpeg")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("jpe")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("zip")) {
ret = "application/zip";
}
if (fileNameTmp.endsWith("rar")) {
ret = "application/rar";
}
if (fileNameTmp.endsWith("doc")) {
ret = "application/msword";
}
if (fileNameTmp.endsWith("ppt")) {
ret = "application/vnd.ms-powerpoint";
}
if (fileNameTmp.endsWith("xls")) {
ret = "application/vnd.ms-excel";
}
if (fileNameTmp.endsWith("html")) {
ret = "text/html";
}
if (fileNameTmp.endsWith("htm")) {
ret = "text/html";
}
if (fileNameTmp.endsWith("tif")) {
ret = "image/tiff";
}
if (fileNameTmp.endsWith("tiff")) {
ret = "image/tiff";
}
if (fileNameTmp.endsWith("pdf")) {
ret = "application/pdf";
}
return ret;
}
//下载部分
public static void fileDownLoad(HttpServletResponse response,String filePath) throws ServletException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
String fileName = filePath.substring(filePath.lastIndexOf("/")+ 1, filePath.length());
try {
response.reset();
response.setContentType(getContentType(fileName));
response.setHeader("Content-disposition", "attachment;filename="+ new String(fileName.getBytes("gb2312"), "ISO8859-1"));
fis = new FileInputStream(filePath);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端 }
bos.close();
bis.close();
fos.close();
fis.close();
}
} catch (IOException e) {
response.reset();
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
if (bos != null) {
bos.close();
}
if (fis != null) {
fis.close();
}
if (bis != null) {
bis.close();
}
} catch (IOException e) {
System.err.print(e);
}
}
}
}
相关文章推荐
- java文件下载通过response流实现文件存储在客户端指定路径
- Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件
- 【java】HttpClient实现HTTP文件通用下载工具类
- 工具类,下载文件,参数给个URL,返回一个Byte数组,byte数组当参数写入指定的路径中
- Java实现文件上传下载工具类
- 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this response
- 天易42----java实现文件下载工具类
- Java实现Zip压缩文件并可以下载
- java的小说展示,下载在查看所有(文件路径找不到,可以修改)
- Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件
- Java实现的文件上传下载工具类完整实例【上传文件自动命名】
- javaweb之response实现文件下载(文件名是中文名需要经过url编码)
- JAVA实现带表单参数的文件上传、下载和文件打包下载
- Java中实现FTP文件上传下载 涉及中文路径以及中文文件
- java实现ftp文件上传下载
- Java代码使用FTPClient实现ftp文件上传下载和删除的功能
- VC获取从文件路径中返回文件名和扩展名两个参数
- java实现web端文件上传与下载
- java文件下载设置中文名称的实例(response.addHeader)
- Java中实现文件上传下载的三种解决方案