You can use apache-commons-fileupload with apache-commons-io component to upload files
If you not have any example to use it
can find it on the official site http://jakarta.apache.org/commons/fileupload/using.html
or use my implement class from apache example
see hereinafter
you must to change some code for normal use in your projects
/**
* $Id: Upload.java , 2006-5-21 20:10:58 WilliamRaym Exp $
* Copyright By WilliamRaym 2006-2006
*/
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.LogFactory;
import com.sun.mail.iap.Response;
import com.williamraym.properties.Property;
public class Upload extends HttpServlet {
private static final int SIZEMAX = 10 * 1024 * 1024;// 10MB
private static final int SIZETHRESHOLD = 1024;// 1MB
public Upload() {
}
private org.apache.commons.logging.Log log = LogFactory.getLog(this.getClass());
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
response.setHeader("cache","none");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out
.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("UTF-8");
boolean writeToFile = true;
DiskFileItemFactory factory = new DiskFileItemFactory();
String tempFilePath = new String(Property
.getProperty("williamraym.home.dir")
+ Property.getFileSeparator() + "temp");
com.williamraym.file.File.createDirectory(tempFilePath);
factory.setRepository(new File(tempFilePath));
factory.setSizeThreshold(SIZETHRESHOLD);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(SIZEMAX);
log.debug(SIZEMAX);
upload.setHeaderEncoding("UTF-8");
try {
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
out.println("1" + item.getFieldName());
out.println("4"
+ new String(item.getString()
.getBytes("ISO-8859-1"), "UTF-8"));
log.info(item.getFieldName());
log.info(new String(
item.getString().getBytes("ISO-8859-1"), "UTF-8"));
} else {
String n = item.getName();
String filename = n.substring(n.lastIndexOf("\\") + 1, n
.length());
String uploadFilePath = Property
.getProperty("williamraym.home.dir")
+ Property.getFileSeparator()
+ "upload"
+ Property.getFileSeparator();
com.williamraym.file.File.createDirectory(uploadFilePath);
String uploadFile = uploadFilePath + filename;
log.info(uploadFilePath);
if (writeToFile) {
File uploadedFile = new File(uploadFile);
try {
item.write(uploadedFile);
} catch (Exception e) {
log.info(e.getMessage());
e.printStackTrace();
}
} else {
InputStream uploadedStream = item.getInputStream();
uploadedStream.close();
}
}
item.delete();
}
} catch (FileUploadException fue) {
fue.getMessage();
log.info(fue.getMessage());
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}