60 lines
2.0 KiB
Java
60 lines
2.0 KiB
Java
package com.zcloud.util;
|
||
|
||
import java.io.BufferedOutputStream;
|
||
import java.io.OutputStream;
|
||
import java.net.URLEncoder;
|
||
|
||
import javax.servlet.http.HttpServletResponse;
|
||
|
||
/**
|
||
* 说明:下载文件
|
||
* 作者:luoxiaobao
|
||
* 官网:www.qdkjchina.com
|
||
*/
|
||
public class FileDownload {
|
||
|
||
/**
|
||
* @param response
|
||
* @param filePath //文件完整路径(包括文件名和扩展名)
|
||
* @param fileName //下载后看到的文件名
|
||
* @return 文件名
|
||
*/
|
||
public static void fileDownload(final HttpServletResponse response, String filePath, String fileName) throws Exception{
|
||
byte[] data = FileUtil.toByteArray2(filePath);
|
||
if(Tools.isEmpty(fileName)){
|
||
fileName = "导出模板.xls";
|
||
}
|
||
fileName = URLEncoder.encode(fileName, "UTF-8");
|
||
response.reset();
|
||
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||
response.addHeader("Content-Length", "" + data.length);
|
||
response.setContentType("application/octet-stream;charset=UTF-8");
|
||
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
|
||
outputStream.write(data);
|
||
outputStream.flush();
|
||
outputStream.close();
|
||
response.flushBuffer();
|
||
}
|
||
|
||
/** 体系文件使用
|
||
* @param response
|
||
* @param filePath //文件完整路径(包括文件名和扩展名)
|
||
* @param fileName //下载后看到的文件名
|
||
* @return 文件名
|
||
*/
|
||
public static void mfFileDownload(final HttpServletResponse response, String filePath, String fileName) throws Exception{
|
||
byte[] data = FileUtil.toByteArray4(filePath);
|
||
fileName = URLEncoder.encode(fileName, "UTF-8");
|
||
response.reset();
|
||
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||
response.addHeader("Content-Length", "" + data.length);
|
||
response.setContentType("application/octet-stream;charset=UTF-8");
|
||
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
|
||
outputStream.write(data);
|
||
outputStream.flush();
|
||
outputStream.close();
|
||
response.flushBuffer();
|
||
}
|
||
|
||
}
|