0

I have certain piece of code which helps me to save the image on server. I need to know how to display the saved image on div.

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();
}%>
shabbir.rang
  • 279
  • 2
  • 4
  • 24
  • 1
    Use Servlet rather than JSP.And What is the problem ? – Hardik Mishra Sep 14 '12 at 07:11
  • when i browse the image and call this jsp page. Image is saved successfully on D:/../images folder but i do not know how to retrieve that image and display on html. About servlet. I am not so aware of it. Are there any examples for saving image on server? – shabbir.rang Sep 14 '12 at 07:17
  • You _will_ need a servlet. Check [How to show a JSP page using Servlet which has image in its Response](http://stackoverflow.com/questions/6347752) for example. – f_puras Sep 14 '12 at 07:52

1 Answers1

0
 fileName = new File("D:/../images/"+saveFile;).getName();

use this variable to get name and then call fileName in your img src. Hope this helps.

KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52