Create a class UploadServlet
[Java]
Package org. action;
Import java. io. File;
Import java. io. IOException;
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. disk. DiskFileItemFactory;
Import org. apache. commons. fileupload. servlet. ServletFileUpload;
@ SuppressWarnings ("unchecked ")
Public class UploadServlet extends HttpServlet {
Private static final long serialVersionUID = 1L;
@ Override
Protected void doGet (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
DoPost (req, resp );
}
@ Override
Protected void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory ();
Factory. setSizeThreshold (8192 );
String tempPath = request. getSession (). getServletContext (). getRealPath ("/")
+ "/Upload ";
File file = new File (tempPath );
If (! File. isDirectory ()){
File. mkdirs ();
}
Factory. setRepository (file );
ServletFileUpload upload = new ServletFileUpload (factory );
List <FileItem> items;
Try {
Items = upload. parseRequest (request );
Iterator <FileItem> itr = items. iterator ();
While (itr. hasNext () {// process each form field in sequence
FileItem item = (FileItem) itr. next ();
If (! Item. isFormField () {/* determine whether it is a Form Control (not a File control). If it is not a Form Control, upload the File */
File savedFile = new File (tempPath, item. getName ());
Item. write (savedFile );
} Else {/* if it is a Form Control, save its value */
System. out. println (item. getFieldName () + "-->" + item. getString ());
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
Configure in web. xml:
[Html]
<Servlet>
<Servlet-name> upload </servlet-name>
<Servlet-class> org. action. UploadServlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> upload </servlet-name>
<Url-pattern>/upload </url-pattern>
</Servlet-mapping>
Page code:
[Java]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">
<Title> My JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
</Head> www.2cto.com
<Body>
<Form action = "<% = basePath %> upload" method = "post" enctype = "multipart/form-data">
<Input type = "file" name = "file"/>
<Input value = "name" name = "name" type = "text"/>
<Input type = "submit"/>
</Form>
</Body>
</Html>
Author: hfmbook