Image viewing problem
Hello ,
I am new to java...I am using tomcat5.5.9...I am using code to upload image in current directory. Code is described below:
String userID ="s-ncp-34";
String chk ="true";
response.setContentType("text/html");
response.setHeader("Cache-control","no-cache");
String err ="";
String lastFileName ="";
String contentType = request.getContentType();
String boundary ="";
finalint BOUNDARY_WORD_SIZE ="boundary=".length();
if(contentType ==null || !contentType.startsWith("multipart/form-data")){
err ="Ilegal ENCTYPE : must be multipart/form-data\n";
err +="ENCTYPE set = " + contentType;
}else{
boundary = contentType.substring(contentType.indexOf("boundary=") + BOUNDARY_WORD_SIZE);
boundary ="--" + boundary;
try{
javax.servlet.ServletInputStream sis = request.getInputStream();
byte[] b =newbyte[1024];
int x=0;
int state=0;
String name=null,fileName=null,contentType2=null;
java.io.FileOutputStream buffer =null;
while((x=sis.readLine(b,0,1024))>-1){
String s =new String(b,0,x);
if(s.startsWith(boundary)){
state = 0;
name =null;
contentType2 =null;
fileName =null;
}elseif(s.startsWith("Content-Disposition") && state==0){
state = 1;
if(s.indexOf("filename=") == -1)
name = s.substring(s.indexOf("name=") +"name=".length(),s.length()-2);
else{
name = s.substring(s.indexOf("name=") +"name=".length(),s.lastIndexOf(";"));
fileName = s.substring(s.indexOf("filename=") +"filename=".length(),s.length()-2);
if(fileName.equals("\"\"")){
fileName =null;
}else{
String userAgent = request.getHeader("User-Agent");
String userSeparator="/";// default
if (userAgent.indexOf("Windows")!=-1)
userSeparator="\\";
if (userAgent.indexOf("Linux")!=-1)
userSeparator="/";
fileName = fileName.substring(fileName.lastIndexOf(userSeparator)+1,fileName.length()-1);
if(fileName.startsWith("\""))
fileName = fileName.substring( 1);
}
}
name = name.substring(1,name.length()-1);
if (name.equals("file")){
if (buffer!=null)
buffer.close();
lastFileName = fileName;
int fileExtension = fileName.indexOf(".");
String fExtName = fileName.substring(fileExtension+1);
// fExtName.toLowerCase();
if ((fExtName.equalsIgnoreCase("jpg")) || (fExtName.equalsIgnoreCase("jpeg")) || (fExtName.equalsIgnoreCase("png")) || (fExtName.equalsIgnoreCase("gif"))){
String fileDir ="./LIM-pics/"+userID+"/";
File file =new File(fileDir);
file.mkdirs();
buffer =new FileOutputStream(fileDir+fileName);
}
else{
}
}
}elseif(s.startsWith("Content-Type") && state==1){
state = 2;
contentType2 = s.substring(s.indexOf(":")+2,s.length()-2);
}elseif(s.equals("\r\n") && state != 3){
state = 3;
}else{
if (name.equals("file"))
if (buffer!=null)
buffer.write(b,0,x);
// out.println(buffer);
}
}
sis.close();
//buffer.close();
}catch(java.io.IOException e){
err = e.toString();
}
}
boolean ok = err.equals("");
if(!ok){
out.println(err);
}else{
}
In this code I am allowing specific type of image files to be uploaded. As I am using current directory for upload...so in tomcat5.5.9, it is taking the pathC:\Program Files\netbeans-5.0\enterprise2\jakarta-tomcat-5.5.9\bin as current directory and uploading images in this path.
But the problem arises when I want to view these images. The code for display image is as follows:
String dirname ="LIM-pics/"+"s-ncp-34";
File f1 =new File(dirname);
File fil;
if (f1.isDirectory()){
String s[] = f1.list();
if (s[0].endsWith("db"))
fil =new File(dirname+"/"+s[1]);
else
fil =new File(dirname+"/"+s[0]);
%>
<img border="0" src="<%=fil.getAbsolutePath()%>" width="100" height="100"> <%}%>
Problem is that this code displays image on server perfectly,but when I want to view image through client, it will not work...it takes image sourceC:\Program Files\netbeans-5.0\enterprise2\jakarta-tomcat-5.5.9\bin , which is not present on client...but it is displaying image name correctly....I don't know how to make this picture visible...or how to view remote image file on the client
please help me in this context, as I am new in this field and blocked at this bug.

