forcing clients to download a pdf and open it in browser
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Henry_Leung
Posted On:   Monday, January 27, 2003 02:24 AM

I am writing a servlet that will force the client to download a pdf file and open it in browser. My server is JRun 3.0 sp2. Below is the code segment I used: public void service( HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException { /*code that produce a bytes array named "bytes" which contains the pdf contents ........ */ //below begins the download response.setContentType("application/pdf"); response.setContentLength(bytes.length); ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.f   More>>

I am writing a servlet that will force the client to download a pdf file and open it in browser. My server is JRun 3.0 sp2. Below is the code segment I used:



			
public void service(
HttpServletRequest request,
HttpServletResponse response
) throws IOException, ServletException
{
/*code that produce a bytes array named "bytes" which contains the pdf contents
........
*/


//below begins the download
response.setContentType("application/pdf");
response.setContentLength(bytes.length);

ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
return;
}





However, the browser always wrongly recognize the data as text/html content, and display them as plain text. I doubted whether the setContentType method is working. So I added getContentType method before and after the line of setContentType to check. But a java.lang.NoSuchMethodException occured at runtime, stating that the setContentType method was not found.....

I supsect that it is JRun's class allaire.jrun.servlet.JRunResponse , which is the class that the response object above belongs to, causing the problem. Perhaps it has not implemented the methods setContentType and getContentType ....



If it hasn't, how should I work this around?



If it has, any suggestion about the cause of this mis-judge of content type?
   <<Less

Re: forcing clients to download a pdf and open it in browser

Posted By:   Henry_Leung  
Posted On:   Monday, January 27, 2003 07:46 PM

Sorry, a little typo. The last sentence of the second paragraph above should be:



But a java.lang.NoSuchMethodException occured at runtime, stating that the getContentType method was not found.....
About | Sitemap | Contact