Java 如何从球衣响应中保存文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20336235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save a file from jersey response?
提问by Jean-Fran?ois Savard
I am trying to download a SWF file using Jersey from a web resource.
我正在尝试使用 Jersey 从 Web 资源下载 SWF 文件。
I have written the following code, but am unable to save the file properly :
我编写了以下代码,但无法正确保存文件:
Response response = webResource.request(MediaType.APPLICATION_OCTET_STREAM)
.cookie(cookie)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
String binarySWF = response.readEntity(String.class);
byte[] SWFByteArray = binarySWF.getBytes();
FileOutputStream fos = new FileOutputStream(new File("myfile.swf"));
fos.write(SWFByteArray);
fos.flush();
fos.close();
It is save to assume that the response does return a SWF file, as response.getMediaType
returns application/x-shockwave-flash
.
假设响应确实返回 SWF 文件,如response.getMediaType
返回application/x-shockwave-flash
.
However when I try to open the SWF, nothing happen (also no error) which suggest that my file was not created from response.
但是,当我尝试打开 SWF 时,没有任何反应(也没有错误)表明我的文件不是从响应中创建的。
采纳答案by ocroquette
From Java 7 on, you can also make use of the new NIO API to write the input stream to a file:
从 Java 7 开始,您还可以使用新的 NIO API 将输入流写入文件:
InputStream is = response.readEntity(InputStream.class)
Files.copy(is, Paths.get(...));
回答by peter.petrov
I think you should not go through a String at all.
我认为你根本不应该通过一个字符串。
If the response is really the SWF file (binary data), then here:
如果响应确实是 SWF 文件(二进制数据),那么这里:
String binarySWF = response.readEntity(String.class);
instead of this code (which reads a String), just try reading
a byte array. See if it helps. Strings are related to encodings
and probably that messes things up.
而不是这个代码(它读取一个字符串),只需尝试读取
一个字节数组。看看它是否有帮助。字符串与编码有关
,可能会把事情搞砸。
回答by Jean-Fran?ois Savard
I've finally got it to work.
我终于让它工作了。
I figured out reading the Jersey API that I could directly use getEntity
to retrieve the InputStream of the response
(assuming it has not been read yet).
我想通了阅读 Jersey API,我可以直接使用它getEntity
来检索的 InputStream response
(假设它还没有被读取)。
Using getEntity
to retrieve the InputStream
and IOUtils#toByteArray
which create a byte array from an InputStream
, I managed to get it to work :
使用getEntity
检索InputStream
和IOUtils#toByteArray
其创建从一个字节数组InputStream
,我设法得到它的工作:
Response response = webResource.request(MediaType.APPLICATION_OCTET_STREAM)
.cookie(cookie)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
InputStream input = (InputStream)response.getEntity();
byte[] SWFByteArray = IOUtils.toByteArray(input);
FileOutputStream fos = new FileOutputStream(new File("myfile.swf"));
fos.write(SWFByteArray);
fos.flush();
fos.close();
Note that IOUtilsis a common Apache function.
请注意,IOUtils是一个常见的 Apache 函数。