Forum Statistics

  • Forum Members: 4,245
  • Total Threads: 3,572
  • Total Posts: 2
There are 1 users currently browsing forums.

Partner Sites

Reply
Old 10-12-2009, 01:33 PM   #1 (permalink)
 
Status: Newbie
Join Date: Sep 2009
Posts: 3

Rep Power: 10 arbaba is on a distinguished road



Default Code for file upload in JSP

Use request object to upload file using JSP.
1.. make an html page from where u user input tag with file type..
this page should be only one input tag.... not more than one..
form tage should use encrype="multipart/form-data".
2.. then use this code... on JPS side....
this is my Bean... where i send request as parameter...
and FileServerPath is the target location.
3.. this method return false, if fails and enter the error
into ErrorMessage gloabal variable.




Quote:
public boolean FileUploadProcess(javax.servlet.http.HttpServletRe quest
request,
String FileServerPath)
{
String contentType = request.getContentType();
if ((contentType != null) &&
(contentType.indexOf("multipart/form-data") >= 0))
{
try
{
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);
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());
//out.println(boundary);
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;

String tmp_file = saveFile.toLowerCase();
if (tmp_file.equals(this._GIF) ||
tmp_file.equals(this._JPEG) ||
tmp_file.equals(this._JPG) || tmp_file.equals(this._PNG))
{
this.FileUserName = saveFile;
this.FileNewName = (new java.util.Date().getTime()) +
saveFile;
//System.out.println("File path = " +
FileServerPath+this.FileNewName);
FileOutputStream fileOut = new
FileOutputStream(FileServerPath +
this.FileNewName);

fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
return true;
}
else
{
this.ErrorMessage = "Invalid File Format. " +
"File Format should be .png, .gif,
.jpg or .jpeg.";
return false;
}
}
catch (Exception exception)
{
this.ErrorMessage = exception.getMessage();
return false;
}
}
else
{
this.ErrorMessage = "Ilegal ENCTYPE : must be
multipart/form-data\n" +
"ENCTYPE set = " + contentType;
return false;

}
}
arbaba is offline Add Infraction for arbaba   Reply With Quote
Old 11-12-2009, 05:15 PM   #2 (permalink)
 
Status: On a mission
Join Date: Nov 2009
Posts: 34

Rep Power: 13 vikas1234 is on a distinguished road



Default

thanks for sharing it with us
vikas1234 is offline Add Infraction for vikas1234   Reply With Quote


Reply

Bookmarks

Tags
None


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may post attachments
You may edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.5.0 RC2