栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

文件下载实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

文件下载实例

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());
}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号