源码1:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jspsmart.upload.*;public class servletUpload extends HttpServlet {private ServletConfig config;
/**
* Init the servlet
*/
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}final public ServletConfig getServletConfig() {
return config;
}
/**
* Handles GET requests
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY BGCOLOR='white'>");
out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
out.println("<HR><BR>");
out.println("The method of the HTML form must be POST.");
out.println("</BODY>");
out.println("</HTML>");
}/**
* Handles POST requests
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<BODY BGCOLOR='white'>");
out.println("<H1>jspSmartUpload : Servlet Sample</H1>");
out.println("<HR>");// Variables
int count=0;
SmartUpload mySmartUpload = new SmartUpload();try {
// Initialization
mySmartUpload.initialize(config,request,response);// Upload
mySmartUpload.upload();// Save the file with the original name
// in a virtual path of the web server
count = mySmartUpload.save(mySmartUpload.getRequest().getParameter("PATH"));// Display the result
out.println(count + " file uploaded.");} catch (Exception e){
out.println("Unable to upload the file.<br>");
out.println("Error : " + e.toString());
}out.println("</BODY>");
out.println("</HTML>");
          }
/**
* Destroy the servlet
*/
public void  destroy () {
}} 
源码2:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class UploadTest extends HttpServlet
{  public void doGet(HttpServletRequest req,
                    HttpServletResponse resp)
    throws ServletException, java.io.IOException
    {
      // Set the content type of the response
      resp.setContentType("text/html");      // Get the PrintWriter to write the response
      java.io.PrintWriter out = resp.getWriter();      // Create the HTML form
      out.println("<html>");
      out.println("<head>");
      out.println("<title>Send Email</title>");
      out.println("<center><h2>Send Email to Karl Moss</h2>");
      out.println("<br><form method=POST action=\"" +
                  req.getRequestURI() + "\" ENCTYPE=\"multipart/form-data\" >");
      out.println("<table>");
      out.println("<tr><td>Name:</td>");
      out.println("<td><input type=text name=name size=30></td></tr>");
      out.println("<tr><td>File:</td>");
      out.println("<td><input type=file name=img></td></tr>");
      out.println("</table><br>");
      out.println("<input type=submit value=\"Send\">");
      out.println("<input type=reset value=\"Reset\">");
      out.println("</form></center></body></html>");
      
      // Wrap up
      out.println("</body>");
      out.println("</html>");
      out.flush();
    }
  
  /**
    * <p>Performs the HTTP POST operation
    *
    * @param req The request from the client
    * @param resp The response from the servlet
    */
  public void doPost(HttpServletRequest req,
                    HttpServletResponse resp)
    throws ServletException, java.io.IOException
    {
      // Set the content type of the response
      resp.setContentType("text/html");      // Create a PrintWriter to write the response
      java.io.PrintWriter out =
        new java.io.PrintWriter(resp.getOutputStream());//指定上传文件最大字节 
MultipartRequest multi = new MultipartRequest(req, ".", 2*1024); 
//或用默认2M,MultipartRequest multi = new MultipartRequest(req, "."); out.println("Params:"); 
Enumeration params = multi.getParameterNames(); 
while (params.hasMoreElements()) { 
  String name = (String)params.nextElement(); 
    String value = multi.getParameter(name); 
    out.println(name + " = " + value); 
    }
  out.println(); 
    
  out.println("Files:"); 
  Enumeration files = multi.getFileNames(); 
  while (files.hasMoreElements()) { 
    String name = (String)files.nextElement(); 
    String filename = multi.getFilesystemName(name); 
    String type = multi.getContentType(name); 
    File f = multi.getFile(name); 
    out.println("name: " + name); 
    out.println("filename: " + filename); 
    out.println("type: " + type); 
    if (f != null) { 
      out.println("f.toString(): " + f.toString()); 
      out.println("f.getName(): " + f.getName()); 
      out.println("f.exists(): " + f.exists()); 
      out.println("f.length(): " + f.length()); 
      out.println(); 
    } 
  } 
      
      // Wrap up
      out.flush();
    }  /**
    * <p>Initialize the servlet. This is called once when the
    * servlet is loaded. It is guaranteed to complete before any
    * requests are made to the servlet
    *
    * @param cfg Servlet configuration information
    */  public void init(ServletConfig cfg)
    throws ServletException
    {
      super.init(cfg);
    }  /**
    * <p>Destroy the servlet. This is called once when the servlet
    * is unloaded.
    */  public void destroy()
    {
      super.destroy();
    }}