Re: Servlet to download a file returning errors
Lew, Thanks for your reply.
Why does the error message say that the error is with
'com.example.web.DownloadServ' when you show the source for
'controller.DownloadServ'?
My bad. I just changed that in the post .. just for ease of reading ..
but convoluted the understanding in the process.
Below is the code again ..
__________________________________________
package com.example.web;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.util.*;
class DownloadServ extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
response.setContentType("application/pdf");
ServletContext ctx=getServletContext();
InputStream is=ctx.getResourceAsStream("/
21stCenturyTiger_Christmas_wishes_2009.pdf");
int read=0;
byte[ ] bytes=new byte[1024];
OutputStream os=response.getOutputStream();
while((read=is.read(bytes)) != -1){
os.write(bytes,0,read);
}
os.flush();
os.close();
}
}
_________________________________________________________
Why do you initialize variables 'read' and 'bytes' to values you simply throw
away?
I am just learning now.. so i just wanted to make it work once and i
am thinking of moving on to these issues later.
Please Help.
Regards
Kuber