关于大文件上传,压缩的java操作

1. 无论是压缩大文件,还是 文件上传sftp 如果用到 ByteArrayOutputStream ,需要注意下,会造成堆益出,这个时候我不是很建议调整服务器参数,因为你这个文件 可能还会更大 ,所以一劳永逸的解决办法如下代码,这种方式适合慢一点,但是能传上去就行。
先看一下主运行代码:
public static void main(String[] args) throws Exception {
    String path ="jxd";
    File pathDirectory = new File(path);
    pathDirectory.mkdir();//创建主路径
    
    String  tempOut =doFileMethod(path);
    File fileTmpDir = new File(tempOut);
    sftpUtil.sftp.put(new FileInputStream(fileTmpDir), "test.zip");//上传
    //删除临时文件
    FileUtils.deleteQuietly(fileTmpDir);
    FileUtils.deleteQuietly(pathDirectory);
}

public static String doFileMethod(String path) throws IOException {
    String tmpDir = path + File.separator + "20230712";
    File fileTmpDir = new File(tmpDir);
    fileTmpDir.mkdirs();
    File PathDirectory = new File(path);

    if(!PathDirectory.isDirectory()){
        //注意这段代码很重要 如果目录FileOutputStream 这段代码就会执行不了,他也不回主动给你创建
        //
        throw new FileNotFoundException(path +"没有目录");
    }
    FileOutputStream fileOutputStream= new FileOutputStream( path + File.separator + "zipTemp");
    ZipOutputStream zos = new ZipOutputStream(fileOutputStream);
    WritableByteChannel writableByteChannel= Channels.newChannel(zos);
    String str="你的url 集合json[]";
    byte[] buffer = new byte[10240];
    try {
        JSONArray jsonArray = JSONArray.parseArray(str);
        for (Object object : jsonArray) {
            Map<String, InputStream> map = new HashMap<>();
            JSONObject jsonObject = (JSONObject) object;
            String fileId = jsonObject.getString("fileId");
            String fileName = jsonObject.getString("fileName");
            String NewfileName = tmpDir + File.separator +fileName;
            String url="拼接Url 路径".concat(fileId);
            downloadFile(url, NewfileName, 0);
            compressByUrlFile(fileName, zos, buffer, NewfileName, writableByteChannel);
        }

    }catch (Exception e){
        System.out.println(e.getMessage());

    }finally {
        zos.close();
        fileOutputStream.close();
        writableByteChannel.close();
        FileUtils.deleteQuietly(fileTmpDir);
    }
    return path + File.separator + "zipTemp";
}

/**
 * 可以进行重试的文件下载
 * @param url
 * @param fileName
 * @param retryCount
 */
private static void downloadFile(String url, String fileName, Integer retryCount) throws Exception{
    try {
        InputStream in = new URL(url).openStream();
        FileUtils.writeByteArrayToFile(new File(fileName), IOUtils.toByteArray(in));
        IOUtils.closeQuietly(in);
    } catch (IOException e) {
        e.printStackTrace();
        if(retryCount < 3) {
            downloadFile(url, fileName, retryCount++);
        } else {
            throw e;
        }
    }
}
/**
 * 压缩文件方法
 *
 *
private static void compressByUrlFile(String fileName, ZipOutputStream zos, String NewfileName,WritableByteChannel writableByteChannel) {
int readLength = 0;     //每次读取出来的长度
int count = 0;
int BUFFER = 10240;

    ZipEntry zipEntry = null;
    File dirFile = new File(NewfileName);
    try {
        FileChannel fileChannel = new FileInputStream(dirFile).getChannel();
            zipEntry = new ZipEntry(fileName);
            zipEntry.setSize(dirFile.length());
            zipEntry.setTime(dirFile.lastModified());
            zos.putNextEntry(zipEntry);
            fileChannel.transferTo(0,dirFile.length(),writableByteChannel);
//这是另一种方法,也是网上最多的方法,因为我们是通过url 所以我们可以不转文件
//直接 获取InputStream 输入流
//URL url = new URL("你的url")
//URLConnection urlConnection = url.openConnection();
//urlConnection.setAllowUserInteraction(false);
//InputStream  iS=url.openStream();
//BufferedInputStream buf = new //BufferedInputStream(iS);
//while ((readLength = buf.read(buffer, 0, BUFFER)) != -1) {
//    zos.write(buffer, 0, readLength);
//}
//zos.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }
}