0

Possible Duplicate:
Retrieving and displaying image from server using JSP

I have certain piece of code which helps me to save the image on server.

JSP CODE:

<%String saveFile="";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile="D:/../images/"+saveFile;
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
}%>

For retrieving the same i have JSP Code:

<%
String name = "";
String path="";
File f = new File("D:/Java/Shabbir/ESignature Work/WebContent");
File[] files = f.listFiles();
for(int i=0;i<files.length;i++){
name=files[i].getName();
path=files[i].getPath();

}
%>

Problem with code is that. I am getting list of files which is in the folder where i just want the name and path of the file i selected. Help really appreciated.

Community
  • 1
  • 1
shabbir.rang
  • 279
  • 2
  • 4
  • 24
  • 2
    Side note: try to avoid JAVA code within JSP. You could for example run that code in a controller and pass the list to your JSP via a model attribute. – sp00m Sep 14 '12 at 11:53
  • Do you think posting your question [twice](http://stackoverflow.com/questions/12419596) will get you better answers? – f_puras Sep 14 '12 at 11:54
  • I have added some more code if you see. That link didnt help so thought of starting a new question along with the new problem i am facing. – shabbir.rang Sep 14 '12 at 11:56

0 Answers0