how can i make a servlet to validate a form with two file upload one image and other a pdf file. along with the other string parameters like name age gender etc.,

i need to validate the pdf and image files in servlet and save it in folder of my project, and the path and other parameters in database.

please help..!!

i need to do in servlets only first then will try in struts.

4 Years
Discussion Span
Last Post by subramanya.vl

Sorry i cannot help you,i still have a very long way to run to be right where you are.I hope you will find your answer.


i think following code might be useful or you can get the idea what to do.

%@page import="java.io.*, java.sql.*"%>

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;
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
    <b>You have successfully upload the file by the name of:</b>

First you need to declare content type as ""multipart/form-data" on your jsp.

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

then using simple html tag

<input type="file">

you can provide provision for file upload on jsp.

For validation you need to write java script code on the jsp.

In servlet you need to use package "org.apache.commons.fileupload" to handle the uploaded file.
below are the links that explains all i have mentioned above.


Even on apche's oficial site there are examples how to use "org.apache.commons.fileupload"

I have used this package in my project for similar requirement.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.