如何从远程php脚本响应中获取二进制文件? [英] How to get a binary file from a remote php script response?
问题描述
我正在调用一个脚本,它给我一个带二进制数据的二进制文件(12345.cl)。脚本完成了,它正在工作,如果我将它粘贴到导航器上,我会得到二进制文件。
I'm calling a script that gives me a binary file (12345.cl), with binary data. The script is done, and it's working, if I paste it on the navigator I get the binary file.
现在我遇到了一个问题:我如何转换响应脚本到二进制资源中以在我的应用程序中使用它?
Now I have a problem: How I transform the response of the script into a binary resource to use it in my app?
目前,我有这样的代码:
For the moment, i have this code:
public void decodeStream( String mURL ){
BufferedInputStream bis = new BufferedInputStream(new URL(mURL).openStream(), BUFFER_IO_SIZE);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos, BUFFER_IO_SIZE);
copy(bis, bos);
bos.flush();
然后,我有一个带响应的BufferedOutputStream,但我不知道如何将其转换为使用它的二进制资源
Then, I have a BufferedOutputStream with the response, but I don't know how to transform it into a binary resource to use it
我需要获取带文件的datainputstream但我不知道如何实现它
I need to obtain a datainputstream with the file but I don't know how to achieve it
推荐答案
您可以使用以下代码:
public void decodeStream( String mURL, String ofile ) throws Exception {
InputStream in = null;
FileOutputStream out = null;
try {
URL url = new URL(mURL);
URLConnection urlConn = url.openConnection();
in = urlConn.getInputStream();
out = new FileOutputStream(ofile);
int c;
byte[] b = new byte[1024];
while ((c = in.read(b)) != -1)
out.write(b, 0, c);
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}
这篇关于如何从远程php脚本响应中获取二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!