I dont know how to solve this..Can anyone help me..

The project is an Intranet aplication... how can a client upload a file/image.... Uploaded file/image should be to the server...


9 Years
Discussion Span
Last Post by peter_budo
<%@ page import="java.io.*" %>

String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
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);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());


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 = "/usr/programs/apache-tomcat-6.0.16/webapps/Pinnacle/hr_photoGallery/" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);

fileOut.write(dataBytes, startPos, file.length());

out.println("File saved as " +saveFile);


This will help to upload an image to the server


Yes, an upload is obviously the right way, but not in a JSP. God's write an actual Servlet, for Christ's sake. This scriptlet stuff is becoming a real scourge. Let me guess, you got this from roseindia, right?

Also, I assume that "/usr/programs/apache-tomcat-6.0.16/webapps/Pinnacle" is the directory in which your application is found? I.E. the Context Root. There are better ways of getting that information than hardcoding it.

Also, using "\n" in that "indexOf" stuff is also a bad idea. What happens if the client doing the upload is a MAC (which uses only \r for the line endings)?


no its not from roseIndia... sorry for "jsp"...and thanks for all the help given........Thank you alll....


no its not from roseIndia... sorry for "jsp"...and thanks for all the help given........Thank you alll....

Are you sure of that? It does really look like that "crap" (sorry for my French ;) )

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.