The following is the code in html to upload a file.
<html>
<body>
<form action="xxxServlet" method="post" enctype="multipart/form-data">
<form action="xxxServlet" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" />
</form>
</body>
</html>
The following is the code for xxxServlet:-
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 = "C:/himanshu/" + saveFile;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
If you don’t want to write the code to parse the input stream taken from the request you can use “commons upload” utility provided by apache.
For using commons upload we have to keep the “commons-fileupload.x.x.jar” and “commons-io-x.x.jar” files in the “WEB-APP / lib” folder. These files can be downloaded from http://commons.apache.org/fileupload/ and http://commons.apache.org/io/ .
The following is the code to place in the xxxServlet:-
File file ;
int maxFileSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
String filePath = context.getInitParameter("file-upload");
// Verify the content type
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") >= 0)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();
while ( i.hasNext() ){
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField() ){
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
}//end of if
}//end of while
}catch(Exception ex) {
ex.printStackTrace();
}
}
Holding the binary data:-
To hold the binary data we have to use InputStream object. This is the object that can hold large binary data. If you pass the InputStream to Statement.setBlob() that saves the binary data to the database.
Getting the real path of the computer on which server is running:-
getServletContext().getRealPath("/upload/files/");
The following is the code to place in the xxxServlet:-
File file ;
int maxFileSize = 5000 * 1024;
ServletContext context = pageContext.getServletContext();
String filePath = context.getInitParameter("file-upload");
// Verify the content type
String contentType = request.getContentType();
if ((contentType.indexOf("multipart/form-data") >= 0)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();
while ( i.hasNext() ){
FileItem fi = (FileItem)i.next();
if ( !fi.isFormField() ){
// Get the uploaded file parameters
String fieldName = fi.getFieldName();
String fileName = fi.getName();
boolean isInMemory = fi.isInMemory();
long sizeInBytes = fi.getSize();
// Write the file
if( fileName.lastIndexOf("\\") >= 0 ){
file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ;
}else{
file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ;
}
fi.write( file ) ;
}//end of if
}//end of while
}catch(Exception ex) {
ex.printStackTrace();
}
}
Holding the binary data:-
To hold the binary data we have to use InputStream object. This is the object that can hold large binary data. If you pass the InputStream to Statement.setBlob() that saves the binary data to the database.
Getting the real path of the computer on which server is running:-
getServletContext().getRealPath("/upload/files/");
References:-
http://corejavaexample.blogspot.in
No comments:
Post a Comment