java - 收集日志 ZIP 并上传到 Http 服务,无需临时文件

标签 java jira nio apache-httpclient-4.x

我需要创建一个实用程序来收集一组日志,将它们压缩并附加到 jira。

我计划使用 httpclient 4.5。 (有 NIO 支持)jira API 在这里不是问题。

我想通过 zip 流直接从文件系统传输文件并上传到 jira,而不需要在文件系统上创建临时 zip 文件。

在这种情况下,我发现 java API 很困惑,无法弄清楚其用法。 仔细看来,这种情况是 Java NIO 使用的一个很好的候选者(也许是一个 channel ?),但未能找到一个好的例子。

作为起点,我创建了一个上传一个文件的示例,但我无法将其带到 Zip 流的下一步。

HttpClient httpClient = createHttpClient();


final String issueUriAttach = issueUri + "/" + "attachments";

HttpPost postRequest = new HttpPost(issueUriAttach);
final FileInputStream fileInputStream = new FileInputStream("README.md");
InputStream in = new InputStream() {
    @Override
    public int read() throws IOException {
        return 0;
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        return fileInputStream.read(b,off,len);
    }
};
InputStreamBody inputStreamBody = new InputStreamBody(in,"README.csv");
HttpEntity reqEntity = MultipartEntityBuilder.create()
        .setContentType(ContentType.MULTIPART_FORM_DATA)
        .addPart("file", inputStreamBody)
        .build();
postRequest.setHeader(getAuthHeader());
postRequest.setHeader(getNocheckHeader());

postRequest.setEntity(reqEntity);
final HttpResponse response = httpClient.execute(postRequest);

正如我所说,我不限于 java I/O java NIO 也是一个选项..

最佳答案

您必须在单独的线程中将数据写入 ZipOutputStream 并将其通过管道传输到 InputStream 供 HttpClient 读取。

/**
 * Zip a collection of files and pipe the output to an InputStream 
 */
public static InputStream zipSource(Collection<Path> paths) throws IOException {
    // Pipe pair. We zip to the output stream in a separate thread. The data is
    // then available to be read from the input stream.
    PipedOutputStream out = new PipedOutputStream();
    PipedInputStream in = new PipedInputStream(out);

    // Zip the file to the input stream in another thread
    zipExecutor.execute(() -> {
        try {
            zip(paths, out);
        } catch (IOException e) {
            System.err.println("Zip failed " + e);
        }
    });

    return in;
}

/**
 * Zip a collection of files to an output stream
 */
public static void zip(Collection<Path> paths, OutputStream out) throws IOException {
    try (ZipOutputStream zout = new ZipOutputStream(out)) {
        for (Path path : paths) {
            zout.putNextEntry(new ZipEntry(path.getFileName().toString()));
            Files.copy(path, zout);
            zout.closeEntry();
        }
    }
}

private static final Executor zipExecutor = Executors.newCachedThreadPool(r -> {
    Thread t = new Thread(r, "zip task");
    t.setDaemon(true);
    return t;
});

关于java - 收集日志 ZIP 并上传到 Http 服务,无需临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40988310/

相关文章:

java - 一旦选择器返回,我可以从 channel 读取多少和多长时间?

java - 为什么我会得到这个正则表达式分组?

java - 按存在的数字对文本文件的行进行排序,并按降序输出整行

java - 环绕网格错误 - 仅减法?

java - 占地面积临时计划

java - nio OP_READ 和 OP_WRITE 操作之间的通信

java - 临时编码的工作流程

Jira:通过 API 添加新的发布版本

python - 从 python 访问 JIRA

java - 使用 java nio 在 zip 内遍历文件树时出现 nosuchfileException