Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Image displayed instead of download

843842Aug 21 2008 — edited Aug 21 2008
I created a servlet with a method for file download . I correctly set the response headers and write the file contents to servletoutputstream ,it works fine for normal files of any other format other than any image format .However for image files they are displayed on the page instead of showing a save as dialog box. Can anyone shed light onto this.The method is briefed as given below.
ServletOutputStream oStream = response.getOutputStream();
				File uFile= new File(filePath);
				FileInputStream fis = new FileInputStream(uFile);
				int c=-1;
				while ((c = fis.read()) != -1){
					   oStream.write(c);
				}
				response.setHeader("Cache-Control", "no-cache");
				response.setHeader("Content-Disposition", "attachment; filename=\""+getFileName(filePath)+"\"");
				response.setContentType(new MimetypesFileTypeMap().getContentType(filePath));
				response.setContentLength((int)uFile.length());
				oStream.flush();
				oStream.close();
				fis.close();
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 18 2008
Added on Aug 21 2008
1 comment
573 views