2012-01-19 34 views
0

我打給我一個二進制文件(12345.cl),帶有二進制數據的腳本。該腳本已完成,並且正在工作,如果我將其粘貼到導航器上,我將獲得二進制文件。如何從遠程php腳本響應中獲取二進制文件?

現在我有一個問題:我如何將腳本的響應轉換爲二進制資源以在我的應用程序中使用它?

就目前而言,我有這樣的代碼:

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與文件,但我不知道如何去實現它

+0

你想寫一個PHP腳本來採取應對或什麼?:) – veritas

+0

不,我有PHP腳本,是在mURL參數,PHP腳本給了我一個二進制文件,我想收到它與java – NullPointerException

回答

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(); 
    } 
} 
+0

它是一個二進制文件,而不是一個文本文件,我也需要獲得與該文件的datainputstream,你知道我必須做的改變,以獲得它? – NullPointerException

+0

我相信你正在捕獲一個PHP腳本的輸出,就像'http:// domain.com/foo.php'。現在你是說這個PHP腳本的輸出是二進制的,比如圖像或音樂文件,而不是一些文本? – anubhava

+0

我已經更新了從mURL流中讀取原始字節並將其寫入文件的答案。請檢查。 – anubhava

相關問題