Groovy 如何将图像加载到浏览器输出流中?

Groovy 如何将图像加载到浏览器输出流中?,groovy,proxy,streaming,Groovy,Proxy,Streaming,我有一个用Groovy编写的小型代理服务器,它会记住用户的请求,然后在SocketOutputStream中加载原始内容。所有内容(html、js和css)都可以正常加载,但图片除外。所有二进制内容都将从页面中消失 这是我的密码: int host = 3128 def address = Inet4Address.getByName("localhost") def server = new ServerSocket(host, 25, address) println "Appication

我有一个用Groovy编写的小型代理服务器,它会记住用户的请求,然后在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()
        }
    }
  }
int host=3128
def address=Inet4Address.getByName(“本地主机”)
def服务器=新服务器套接字(主机,25,地址)
println“应用程序在$address$host上启动”
while(true){
server.accept(){socket->
socket.withStreams{SocketInputStream输入,SocketOutputStream输出->
def reader=input.newReader()
字符串缓冲区=reader.readLine()
params=buffer.split(“”)
字符串url=params[1]
URL contentUrl=新URL(URL)
//用要求的内容做一些艰苦的工作
output.withWriter{writer->

writer如果您需要二进制数据,则不应使用读卡器和写卡器

尝试替换:

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