import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.linkOption;
import java.nio.file.Paths;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tika.Tika;
@Slf4j
public class FileDownload {
public static void download(String fileFullPath, String fileOriginalName,
HttpServletRequest request, HttpServletResponse response) throws Exception {
FileDownload.download(fileFullPath, fileOriginalName, request, response, false);
}
public static void download(String fileFullPath, String fileOriginalName,
HttpServletRequest request, HttpServletResponse response, boolean isOnline) throws Exception {
if (StringUtils.isBlank(fileOriginalName)) {
throw new Exception(“文件名不能为空”);
}
if (StringUtils.isBlank(fileFullPath)) {
throw new Exception(“文件路径不能为空”);
}
if (response.isCommitted()) {
throw new Exception(“HTTP响应已经提交”);
}
if (!Files.exists(Paths.get(fileFullPath), linkOption.NOFOLLOW_linkS)) {
throw new Exception(“文件不存在”);
}
try (InputStream inStream = new FileInputStream(fileFullPath)) {
FileDownload.buildResponse(fileFullPath, fileOriginalName, request, response, isOnline);
byte[] b = new byte[100];
int len;
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
private static void buildResponse(String fileFullPath, String fileOriginalName,
HttpServletRequest request,
HttpServletResponse response, boolean isOnline) throws Exception {
try {
String browser = request.getHeader(“User-Agent”);
if (StringUtils.isBlank(browser)) {
throw new Exception(“请求头中未发现User-Agent属性”);
}
//response.setHeader("Accept-Ranges", "bytes");
//response.addHeader("Content-Length", String.valueOf(size));
String filename;
if (browser.contains("MSIE 6.0") || browser.contains("MSIE 7.0") || browser
.contains("Safari")) //IE6,IE7,苹果
{
filename = "filename=" + new String(fileOriginalName.getBytes(), "ISO8859-1");
} else if (browser.contains("MSIE 8.0") || browser.contains("MSIE 9.0")) //IE8,IE9
{
filename = "filename=" + URLEncoder.encode(fileOriginalName, "UTF-8");
} else //火狐,谷歌或者其他的浏览器
{
filename = "filename*=UTF-8''" + URLEncoder.encode(fileOriginalName, "UTF-8");
}
response.reset();
if (isOnline) { //在线打开
Tika tika = new Tika();
response.setContentType(tika.detect(Paths.get(fileFullPath)));
response.setHeader("Content-Disposition", "inline;" + filename);
} else { //下载
//设置强制下载不打开文件
//response.setContentType("application/x-msdownload");//对应 .dll .exe .mnd类型的文件
//response.setContentType("application/force-download");//未找到这种ContentType
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;" + filename);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new Exception(e.getMessage());
}
}
public static void main(String[] args)throws Exception {
String filePath=“E:moduledoc刘浩宇照片搞了个假后缀名.txt”;
Tika tika = new Tika();
URL u = new URL(“file:///” + filePath);
log.info(“aaaaaaaaaaa={},{},{}”,tika.detect(filePath),tika.detect(Paths.get(filePath)),u.openConnection().getContentType());
}
}



