Pr.Pg Next Pg

File Uploading tutorials

  • HTML form tag allow user to upload files to the server. Uploading file could be anything from text file to binary file.

  • Form tag with Post method send to JSP or servlet for further process and uploading file to server.

 

Creating a file upload form

  • To create a File Upload form in JSP you will use form tags method attribute as POST method, since GET method cannot be used because of file size and binary data files, you will also use enctype attribute as multipart/form-data.

  • The form action attributes should be set to JSP or Servlet file which would handles file uploading process, or if you are using JSP page then no need of setting action because JSP pages can handle both Java code and HTML code in same page.

  • To upload a single file you should use a single <input …/> tag with attribute type=”file”. To allow multiple files uploading, include more than one input tags with different values for the name attribute, we also need one <input../> tag with attribute typle=”submit” to send the file to servlet or JSP for further processing and uploading.

 

 

JSP program for file uploading

Sam Sir

<%-- Program name SingleUpload.jsp --%>

<%-- JSP program for file uploading --%>

<%@ 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);

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;

File f = new File("d:/data/" + saveFile);

FileOutputStream fileOut = new FileOutputStream(f);

System.out.println("File :: " + f.getAbsolutePath());

int len = endPos - startPos;

len = dataBytes.length;

System.out.println(dataBytes.length + " # " + startPos + " # "

+ endPos + " @@@ " + len);

fileOut.write(dataBytes, startPos, len - startPos);

fileOut.flush();

fileOut.close();

out.println("File saved at " + f.getAbsolutePath()+"<BR>");

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

} else {

System.out.println("Error in Uploading");

}

%>

<html>

<head>

<title>Upload page</title>

</head>

<body>

<center><h4>File Uploading</h4></center>

<hr>

<form method="post" enctype="multipart/form-data">

<input type="file" name="uploadfile">

<input type="submit" name="Submit" value="Submit">

<input type="reset" name="Reset" value="Reset">

</form>

</body>

</html>

Output

Uploading files using Jsp

 

Uploading files using Jsp

Pr.Pg border                                              Next Pg