Handling images with Http Servlet
Am working on developing a Proxy Server. Tomcat as the Servlet engine and a Servlet that recieves the URL requests from the user and then returns the web page requested back to the user.
The problem: I mananged to get the web pages open, but any images on these pages wouldn't.
I think the approach I used to handle these request isnt effiecient and there must be a better way to do it.
Does anyone have any idea or tried this before?
The code:
PrintWriter out = resp.getWriter();
String url = null;
if (req.getQueryString() != null) {
url = req.getRequestURL().toString() + req.getQueryString();
}
else {
url = req.getRequestURL().toString();
}
try {
URL site = new URL(url);
HttpURLConnection conn = (HttpURLConnection) site.openConnection();
InputStream inS = conn.getInputStream();
BufferedReader in = new BufferedReader( new InputStreamReader( inS));
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println(inputLine);
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("\nMalformedURLException\n");
out.println("\nMalformedURLException\n");
}
[1283 byte] By [
AtGma] at [2007-10-2 19:29:36]

Is that the code you are using to send images? Then your problem is because you used a Reader and a Writer. Those are only useful for text files (note that you're reading one line at a time), and images are not text files. Get rid of them entirely. Read from an input stream (BufferedInputStream if you like) and write to an output stream and not a writer. This will work just as well for text files, so don't bother to write one version of the code for text and another version for images.
DrClap: I did try the BufferedInputStream before, but I didnt manage to get it display the web pages. While I can with BufferedReader.
With BufferedReader, at least it has the method readLine() which I can assign it to a String that displays the output in the browser (HTML).
I am trying now again with the BufferedInputStream, but so far I can't figure out how to get the same result,
Below is the working code that I can at least open the webpages with, but the images do not display:
InputStream inS = conn.getInputStream();
BufferedReader in = new BufferedReader( new InputStreamReader( inS));
String inputLine;
while ((inputLine = in.readLine()) != null){
out.println(inputLine);
}
BufferedInputStream returns bytes and that wouldnt display it as (HTML)
AtGma at 2007-7-13 21:16:39 >

One more thing in the code that you have given you are not handling the header information. You have to read the relevent responce headers from the URLConnection and add them to the responce of the serverlet.
atleast the content-type and content-encoding othewise the browsers has no way of knowing what the meaning of incomming data
LRMKa at 2007-7-13 21:16:39 >

LRMK: Thanks for the tip. I am yet trying to grasp the whole concept of how to handle the whole project.
I left out these 2 lines of code.
resp.setContentType("Text/html");
resp.setHeader("Cache-Control","no-cache");
So far these are the lines I have, not too sure yet if my problem can be solved by setting others or if any others are required.
AtGma at 2007-7-13 21:16:39 >

A friend helped me out, below is the code which works for me, it might be useful to anyone.
try {
URL site = new URL(url);
HttpURLConnection conn =
(HttpURLConnection) site.openConnection();
InputStream input = conn.getInputStream();
BufferedInputStream stream = new BufferedInputStream(input);
int read = 0;
while ((read = stream.read()) != -1) {
out.write(read);
}
stream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("\nMalformedURLException\n");
out.println("\nMalformedURLException\n");
}
Thanks to everyone who tried to help.
AtGma at 2007-7-13 21:16:39 >

> LRMK: Thanks for the tip. I am yet trying to
> grasp the whole concept of how to handle the whole
> project.
>
> I left out these 2 lines of code.
>
> resp.setContentType("Text/html");
> resp.setHeader("Cache-Control","no-cache");
>
> So far these are the lines I have, not too sure yet
> if my problem can be solved by setting others or if
> any others are required.
These lines wil course the problems when handling the images
Different file types have different content types and you should always provide the content type that describes the type of incomming data.
For html files you can use Text/HTML
for plain text you user Text/Plain
for images you have to use one of Image/giff, Image/jpeg, Image/png......
depending on the type of the image that you are handling. Otherwise the browser will not handle it properly.
LRMKa at 2007-7-13 21:16:39 >
