[英]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.