GeeCON Prague 2014*
The moose likes Servlets and the fly likes Download through servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Download through servlets" Watch "Download through servlets" New topic
Author

Download through servlets

karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
Hi all, The following code does not work for me.. please do suggest a solution

DownloadPersonalCalc.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DownloadPersonalCalc extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
res.setContentType("application/pdf");


InputStream is=getServletContext().getResourceAsStream("/graphs.pdf");

OutputStream os=res.getOutputStream();

int read=0;
byte[] bytes=new byte[1024];


while((read=is.read())!=-1)
{
os.write(bytes,0,read);
}
os.flush();
os.close();
}
}

Locations

graphs.pdf: F:\Tomcat\webapps\Calculator\graphs.pdf
DownloadPersonalCalc.java: F:\Tomcat\webapps\Calculator\WEB-INF\classes

A java addict
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
What does "it doesn't work" mean? What exactly is happening? TellTheDetails


Ping & DNS - my free Android networking tools app
karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
I get a black screen in the browser and no download happens,,,,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
Any exceptions in the log files?

You probably meant to say "is.read(bytes)" instead of "is.read()".
steve claflin
Ranch Hand

Joined: Dec 04, 2008
Posts: 54
Your blank screen is because the browser never got a pdf, but did get the content type header. You can postpone setting the content type until you know you opened and read the file - using a try block to attempt to read the file into a buffer, and set the content type for a pdf at the end of that block, then have the catch block set the content type to text/plain so you can send the stack trace as the response and return. After that, put the code to send the pdf from the buffer, which of course won't run if you got the exception, due to the return.
karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
Ulf Dittmer wrote:
Any exceptions in the log files?
You probably meant to say "is.read(bytes)" instead of "is.read()".


Your are rite.. thank you../.


The java documentation says:

The read(b) method for class InputStream has the same effect as:
read(b, 0, b.length)

while the doc for public int read(byte[] b, int off, int len) throws IOException says

It Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer.
This method blocks until input data is available, end of file is detected, or an exception is thrown.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off] and elements b[off+len] through b[b.length-1] are unaffected.

If the first byte cannot be read for any reason other than end of file, then an IOException is thrown. In particular, an IOException is thrown if the input stream has been closed.

The read(b, off, len) method for class InputStream simply calls the method read() repeatedly. If the first such call results in an IOException, that exception is returned from the call to the read(b, off, len) method. If any subsequent call to read() results in a IOException, the exception is caught and treated as if it were end of file; the bytes read up to that point are stored into b and the number of bytes read before the exception occurred is returned. Subclasses are encouraged to provide a more efficient implementation of this method.

Parameters:
b - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the maximum number of bytes to read.



My question is if the offset value is set to be 0, then wont the document be read everytime from the beginning when it is put in a loop???

[Edit: large red text will just tick people off. Rule of thumb: don't tick off people you are trying to get to help you.]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
It has been a while but as I recall, you also need to set the content length header or the browser never decides that it has all the content. You can of course get the file length before you start if you open /graphs.pdf by means of a File instead of getting as stream.

Bill
karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
William Brogden wrote:It has been a while but as I recall, you also need to set the content length header or the browser never decides that it has all the content. You can of course get the file length before you start if you open /graphs.pdf by means of a File instead of getting as stream.

Bill


Thanks bill.. but i have already found the problem... Would be glad if you can answer to my previous post in this thread...
steve claflin
Ranch Hand

Joined: Dec 04, 2008
Posts: 54
The offset is the position to start WRITING in the destination array, not the location to read from in the source stream. The read starts at the current position in the input stream, which is whatever location you left off at from the previous read.

karthick Soundararaj wrote:

while the doc for public int read(byte[] b, int off, int len) throws IOException says

It Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer.
This method blocks until input data is available, end of file is detected, or an exception is thrown.

. . .

Parameters:
b - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the maximum number of bytes to read.


My question is if the offset value is set to be 0, then wont the document be read everytime from the beginning when it is put in a loop???

[Edit: large red text will just tick people off. Rule of thumb: don't tick off people you are trying to get to help you.]
karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
steve claflin wrote:The offset is the position to start WRITING in the destination array, not the location to read from in the source stream. The read starts at the current position in the input stream, which is whatever location you left off at from the previous read.


Thanks a lot.. Opened my eye.. i realise how closed thinking i have been having!!

 
GeeCON Prague 2014
 
subject: Download through servlets