2014-04-29 77 views
0

我有一個用Groovy編寫的小型代理服務器,它記住用戶的請求,然後加載原始內容int SocketOutputStream。一切(html,js和css)加載正常 - 但圖片。所有二進制內容都從頁面中消失。如何將圖像加載到瀏覽器輸出流?

這裏是我的代碼:

int host = 3128 
def address = Inet4Address.getByName("localhost") 
def server = new ServerSocket(host, 25, address) 
println "Appication strted on $address, $host" 

while (true) { 
    server.accept() { socket -> 
    socket.withStreams {SocketInputStream input, SocketOutputStream output -> 
     def reader = input.newReader() 

     String buffer = reader.readLine() 
     params = buffer.split(' ') 
     String url = params[1] 

     URL contentUrl = new URL(url) 
     //Doing some hard work with requested content 

     output.withWriter { writer -> 
      writer << new URL(url).openStream() 
     } 
    } 
    } 

}

也許有人cpould幫助我嗎? 謝謝。

回答

1

如果你期待的二進制數據,則不應使用讀者和作家......

嘗試更換:

output.withWriter { writer -> 
     writer << new URL(url).openStream() 
    } 

隨着

new URL(url).withInputStream { uin -> 
     uin.eachByte(4096) { buffer, nBytes -> 
      output.write(buffer, 0, nBytes) 
     } 
    } 
+0

感謝了很多人 - 真的幫助! –