package cn.ksource.core.util;
|
|
import java.io.BufferedInputStream;
|
import java.io.BufferedOutputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.io.UnsupportedEncodingException;
|
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletResponse;
|
|
public class DownloadUtil {
|
|
/**
|
* 下载文件
|
* @param response
|
* @param file
|
* @param fileName
|
* 作者:杨凯
|
*/
|
public static void download(HttpServletResponse response,File file,String fileName,Boolean deleteFile) throws IOException{
|
response.setCharacterEncoding("gbk");
|
// 以流的形式下载文件。
|
InputStream fis = new BufferedInputStream(new FileInputStream(file));
|
byte[] buffer = new byte[fis.available()];
|
fis.read(buffer);
|
fis.close();
|
// 清空response
|
response.reset();
|
// 设置response的Header
|
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "ISO8859-1"));
|
response.addHeader("Content-Length", "" + file.length());
|
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
|
|
String name = file.getName();
|
System.out.println(name );
|
String suffix = name.substring(name.lastIndexOf(".")+1, name.length()).toLowerCase();
|
if ("doc".equals(suffix)) {
|
response.setContentType("application/msword");
|
}else if ("xls".equals(suffix)) {
|
response.setContentType("application/msexcel");
|
}else if ("pdf".equals(suffix)) {
|
response.setContentType("application/pdf");
|
}else if ("txt".equals(suffix)) {
|
response.setContentType("application/txt");
|
}else{
|
response.setContentType("application/octet-stream");
|
}
|
|
toClient.write(buffer);
|
toClient.flush();
|
toClient.close();
|
response.flushBuffer();
|
if (deleteFile) {
|
file.delete();
|
}
|
}
|
|
/**
|
* 下载文件
|
* @param response
|
* @param file
|
* @param fileName
|
* 作者:杨凯
|
*/
|
public static void download(HttpServletResponse response,InputStream fis,String fileName) throws IOException{
|
response.setCharacterEncoding("gbk");
|
response.addHeader("Content-Length", "" + fis.available());
|
// 以流的形式下载文件。
|
byte[] buffer = new byte[fis.available()];
|
fis.read(buffer);
|
fis.close();
|
// 清空response
|
response.reset();
|
// 设置response的Header
|
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), "ISO8859-1"));
|
|
|
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
|
|
String suffix = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()).toLowerCase();
|
if ("doc".equals(suffix)) {
|
response.setContentType("application/msword");
|
}else if ("xls".equals(suffix)) {
|
response.setContentType("application/msexcel");
|
}else if ("pdf".equals(suffix)) {
|
response.setContentType("application/pdf");
|
}else if ("txt".equals(suffix)) {
|
response.setContentType("application/txt");
|
}else{
|
response.setContentType("application/octet-stream");
|
}
|
|
toClient.write(buffer);
|
toClient.flush();
|
toClient.close();
|
response.flushBuffer();
|
}
|
|
|
public static String toClientForReport(HttpServletResponse response,ByteArrayOutputStream baos,String name) throws UnsupportedEncodingException{
|
response.setHeader("Content-Type", "application/vnd.ms-excel");
|
response.setHeader("Content-Disposition", "inline; filename=\""+new String(name.getBytes("GB2312"),"ISO8859-1")+".xls\"");
|
response.setCharacterEncoding("UTF-8");
|
try {
|
ServletOutputStream out = response.getOutputStream();
|
baos.writeTo(out);
|
baos.flush();
|
baos.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
}
|