0

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?

1 Answer 1

2

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();
}
Sign up to request clarification or add additional context in comments.

5 Comments

This is not using any disk space on your server (except for the program itself).
what if large number of users visit my site simultaneously? will it go slow /or hang?
This code shouldn't cause severe performance degradation if many users visit simultaneously. If it does take a while to run, the Tomcat server should create more threads to handle any other incoming connections from clients. My guess is that any performance degradation will be due to having two sets of network traffic rather than one.
Perhaps you should try running some tests to find out how it performs when dealing with large volumes of data (such as the videos you mentioned). First, time it when the client is downloading directly from the "other server". Next, time it when the client is downloading indirectly through this code.
You may want to update the question to indicate that it is for video.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.