• 文件相关工具类


    1.MultipartFile文件转File

        public File transferToFile(MultipartFile multipartFile) {
    //       选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
            File file = null;
            try {
                String originalFilename = multipartFile.getOriginalFilename();
                String[] filename = originalFilename.split("\\.");
                file=File.createTempFile(filename[0], filename[1]);
                multipartFile.transferTo(file);
                file.deleteOnExit();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return file;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.读取文件(txt、json)

     public String readerMethod(File file) throws IOException {
            FileReader fileReader = new FileReader(file);
            Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            String jsonStr = sb.toString();
            return jsonStr;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.下载网络文件

       /**
         * 从网络Url中下载文件-兼容linux
         *
         * @param urlStr 网络文件地址
         * @param fileName 要保存的文件名
         * @param savePath  要保存的文件路径
         * @throws IOException
         */
        public static void downLoadFromUrl(String urlStr, String fileName, String savePath) throws IOException {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(2 * 1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            InputStream inputStream = conn.getInputStream();
            //获取自己数组
            byte[] getData = readInputStream(inputStream);
            //文件保存位置
            File saveDir = new File(savePath);
            if (!saveDir.exists()) {
                saveDir.mkdir();
    
            }
            File file = new File(saveDir + File.separator + fileName);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(getData);
            if (fos != null) {
                fos.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    
    
    • 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
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    4.压缩文件

            <!--文件压缩-->
            <dependency>
                <groupId>net.coobird</groupId>
                <artifactId>thumbnailator</artifactId>
                <version>0.4.8</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
      //压缩文件
            Thumbnails.of("完整本地文件地址")
                    .scale(1f)//缩放比例
                    .outputQuality(0.5f)//图片质量,在0到1,越接近于1质量越好,越接近于0质量越差。
                    .toFile("压缩完成存放的文件地址");
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    一、excel转pdf格式jacob.jar
    10.7- 的报错整理(与linux、python相关)
    全力助推徐工集团转型升级,迅镭激光智能装备展现硬核实力!
    ES6 的 class 类和Typescript 的 class 类的区别
    金仓数据库 Pro*C 迁移指南(3. KingbaseES Pr*oc 对 Oracle Pro*c 的兼容)
    kubernetes
    AndroidT(13) -- natvie LOG 输出的实现(三)
    [buuctf.reverse] 121-125
    LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
    (附源码)mysql+ssm自助游服务系统 毕业设计 250858
  • 原文地址:https://blog.csdn.net/qq_35222232/article/details/133085535