For the introductory knowledge read my previous post on file upload introduction before servlet 3.0 came to rescue...It is advisable to read it thoroughly or if u avoid the basics, you might spend 50 minutes on a bug trying to skip a 5 min.
Libraries to the rescue.....
There are a lot of libraries that are available free to download that eases your file upload coding task!..the 2 libraries I found to be pretty flexible are
1. Apache Commons File upload API
2. Oreilly's file uploading library.
Since, Apache commons file upload api is pretty popular and there are a lot of codes available over the internet regarding how to use it...I'll better focus on Oreilly's file uploading library that works magic and is pretty impressive!
All you need to do is -
1. Download the library from http://www.servlets.com/cos/ and include it in your class path the way you import external jar/zip files from you IDE, or using command prompt.
2. Make an HTML file as I suggested in my previous post.
3. Make a servlet (don't use JSP for backend logic...remember I told you...it's a bad practice. use JSP for front end logic only!) that actually uses it to write the file upload logic.
First thing's first, Download the file (cos-26Dec2008.zip) from the link. You'll find the file download link at the bottom of Oreilly's page and iclude it in your project's class path.
Now, lets get started with the servlet....You may like to see the documentation before you get started (and you should before blindly copy-pasting the use of a class and it's methods). The documentation goes here: JAVADOC
I'll be going to use the following constructor :
MultipartRequest(javax.servlet.http.HttpServletRequest request, java.lang.String saveDirectory, int maxPostSize)
There are three parameters of this function, the first one being the HttpServletRequest. The second one defines the path of the directory where the file will get saved in the server hard-disk, after getting uploaded. The third parameter constraints the maximum size of file upload.
So, we can't do much about the first parameter. The second parameter needs to be specified and is the most important. Remember that the path will not be any machine specific path or a client side path. It should be a generalized server path where your project resides.To get the generalized server path use:
String saveFile=this.getServletContext().getRealPath("\\");
For the 3rd parameter, it's upto your will. For eg, if u want to limit max file size to 4MB u should write "4*1024*1024" as the third constructor.
Now if we use these parameters and create an object of the "MultipartRequest" class your file upload code is ready. The only thing left is to retrieve the file name. We'll use the method -
getOriginalFileName(java.lang.String name)
The parameter to this method is the "name" you provide to < input type="file" name="file">.
The entire servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import javax.servlet.http.HttpServlet;
import com.oreilly.servlet.*;
/**
*
* @author soumya
*/
public class multiserv extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
String saveFile=this.getServletContext().getRealPath("\\");
MultipartRequest mr=new MultipartRequest(request,saveFile,4*1024*1024);
System.out.print(saveFile); // see the path in your console for reference
String filename=mr.getOriginalFileName("file");
response.sendRedirect("home.jsp");
}
catch(Exception e)
{
out.print(e.getMessage());
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
So that's it...you got the tutorial for uploading file...and that too without servlet 3.0!...cheers!! :) Hope you liked it....