• java实现本地文件转文件流发送到前端


    java实现本地文件转文件流发送到前端

    Controller

      public void export(HttpServletResponse response) {
       // 创建file对象
          response.setContentType("application/octet-stream");
          // 文件名为 s
          response.setHeader("Content-Disposition", "attachment;fileName=" + s);
          FileUtils.writeBytes(fileName, response.getOutputStream());
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    FileUtils

     public static void writeBytes(String filePath, OutputStream os) throws IOException{
            FileInputStream fis = null;
            try
            {
                File file = new File(filePath);
                if (!file.exists())
                {
                    throw new FileNotFoundException(filePath);
                }
                fis = new FileInputStream(file);
                byte[] b = new byte[1024];
                int length;
                while ((length = fis.read(b)) > 0)
                {
                    os.write(b, 0, length);
                }
            }
            catch (IOException e)
            {
                throw e;
            }
            finally
            {
                IOUtils.close(os);
                IOUtils.close(fis);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    如果是临时文件需要删除
    controller

      public void repairStatisticListExport(HttpServletResponse response) {
       // 创建file对象
          response.setContentType("application/octet-stream");
          // 文件名为 s
          response.setHeader("Content-Disposition", "attachment;fileName=" + s);
          FileUtils.writeBytes(fileName, response.getOutputStream());
          FileUtils.deleteFile(fileName);
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    deleteFile方法

      public static boolean deleteFile(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
          file.delete();
          flag = true;
        }
        return flag;
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    临时文件路径

      public static String getDefaultBaseDir() {
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("windows")) {
          return "C:/uploadPath/";
        } else if (os.toLowerCase().startsWith("linux")) {
          return "/home/uploadPath/";
        }
        return "/home/uploadPath/";
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    Ant Design+react 路由跳转
    Linux基本命令
    [附源码]java毕业设计医院门诊信息管理系统
    使用echarts做一个空气质量指数仪表盘, 对接天气接口, 附源码
    uniapp之ios开发及支付整体流程爬坑记录
    9.25 校招 实习 内推 面经
    volatile与JMM
    【JavaScript复习十六】函数
    快鲸物业管理系统:助力物业管理服务双提升
    .Net 7内容汇总(3)--反射优化
  • 原文地址:https://blog.csdn.net/XLX2339635744/article/details/132627931