i want to creat a pipe between clint browser<->my server <-> some other server .For downloadin some file. I am using apache tomcat as my server. How can i create the pipe via my server ? i dont have mucg space on my server . So i dont wan to save files on my server .I just want the download data to go via my server due to some reasons.Data should og in real time. Can i do this using streams in j2ee? if yes how?
A:
Look at this thread - http://stackoverflow.com/questions/364936/input-and-output-stream-pipe-in-java
adatapost
2009-08-20 14:30:12
+2
A:
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();
}
Adam Paynter
2009-08-20 14:34:53
will this work if i have less space on my server?
2009-08-20 14:46:03
This is not using any disk space on your server (except for the program itself).
Bombe
2009-08-20 14:46:59
what if large number of users visit my site simultaneously? will it go slow /or hang?
2009-08-20 15:02:20
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.
Adam Paynter
2009-08-20 15:12:51
shoul i go for this code for video sharing site?
2009-08-20 15:15:56
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.
Adam Paynter
2009-08-20 15:27:12
You may want to update the question to indicate that it is for video.
Adam Paynter
2009-08-20 15:27:43