public class DownloadUtils {
private static Logger logger = LoggerFactory.getLogger(com.hikvision.idatafusion.util.DownloadUtils.class);
public DownloadUtils() {
}
/**
* 从服务器下载文件
* @param fileName
* @param filepath
* @param request
* @param response
* @throws Exception
*/
public static void download(String fileName, String filepath, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//String ctxPath = request.getSession().getServletContext().getRealPath("/") + filepath;
try {
File file = new File(filepath);
if (!file.exists()) {
logger.warn("download agent is error ! messages --->> " + filepath + " is not exists !");
}
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(file.length()));
bis = new BufferedInputStream(new FileInputStream(filepath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception var13) {
logger.warn("download agent is error ! messages --->> " + var13.fillInStackTrace());
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}
}
/**
* 下载二进制文件
* @param fileName 文件名
* @param bytes 文件的二进制字节数组
* @param request
* @param response
* @throws Exception
*/
public static void downloadFromBytes(String fileName,byte[] bytes, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("application/octet-stream;charset=ISO8859-1");
request.setCharacterEncoding("UTF-8");
BufferedOutputStream bos = null;
try {
response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(), "ISO8859-1"));
bos = new BufferedOutputStream(response.getOutputStream());
bos.write(bytes);
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
bos.close();
}
}
}
/**
* 从类路径下下载文件
* @param response 返回
* @param filePath 文件所在类路径
* @param fileName 下载的文件名
*/
public static void downLoadFromClasspath(HttpServletResponse response, String filePath, String fileName) {
InputStream inputStream = null;
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource=resourceLoader.getResource(filePath);
logger.info(resource.toString());
BufferedInputStream bis=null;
try {
inputStream=resource.getInputStream();
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
response.addHeader(HttpHeaders.CONTENT_TYPE,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
BufferedOutputStream bos = new BufferedOutputStream(
response.getOutputStream());
bis = new BufferedInputStream(inputStream);
byte[] b=new byte[1024];
int i = bis.read(b);
while (i != -1) {
bos.write(b, 0, b.length);
bos.flush();
i = bis.read(b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}