当前位置:   article > 正文

JAVA下载 PDF 到本地 或 返回文件流

JAVA下载 PDF 到本地 或 返回文件流

 

  1. @Slf4j
  2. public class PDFUtils {
  3. /**
  4. *
  5. * @param fileUrl 文件路径
  6. * @param saveUrl 文件保存路径
  7. * @param fileName 文件名称
  8. * @throws IOException
  9. */
  10. public static void downloadPdf(String fileUrl, String saveUrl, String fileName) throws IOException {
  11. URL url = new URL(fileUrl);
  12. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  13. //设置超时间为3秒
  14. conn.setConnectTimeout(5*1000);
  15. //防止屏蔽程序抓取而返回403错误
  16. conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  17. //得到输入流
  18. InputStream inputStream = conn.getInputStream();
  19. //获取自己数组
  20. byte[] getData = readInputStream(inputStream);
  21. //文件保存位置
  22. File saveDir = new File(saveUrl);
  23. if(!saveDir.exists()){
  24. saveDir.mkdir();
  25. }
  26. File file = new File(saveDir+File.separator+fileName);
  27. FileOutputStream fos = new FileOutputStream(file);
  28. fos.write(getData);
  29. if(fos!=null){
  30. fos.close();
  31. }
  32. if(inputStream!=null){
  33. inputStream.close();
  34. }
  35. System.out.println("info:"+url+" download success");
  36. }
  37. /**
  38. * 从输入流中获取字节数组
  39. * @param inputStream
  40. * @return
  41. * @throws IOException
  42. */
  43. public static byte[] readInputStream(InputStream inputStream) throws IOException {
  44. byte[] buffer = new byte[1024];
  45. int len = 0;
  46. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  47. while((len = inputStream.read(buffer)) != -1) {
  48. bos.write(buffer, 0, len);
  49. }
  50. bos.close();
  51. return bos.toByteArray();
  52. }
  53. /**
  54. * 下载pdf返回文件流
  55. * @param response 请求头
  56. * @param pdfName fileName
  57. * @param path 路径
  58. */
  59. public static void toDownload(HttpServletResponse response, String pdfName,String path) {
  60. ServletOutputStream out = null;
  61. InputStream inputStream = null;
  62. try {
  63. // 获取外部文件流
  64. log.info("下载中------invPdfUrl=" +path);
  65. URL url = new URL(path);
  66. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  67. conn.setConnectTimeout(3 * 1000);
  68. //防止屏蔽程序抓取而返回403错误
  69. conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  70. inputStream = conn.getInputStream();
  71. /**
  72. * 输出文件到浏览器
  73. */
  74. int len = 0;
  75. // 输出 下载的响应头,如果下载的文件是中文名,文件名需要经过url编码
  76. response.setContentType("text/html;charset=utf-8");
  77. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(pdfName, "UTF-8"));
  78. response.setHeader("Cache-Control", "no-cache");
  79. out = response.getOutputStream();
  80. byte[] buffer = new byte[1024];
  81. while ((len = inputStream.read(buffer)) > 0) {
  82. out.write(buffer, 0, len);
  83. }
  84. out.flush();
  85. log.info("pdf文件下载完成.....");
  86. } catch (Exception e) {
  87. log.error("pdf文件下载异常,e = {}", e);
  88. e.printStackTrace();
  89. } finally {
  90. if (inputStream != null) {
  91. try {
  92. inputStream.close();
  93. } catch (Exception e) {
  94. }
  95. }
  96. if (out != null) {
  97. try {
  98. out.close();
  99. } catch (Exception e) {
  100. }
  101. }
  102. }
  103. }
  104. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/207139
推荐阅读
相关标签
  

闽ICP备14008679号