简体   繁体   English

Java EE输入/输出流

[英]Java EE input/output stream

I want to create a pipe between a client browser <-> my server <-> some other server for downloading some file. 我想在客户端浏览器<->我的服务器<->其他服务器之间创建管道,以下载某些文件。 I am using Apache Tomcat as my server. 我正在使用Apache Tomcat作为服务器。

How can I create the pipe via my server? 如何通过服务器创建管道? I don't have much space on my server, so I don't want to save files on my server. 我的服务器上没有太多空间,所以我不想在服务器上保存文件。

I just want the download data to go via my server due to some reasons. 由于某些原因,我只希望下载数据通过我的服务器。 Data should flow in real time. 数据应实时流动。

Can I do this using streams in Java EE? 我可以使用Java EE中的流来做到这一点吗?

Perhaps this is what you mean? 也许这就是你的意思?

Disclaimer: I have not tried compiling or running any of this 免责声明:我没有尝试编译或运行任何此类

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    URL url = new URL("http://your-other-server/the/resource/you/want");

    InputStream source = url.openStream();
    OutputStream destination = response.getOutputStream();

    byte[] buffer = new byte[1024];
    int length;
    while ((length = source.read(buffer)) != -1) {
        destination.write(buffer, 0, length);
    }

    source.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM