简体   繁体   中英

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.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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