简体   繁体   English

如何将GZIP内容编码应用于HTTP文件上传multipart / form-data POST

[英]How can I apply GZIP content-encoding to an HTTP file upload multipart/form-data POST

I have a custom java console app I am writing to upload non-binary files to a java app server I own. 我有一个正在编写的自定义Java控制台应用程序,用于将非二进制文件上传到我拥有的Java应用程序服务器。 It is performing an HTTPS multipart/form-data POST with the file to a REST api. 它正在执行HTTPS多部分/表单数据POST,并将文件发送到REST api。 While it works great for small files, I would like to apply GZIP content-encoding to the post request, so it more efficiently handles large files. 虽然它非常适合小文件,但我想将GZIP内容编码应用于发布请求,以便更有效地处理大文件。

Is there a JAVA library I can use to gzip the post, including the file content and then un-zip it on the other side? 是否可以使用JAVA库对帖子进行gzip压缩,包括文件内容,然后在另一侧将其解压缩? I would like to avoid having to zip the file first and would rather rely on HTTP encoding to handle it. 我想避免必须首先压缩文件,而宁愿依靠HTTP编码来处理它。

To be pedantic, you wouldn't gzip the entire POST. 要学究点,您不会gzip整个POST。 You would just Gzip the content data, and then in your POST set the Content-Encoding as gzip . 您只需Gzip内容数据,然后在POST中将Content-Encoding设置为gzip

You haven't post ed your code ( get it???), so some assumptions need to be made to give an example: 您尚未发布代码( 获取代码?),因此需要做出一些假设以给出示例:

import java.util.zip.GZIPOutputStream;
import java.io.ByteArrayOutputStream;
...

final String yourData = "butts";
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final GZipOutputStream gzipOutputStream;

try {
    gzipOutputStream = new GZipOutputStream(byteArrayOutputStream);
    gzipOutputStream.write(yourData.getBytes("utf-8"));
} finally {
    gzipOutputStream.close();
}

final byte[] gzippedButts = byteArrayOutputStream.toByteArray();

/*
 * Now use the gzipped data as the data in your POST, and also
 * make sure to set the Content-Encoding of your HTTP POST to "gzip".
 */

Edit : Reading the question again, it sounds like OP wants a library that will abstract away all of the handling and just Gzip a request body under the hood. 编辑 :再次阅读该问题,听起来OP想要一个将所有处理抽象出来的库,而只是在后台Gzip一个请求主体。 Unfortunately, I am not aware of any such library. 不幸的是,我不知道有任何这样的图书馆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 逻辑应用 http 发布 multipart/form-data 文件上传 - Azure logic app http post multipart/form-data file upload 如何使用Java Spring将带有上传文件的多部分/表单数据表单重新发送到其他服务器 - How to resend post multipart/form-data form with upload file to different server with Java Spring 如何使用SpringMVC和MockMVC发布文件上载的multipart / form-data - How to Post multipart/form-data for a File Upload using SpringMVC and MockMVC 与多部分/表单数据上传表单一起传递参数(Java Http 上传后) - Passing parameters along with a multipart/form-data upload form (Java Http Post Upload) 使用Camel上传多部分表单数据文件 - Multipart form-data file upload with Camel 如何使用multipart / form-data在服务器中上传文件? - how to upload a file in server using multipart/form-data? Spring 我可以使用 multipart/form-data 发布参数数组吗 - Spring Can I post params array with multipart/form-data 如何在 MockMVC 中发布多部分/表单数据? - How to POST multipart/form-data in MockMVC? 如何在java servlet中处理multipart / form-data POST请求? - How can I handle multipart/form-data POST requests in my java servlet? 我如何解析从 multipart/form-data 请求发送的内容以获取 java 中的文件? - How do i parse content sent from multipart/form-data request to get file in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM