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;

}

[2927 byte] By [mccordla] at [2007-11-27 9:03:01]
# 1

looking at the API documentation:

"This stream class is unbuffered. Therefore this method will always

return 0 for streams connected to regular files."

I had wrapped the SmbFileInputStream in a BufferedInputStream:

in = new BufferedInputStream(new SmbFileInputStream(...));

contentLength = in.available()

However, a 0 is still returned as the contentLength

Please, someone has bound to know what I am doing wrong. I've been working on this for two months.

Any Suggestions?

mccordla at 2007-7-12 21:34:31 > top of Java-index,Java Essentials,Java Programming...