文件下载工具类-FileDownloadUtil
作者:互联网
==============================================文件下载工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.net.URL; import java.net.URLEncoder; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class FileDownloadUtil { private static final Logger logger = LoggerFactory.getLogger(FileDownloadUtil.class); /** * 普通文件下载(流的方式) * @param targetFile 预下载的文件路径(服务器路径) * @param response */ public static void downloadLocal(String targetFile, HttpServletResponse response) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try{ logger.info("【普通文件下载】开始下载文件, targetFile={}", targetFile); //设置文件下载时,文件流的格式 bis = new BufferedInputStream(new FileInputStream(targetFile)); bos = new BufferedOutputStream(response.getOutputStream()); //下面这个变量保存的是要下载的文件拼接之后的完整路径 String downName = FilePathUtil.extractFileName(targetFile); response.reset();//先清空response 非常重要 response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(downName,"utf-8")); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } logger.info("【普通文件下载】结束下载文件, 下载后的文件filename={}", downName); }catch(Exception e){ e.printStackTrace(); }finally { try { bis.close(); bos.close(); }catch (Exception e){ e.printStackTrace();; } } } /** * 普通文件下载(支持在线打开) * @param targetFile 预下载的文件路径(服务器路径) * @param response */ public static void downloadLocal(String targetFile, HttpServletResponse response, boolean isOnline) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try{ logger.info("【普通文件下载】开始下载文件, targetFile={}", targetFile); //设置文件下载时,文件流的格式 bis = new BufferedInputStream(new FileInputStream(targetFile)); bos = new BufferedOutputStream(response.getOutputStream()); //下面这个变量保存的是要下载的文件拼接之后的完整路径 String downName = FilePathUtil.extractFileName(targetFile); response.reset();//先清空response 非常重要 if(isOnline) { URL url = new URL("file:///" + targetFile); response.setContentType(url.openConnection().getContentType()); response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(downName,"utf-8")); }else { response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(downName,"utf-8")); } byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } logger.info("【普通文件下载】结束下载文件, 下载后的文件filename={}", downName); }catch(Exception e){ e.printStackTrace(); }finally { try { bis.close(); bos.close(); }catch (Exception e){ e.printStackTrace();; } } } /** * 网络文件下载到服务器 * @param urlFile 网络文件路径 reg windine.blogdriver.com/logo.gif * @param targetFile 服务器文件路径 E:/home/img/img.zip * @param response */ public static void downloadUrl(String urlFile, String targetFile, HttpServletResponse response) { ReadableByteChannel rbc = null; FileOutputStream fos = null; try{ logger.info("【网络文件下载】开始下载文件, urlFile={} targetFile={}", urlFile, targetFile); URL website = new URL(urlFile); rbc = Channels.newChannel(website.openStream()); fos = new FileOutputStream(targetFile); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); logger.info("【普通文件下载】结束下载文件, 下载后的文件targetFile={}", targetFile); }catch(Exception e){ e.printStackTrace(); }finally { try { rbc.close(); fos.close(); }catch (Exception e){ e.printStackTrace();; } } } }
==============================================文件下载工具测试类
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.taoxw.plugins.file.FileDownloadUtil; @RequestMapping("/taoxw/file") @Controller public class FileController { /** * 网络资源下载 * @param request * @param response */ @RequestMapping(value = "downloadFile03", method = RequestMethod.GET) @ResponseBody public void downloadUrl(HttpServletRequest request, HttpServletResponse response) { String urlFile = "https://i1.hdslb.com/bfs/face/08b322c8ac249d26319557af4955a56ac6a4b397.jpg@140w_140h_1c.webp"; String targetFile = "E:\\home\\commUtils\\temp\\140w_140h_1c.webp"; FileDownloadUtil.downloadUrl(urlFile, targetFile, response); } /** * 普通下载(支持在线打开) * @param request * @param response */ @RequestMapping(value = "downloadFile02", method = RequestMethod.GET) @ResponseBody public void downloadFile(HttpServletRequest request, HttpServletResponse response) { String targetFile = "E:\\home\\commUtils\\temp\\Person03.txt"; FileDownloadUtil.downloadLocal(targetFile, response, true); } /** * 普通下载 * @param request * @param response */ @RequestMapping(value = "downloadFile01", method = RequestMethod.GET) @ResponseBody public void downloadFile(HttpServletResponse response) { String targetFile = "E:\\home\\commUtils\\temp\\Person03.txt"; FileDownloadUtil.downloadLocal(targetFile, response); } }
通过浏览器GET请求方式下载
http://127.0.0.1:8090/taoxw/file/downloadFile01 ---下载服务器文件到本地
http://127.0.0.1:8090/taoxw/file/downloadFile02 ---下载服务器文件到本地,且支持在线打开
http://127.0.0.1:8090/taoxw/file/downloadFile02 ---下载网络资源文件到服务器,资源文件后缀保持一致,否则打不开
标签:下载工具,文件,param,targetFile,FileDownloadUtil,import,response,下载 来源: https://www.cnblogs.com/taoxw/p/15929260.html