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.

Recommended Answers

All 3 Replies

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;
saveFile="C:/UploadedFiles/"+saveFile;
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
    <b>You have successfully upload the file by the name of:</b>
     <%
 out.println(saveFile);
   }
%>

@abevenkat
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.

http://commons.apache.org/fileupload/using.html
http://myjavaswtech.blogspot.in/2012/05/servlet-file-upload-example.html

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.