Total members 11890 |It is currently Thu Apr 25, 2024 10:52 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





hi can anyoneeeeeeeeeeeeeeeeeeeeee please help me with a JSP code to upload
files on to the server please....




Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

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.



Code:


public boolean FileUploadProcess(javax.servlet.http.HttpServletRequest
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;

}
}


_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time

Need help on jsp and mysql.... i have to upload jsp and mysql to a web hosting company..how?


Author:
Newbie
User avatar Posts: 29
Have thanks: 0 time

Fileupload with request Parameters

Code:
http://itminds-usefullinks.blogspot.in/2011/12/upload-excel-sheet-content-into.html



Author:
Post new topic Reply to topic  [ 4 posts ] 

  Related Posts  to : File Upload in JSP
 Upload PDF file in php     -  
 JSP to Upload File     -  
 Upload File to a new directory     -  
 upload file in ajax     -  
 Upload PHP file code     -  
 PHP HTML form with to file upload     -  
 Struts2 upload file and ajax     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 Upload Form     -  
 Servlets how to upload Files     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com