reset password
Author Message
talisunep
Posts: 140
Posted 17:59 May 20, 2013 |

professor what kind of changes do we need to make to the class design to add the profile pictures?
do we need to make any changes to class  for the homework ?

cysun
Posts: 2935
Posted 18:13 May 20, 2013 |

1. You need to figure out that. This is a homework after all.

2. Yes.

talisunep
Posts: 140
Posted 13:24 May 21, 2013 |

professor i am getting this error
 

 

HTTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.web.multipart.commons.CommonsMultipartFile cannot be cast to java.io.File

my controller looks like this... why am i getting this error? my commons io and file upload dependencies have also been included...

 

byte[] image = new byte[(int) ((File) file).length()];

try {

FileInputStream fileInputStream = new FileInputStream(file.getName());

fileInputStream.read(image);

fileInputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

cysun
Posts: 2935
Posted 15:10 May 21, 2013 |
talisunep wrote:

professor i am getting this error
 

 

HTTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.web.multipart.commons.CommonsMultipartFile cannot be cast to java.io.File

my controller looks like this... why am i getting this error? my commons io and file upload dependencies have also been included...

 

byte[] image = new byte[(int) ((File) file).length()];

try {

FileInputStream fileInputStream = new FileInputStream(file.getName());

fileInputStream.read(image);

fileInputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

Isn't the error message pretty clear?
 

talisunep
Posts: 140
Posted 20:49 May 22, 2013 |

professor i am using ImageIO, i am able to retrive the image data via controller/userDao then i use the Image io to convert back to image(jpg in this case)  my question is can i pass the file to jsp to display as image ? currently it only displays image.jpg and not the image as i did a

models.put("outputFile", outputFile);

talisunep
Posts: 140
Posted 20:57 May 22, 2013 |

in this case i did a test on my controller
 

ImageIO.write(convert, "jpg", new File("/Users/talisuneplongkumer/Desktop/demoUpload/1.jpg"));

up till this point the image is being retrived from the database and converted back to jpg  successfully when i use imageIO.write?
How can i pass this to the jsp?

cysun
Posts: 2935
Posted 21:44 May 22, 2013 |

You can't just "write" an image to a JSP. A JSP is basically an HTML page (with some dynamic content), and the only way to include an image in HTML is to use the <img> tag. The src attribute of <img> should point to some controller that returns the image (like in the file download example).

talisunep
Posts: 140
Posted 22:18 May 22, 2013 |

okay, professor if i use a different say image controller for the image recovery and handling from databse how can i invoke this image controller when i want to display that image for that id in jsp? 
 

if i seperate my lawyer  controller display how can the jsp for display for lawyer display image of that lawyer fron another controller?
is there sessions involved?

when i visit display lawyer -  my lawyer controller gets displayed...
how can i make this image controller take in the same id parameter and take action

can we do something no the jsp like

<c:when test="${ not empty userChecked}">
then
<img src="image?lawyerID=4" >

but here i was wondering  how image controller will invoke the controller to get image for id=4?

Last edited by talisunep at 22:31 May 22, 2013.
cysun
Posts: 2935
Posted 22:44 May 22, 2013 |

I don't see what the difficulty is. In your JSP you would have something like <img src="getImage?lawyerId=${lawyer.id}" />.

Last edited by cysun at 22:45 May 22, 2013.
talisunep
Posts: 140
Posted 23:00 May 22, 2013 |

<img src="getImage?lawyerId=${lawyer.id}" />.

professor i was wondering for getImage will it be a controller or a method inside lawyer controller?

talisunep
Posts: 140
Posted 00:12 May 23, 2013 |
talisunep wrote:

<img src="getImage?lawyerId=${lawyer.id}" />.

professor i was wondering for getImage will it be a controller or a method inside lawyer controller?

created a controller servlet it working now... thank u

talisunep
Posts: 140
Posted 05:26 May 23, 2013 |

Professor whats the best way to access/point-to files within the  spring directory?

For the default lawyer image.. i dont know if i am following the right approach but i am putting a default image called
default.jpg on my WEB-INF/image folder and i am trying to read this and convert into byte[] when a lawyer does not upload an image...

the problem here i am facing is that spring is not able to identify the path to read from the folder..
i've tried
String fileLocation = resp.getServletContext().getRealPath("/src/main/webapp/WEB-INF/images/");
which gives me a path/file not  found error

as well as
InputStream in = getClass().getResourceAsStream("/default.jpg");
which gives me a null pointer error

 

cysun
Posts: 2935
Posted 08:10 May 23, 2013 |
talisunep wrote:

Professor whats the best way to access/point-to files within the  spring directory?

For the default lawyer image.. i dont know if i am following the right approach but i am putting a default image called
default.jpg on my WEB-INF/image folder and i am trying to read this and convert into byte[] when a lawyer does not upload an image...

the problem here i am facing is that spring is not able to identify the path to read from the folder..
i've tried
String fileLocation = resp.getServletContext().getRealPath("/src/main/webapp/WEB-INF/images/");
which gives me a path/file not  found error

as well as
InputStream in = getClass().getResourceAsStream("/default.jpg");
which gives me a null pointer error

I'd put /image in WebContent instead of WEB-INF, and use a <c:if> in the JSP to either load the user image through a controller or the default image by direct link (e.g. <img src="image/default.jpg">). It's easier to program and gives better performance because the browser can cache the the image for all the lawyers who didn't upload their own images.

In your approach the correct path to getRealPath() is "/WEB-INF/image/default.jpg". getReadPath() converts a path relative to the application context to a path on disk.