This short article describes how to upload a file using Apache Jakarta commons upload library.
1) Required downloads: Download the library from here. Keep the following JAR files in classpath: commons-fileupload-1.2.1.jar and commons-io-1.4.jar.
2) JSP/HTML code: To upload a file you need to set the encoding type in your form to multipart/form-data.
Give your users a file-select box to select the file :
input type="file" name="myFile" width="100"
You will most probably have other form fields like textbox, radio buttons etc. We have one text field with name 'user' in this form:
input maxlength="255" size="40" name="user"
3) Form data handling: Now you have sent the file and other form data, now we need to make use of this form data. Most probably you need to save the file at some location on the server.
One problem that many developers face is getting a null when they try to extract the value in form fields using request.getParameter(). So in our case, if you try doing this:
String user=request.getParameter("user");
you will find that 'user' has a null value.
But Apache Jakarta Commons Upload library gives us the way to overcome this problem also.
To handle the form data, I am using a servlet here. You may use a JSP, if you want, however that won't be a good design!
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
......
......
......
//Step 1: check if the request is a file upload request or not.
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//Step 2: parse the request.
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = new ArrayList();
try {
items = upload.parseRequest(request);
} catch (FileUploadException fue) {
fue.printStackTrace();
}
//Step 3:
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
// I am saving the files in WEB-INF/images folder.
try {
String FOLDER = getServletContext().getRealPath("/WEB-INF");
String imgName= item.getName();
int last=imgName.lastIndexOf("\\");
imgName=imgName.substring(last+1,imgName.length());
FOLDER = FOLDER.replace("WEB-INF", "images/"+imgName);
if (FOLDER != null) {
FOLDER = FOLDER.replace('\\', '/'). replace('/', File.separatorChar);
}
File file = new File(FOLDER);
item.write(file);
qb.setImage(FOLDER);
} catch (Exception e) {
e.printStackTrace();
}
} else { //Handle the textual form data.
//Get the name of the field
String name = item.getFieldName();
//Get the value of the field
String value = item.getString();
System.out.println("Name=" + name + " value=" + value);
if(name.equals("user"))
System.out.println("User ="+value);
}
}
.....
.....
.....
}
This is all that you need to upload a file. See it was so simple!
1) Required downloads: Download the library from here. Keep the following JAR files in classpath: commons-fileupload-1.2.1.jar and commons-io-1.4.jar.
2) JSP/HTML code: To upload a file you need to set the encoding type in your form to multipart/form-data.
Give your users a file-select box to select the file :
input type="file" name="myFile" width="100"
You will most probably have other form fields like textbox, radio buttons etc. We have one text field with name 'user' in this form:
input maxlength="255" size="40" name="user"
3) Form data handling: Now you have sent the file and other form data, now we need to make use of this form data. Most probably you need to save the file at some location on the server.
One problem that many developers face is getting a null when they try to extract the value in form fields using request.getParameter(
String user=request.getParameter("user");
you will find that 'user' has a null value.
But Apache Jakarta Commons Upload library gives us the way to overcome this problem also.
To handle the form data, I am using a servlet here. You may use a JSP, if you want, however that won't be a good design!
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
......
......
......
//Step 1: check if the request is a file upload request or not.
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
//Step 2: parse the request.
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = new ArrayList();
try {
items = upload.parseRequest(request);
} catch (FileUploadException fue) {
fue.printStackTrace();
}
//Step 3:
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
// I am saving the files in WEB-INF/images folder.
try {
String FOLDER = getServletContext().getRealPath("/WEB-INF");
String imgName= item.getName();
int last=imgName.lastIndexOf("\\");
imgName=imgName.substring(last+1,imgName.length());
FOLDER = FOLDER.replace("WEB-INF", "images/"+imgName);
if (FOLDER != null) {
FOLDER = FOLDER.replace('\\', '/'). replace('/', File.separatorChar);
}
File file = new File(FOLDER);
item.write(file);
qb.setImage(FOLDER);
} catch (Exception e) {
e.printStackTrace();
}
} else { //Handle the textual form data.
//Get the name of the field
String name = item.getFieldName();
//Get the value of the field
String value = item.getString();
System.out.println("Name=" + name + " value=" + value);
if(name.equals("user"))
System.out.println("User ="+value);
}
}
.....
.....
.....
}
This is all that you need to upload a file. See it was so simple!
Comments
File file = new File(FOLDER);
item.write(file);
qb.setImage(FOLDER);