zoukankan      html  css  js  c++  java
  • java 的在线下载文件 .pdf

     java  的在线下载文件  .pdf

    1.下载资源的本地位置

    
    

    2.设置响应头

    
    

    3.下载代码




    1
    PeriodicalResource periodicalResource = periodicalResourceService.get(id); 2 String filePath = periodicalResource.getAttachment();//获取资源位置 3 File file = new File(periodicalBaseDir + filePath);//本地资源位置 4 if (file.exists()) { 5 response.setContentType("application/force-download");// 设置强制下载不打开 6 response.addHeader("Content-Disposition", 7 "attachment;fileName=" + filePath.split("/")[filePath.split("/").length-1]);// 设置文件名 8 byte[] buffer = new byte[1024]; 9 FileInputStream fis = null; 10 BufferedInputStream bis = null; 11 try { 12 fis = new FileInputStream(file); 13 bis = new BufferedInputStream(fis); 14 OutputStream os = response.getOutputStream(); 15 int i = bis.read(buffer); 16 while (i != -1) { 17 os.write(buffer, 0, i); 18 i = bis.read(buffer); 19 } 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } finally { 23 if (bis != null) { 24 try { 25 bis.close(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 } 30 if (fis != null) { 31 try { 32 fis.close(); 33 } catch (IOException e) { 34 e.printStackTrace(); 35 } 36 } 37 } 38 }

     自己之前没有做过在线现在的  项目中用到了  就让大神发了一份代码  看了看  自己研究一下                       

  • 相关阅读:
    3.默认参数的注意事项
    2.关键字global,nonlocal
    1.函数的结构,调用,传参,形参,实参,args,kwargs,名称空间,高阶函数
    19. 实现一个整数加法计算器(多个数相加):
    20.文件操作
    18.使用for循环计算+1-3+5-7+9-11+13...99的结果
    ubuntu docker更新镜像源
    虚拟环境的使用
    前端基础之HTML
    jQuery基本使用
  • 原文地址:https://www.cnblogs.com/zhukaixin/p/9151070.html
Copyright © 2011-2022 走看看