如何从远程php脚本响应中获取二进制文件?

时间:2012-01-19 09:43:40

标签: java php

我正在调用一个脚本,它给我一个带二进制数据的二进制文件(12345.cl)。脚本完成了,它正在工作,如果我将它粘贴到导航器上,我会得到二进制文件。

现在我遇到了一个问题:如何将脚本的响应转换为二进制资源以在我的应用程序中使用它?

目前,我有这段代码:

 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,但我不知道如何将它转换为二进制资源来使用它

我需要使用文件获取datainputstream,但我不知道如何实现它

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

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();
    }
}
相关问题