PathToolUtil.java 3.64 KB
package com.ruoyi.utils;

import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;

public class PathToolUtil {

    private static final Logger log = LoggerFactory.getLogger(PathToolUtil.class);

    // 维护写入队列 不停的遍历队列内的任务来完成写入 提高指令的响应速度

//    private static LinkedList<Runnable> runnableList = new LinkedList<>();

    public static boolean generateRelativeImagePath(String catalogue, String imei, String uploadTime, byte[] bits) {
//        runnableList.push();
        String resourceDirectory = "Bsth-admin/src/main/resources";
        String path = StringUtils.format("{}/{}/{}/{}.{}", resourceDirectory,
                catalogue, imei, uploadTime, "jpg");
        File file = new File(path);
        RandomAccessFile raf = null;
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
        }
        try {
            // 创建RandomAccessFile对象,并以读写模式打开文件
            raf = new RandomAccessFile(file, "rw");

            // 将文件指针移动到文件末尾
            raf.seek(file.length());

            // 写入数据
            raf.write(bits);

        } catch (Exception e) {
            log.error("图片写入出错:{}", e.getMessage());
            // TODO 第三次返回true
        } finally {
            try {
                if (raf != null) {
                    raf.close();
                }
            } catch (Exception e) {
                log.info(e.getMessage());
            }
            return true;
        }
    }


    public static boolean generateRelativeSpeechPath(String catalogue, String imei, String uploadTime, byte[] bits) {
//        runnableList.push();
        String resourceDirectory = "Bsth-admin/src/main/resources";
        String path = StringUtils.format("{}/{}/{}/{}.{}", resourceDirectory,
                catalogue, imei, uploadTime, "mp3");
        File file = new File(path);
        RandomAccessFile raf = null;
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
        }
        try {
            // 创建RandomAccessFile对象,并以读写模式打开文件
            raf = new RandomAccessFile(file, "rw");

            // 将文件指针移动到文件末尾
            raf.seek(file.length());

            // 写入数据
            raf.write(bits);

        } catch (Exception e) {
            log.error("图片写入出错:{}", e.getMessage());
            // TODO 第三次返回true
        } finally {
            try {
                if (raf != null) {
                    raf.close();
                }
            } catch (Exception e) {
                log.info(e.getMessage());
            }
            return true;
        }
    }



    public static void main(String[] args) {

        try {
            int count = 0;
            FileInputStream fis = new FileInputStream("C:\\Users\\20412\\Desktop\\Bsth\\岗前检查\\Bsth\\Bsth-admin\\src\\main\\resources\\564.jpg");
            byte[] bits = new byte[1024];
            int len;
            while ((len = fis.read(bits)) != -1) {
                count++;
                for (int i = 0; i < len; i++) {
                    System.out.print(bits[i]);
                }
                System.out.println("分片");
            }
            System.out.println("共" + count + "片");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }


}