com.lone.common.util.FileViewOrDownload Maven / Gradle / Ivy
The newest version!
package com.lone.common.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
/**
* 下载文件
*
* @version
*/
public class FileViewOrDownload {
/**
* 文件下载或预览
*
* @param request
* @throws Exception
* @throws Exception
*/
public static HttpServletResponse viewOrDownloadFile(HttpServletResponse response, String filePath, String doFg) {
response.setContentType("UTF-8");
response.setCharacterEncoding("UTF-8");
InputStream bis = null;
BufferedOutputStream bos = null;
long fileLength = 0;
if (StringUtil.isNotEmpty(filePath)) {
fileLength = new File(filePath).length();
try {
bis = new BufferedInputStream(new FileInputStream(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
try {
String extend = FileUtils.getExtend(filePath);
String fileName = FileUtils.getFileName(filePath);
if (extend.equals("text")) {
response.setContentType("text/plain;");
} else if (extend.equals("doc")) {
response.setContentType("application/msword;");
} else if (extend.equals("xls")) {
response.setContentType("application/ms-excel;");
} else if (extend.equals("pdf")) {
response.setContentType("application/pdf;");
} else if (extend.equals("jpg") || extend.equals("jpeg")) {
response.setContentType("image/jpeg;");
} else {
response.setContentType("application/x-msdownload;");
}
if ("view".equals(doFg)) {
response.setHeader("Content-disposition",
"inline; filename=" + new String((fileName + "." + extend).getBytes("GBK"), "ISO8859-1"));
} else {
response.setHeader("Content-disposition",
"attachment; filename=" + new String((fileName + "." + extend).getBytes("GBK"), "ISO8859-1"));
}
response.setHeader("Content-Length", String.valueOf(fileLength));
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 (IOException e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
/**
* @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);
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();
}
public static void fileDownloadBoot(final HttpServletResponse response, String filePath, String fileName)
throws Exception {
byte[] data = FileUtil.toByteArray2boot(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();
}
}