import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author yl
*
*/
public class FileDownloadUtil {
/**
* <p>
* 文件下载工具方法
* </p>
*
* @param fileName 文件名
* @param downloadPath 文件路径
* @param response
* @throws Exception
*/
public static void modelDownload(String fileName, String downloadPath,
HttpServletResponse response) throws Exception {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
long fileLength = new File(downloadPath).length();
if(fileLength>0){
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downloadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
}else{
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=文件不存在!");
response.setHeader("Content-Length", String.valueOf(fileLength));
System.out.println("文件不存在!"+downloadPath);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
}
}
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author yl
*
*/
public class FileDownloadUtil {
/**
* <p>
* 文件下载工具方法
* </p>
*
* @param fileName 文件名
* @param downloadPath 文件路径
* @param response
* @throws Exception
*/
public static void modelDownload(String fileName, String downloadPath,
HttpServletResponse response) throws Exception {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
long fileLength = new File(downloadPath).length();
if(fileLength>0){
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downloadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[1024];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
}else{
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename=文件不存在!");
response.setHeader("Content-Length", String.valueOf(fileLength));
System.out.println("文件不存在!"+downloadPath);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
}
}