FileOutputStream write与原文件md5不一致,文件变大了 或者 ResponseEntity 下载的文件与原文件md5值不一样

FileOutputStream write与原文件md5不一致,文件变大了

原因写入的时候字符问题代码如下:

 

  public static void main(String[] args) throws IOException {
        String   fileName = "aaa.jar";
        System.out.println("start");
//        Files.copy(Paths.get(path,fileName),Paths.get("d://test//b.jar"));  //这种写法正常
        Path path = Paths.get(AppController.path, fileName);
        Path path1 = Paths.get("d://test//g.jar");

        File file = path.toFile();
        File file1 = path1.toFile();

        FileInputStream fileInputStream = new FileInputStream(file);
        OutputStream outputStream = new FileOutputStream(file1);
//        byte[] buff = new byte[fileInputStream.available()];  //原来的写法
// 下面是改正后的写法(绿色注释掉的错误写法)
        byte[] bts = new byte[1024*1024];
        int len=-1;
        while ((len=fileInputStream.read(bts)) != -1) {
            outputStream.write(bts, 0, len);
        }

//        outputStream.write(buff,0,buff.length);  //原来的写法
        outputStream.flush();
        outputStream.close();
        fileInputStream.close();
        System.out.println("over");
    }

 

上一篇:【leetcode】不邻接植花


下一篇:python pyqt5 打开文件夹 读取文件列表