aspose file tools*
The moose likes JSP and the fly likes Doubt regarding Image downloading with JSP/Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Doubt regarding Image downloading with JSP/Servlet" Watch "Doubt regarding Image downloading with JSP/Servlet" New topic
Author

Doubt regarding Image downloading with JSP/Servlet

Anil Chintha
Greenhorn

Joined: Oct 26, 2007
Posts: 1
Hi All,

I have written image downloading code in servlet and it is working fine.But If I write the same code in jsp it is not working ...
Could any one let me know the reason ?

Code In Servlet's Do Get method...

response.setContentType("image/jpeg");
ServletOutputStream out;
out = response.getOutputStream();
String uploadDir = "C:\\Users\\ufindanil\\Documents\\NetBeansProjects\\HibWeb/web/uploadedphotos/Parents/DadPose.jpg";
FileInputStream fin = new FileInputStream(uploadDir);

BufferedInputStream bin = new BufferedInputStream(fin);
BufferedOutputStream bout = new BufferedOutputStream(out);
int ch =0; ;
while((ch=bin.read())!=-1)
{
bout.write(ch);
}

bin.close();
fin.close();
bout.close();
out.close();

Code in JSP:

<%@page import="java.io.BufferedOutputStream"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.FileInputStream"%>
<%
response.setContentType("image/jpeg");


String uploadDir = "C:\\Users\\ufindanil\\Documents\\NetBeansProjects\\HibWeb/web/uploadedphotos/Parents/DadPose.jpg";
FileInputStream fin = new FileInputStream(uploadDir);

BufferedInputStream bin = new BufferedInputStream(fin);
BufferedOutputStream bout = new BufferedOutputStream(response.getOutputStream());
int ch =0; ;
while((ch=bin.read())!=-1)
{
bout.write(ch);
}

bin.close();
fin.close();
bout.close();
out.close();

%>

When I run jsp file it is saying image can't be displayed because it contains errors
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Anil Chintha wrote:I have written image downloading code in servlet and it is working fine.But If I write the same code in jsp it is not working ...

Why on earth would you move this code to a JSP? JSPs should not be used for such purposes. A servlet is the correct choice.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Anil Chintha wrote:When I run jsp file it is saying image can't be displayed because it contains errors

Read this article to understand how JSP works and why the image is corrupt.

Never use a JSP to serve binary data. In fact, never put any Java code in a JSP at all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding Image downloading with JSP/Servlet
 
Similar Threads
file downloader for image downloading not upto the limit....
copying files
Program to Copy *.txt files in a Directory
Streaming an image to the browser
java.io.Exception: Insufficient system resources exist to complete the requested serv