Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

Uploading Multiple Files by Using JSP
JSP is mainly used for the presentation logic, In this example we are going to tell you how can make use of scriptlet to upload multiple files.

In this example we are going to tell you how we can upload multiple files by using Jsp and how it will get stored on a particular memory area.Firstly we need controller,For this program we are using one package java.io.* which provides us classes and interfaces for java input/output.The logic of the program will be written inside the scriptlet. The scriptlet tag is mostly used for writing the java code inside the jsp.

The classes and interfaces we have used in the program are List and Iterator of java.util.* package, ServletFileLoad DiskFileItemFactory. These are the classes which have been provided to us by the apache to help us in file uploading.

Inside the scriptlet call the is MultipartContent() method of class ServletFileUpload which takes one parameter request. This method checks whether there is a file upload request or not. It returns the boolean value. If there is a request for uploading the file then it makes a object of DiskFileItemFactory class and implements FileItemFactory. The implementation of FileItemFactory means that it creates FileItem instances which keep their content either in memory or disk. While using the DiskFileItemFactory the temporary files are automatically deleted as soon as they are no longer needed. The interface FileItemFactory is a factory for creating FileItem instances.

In this program firstly you will get a jsp page which will have the option of browse and Send Files. The logic behind it will be controlled by the controller.

 
Code of the index_multiple_upload.jsp ________________________________________________________ <%@ page language="java" % >
<HTML>
<HEAD>
<TITLE>
Display file upload form to the user</TITLE>
</HEAD>
<% // for uploading the file we used Encrypt type of multipart/ form-data and input of file type to browse and submit the file %>
<BODY>
<FORM ENCTYPE="multipart/form-data" ACTION= "sinle_upload_page.jsp" METHOD=POST>
<br>
<br><br>
<center>
<table border="2" >
<tr>
<center>
<td colspan="2">
<p align= "center">
<B>
PROGRAM FOR UPLOADING THE FILE</B>
<center>
</td>
</tr> <tr>
<td>
<b>
Choose the file To Upload:</b> </td> <td>
<INPUT NAME="F1" TYPE="file">
</td>
</tr>
<tr>
<td colspan="2">
<p align="right">
<INPUT TYPE="submit" VALUE="Send File" >
</p>
</td>
</tr>
</table>
</center>
</FORM>
</BODY>
</HTML> __________________________________________________ Code of the Multiple_upload.jsp
<%@ page import="java.io.*" %>
<% //to get the content type information from JSP Request Header
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and as well as the passed data from mulitpart/form-data is greater than or equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/ form-data") >= 0))
{
DataInputStream in = new DataInputStream(request. getInputStream());
//we are taking the length of Content type data int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code while (totalBytesRead < formDataLength)
{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
} String file = new String(dataBytes);
//for saving the file name 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());
int pos;
//extracting the index of file 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;
// creating a new file with the same name and writing the content in new file
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
<Br>
<table border="2">
<tr>
<td>
<b>
You have successfully upload the file by the name of:</b>
<% out.println(saveFile); %>
</td>
</tr>
</table>
<%
}
%>
 
Job or extra money for students

Search Engine Rank of your blog or websites