Loading a jpg using jcifs and displaying it
I have a jsp page that calls a servlet through an img tag, <img
src="image?file=Image002/20060725/273251aaaa.jpg" />. The doGet method retrieves the image through the getPhoto method where I use jcifs to connect to the filesystem to retrieve the file. However, when I print out the contentLength to verify that in fact I loaded the image it comes out empty. I made sure that the connection was made by testing on a path that I know does not exist, jcifs.smb.SmbException: The system cannot find the file specified. When I give the correct path I do not get an exception but I get a broken image displayed. Can someone tell me what I am doing wrong on my getPhoto method?
Thanks.
protected void doGet(HttpServletRequest req,HttpServletResponse res) throws
ServletException, IOException
{
// Get file name from request.
String imageFileName = req.getParameter("file");
// Check if file name is supplied to the request.
if (imageFileName == null) {
return;
}
System.err.println(imageFileName);
// Prepare streams.
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
// Open image file.
in = new BufferedInputStream(getPhoto(imageFileName));//get Photo
out = new BufferedOutputStream(res.getOutputStream());
int contentLength = in.available();
if(contentLength == 0 ){
System.err.println("BAD IMAGE");
}else{
System.err.println("Good Image");
}
// Init servlet response.
res.setContentLength(contentLength);
res.setContentType("image/jpeg");
out = new BufferedOutputStream(res.getOutputStream());
// Write file contents to response.
while (contentLength-- > 0) {
out.write(in.read());
}
// Finalize task.
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
finally {
// Gently close streams.
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static InputStream getPhoto(String imgPath){
SmbFileInputStream in = null;
System.err.println("Test1"); //make sure methood is being called
Config.setProperty("jcifs.netbios.wins", "ip");
Config.setProperty("jcifs.smb.client.domain", "domain");
Config.setProperty("jcifs.smb.client.username", "user");
Config.setProperty("jcifs.smb.client.password", "secret");
try{
in = new SmbFileInputStream("smb://ip/idworks/" + imgPath);
System.err.println("Test2");
}catch(Exception e){
e.printStackTrace();
System.err.println("Test3");
}
System.err.println("Test4");
return in;
}

