Using JSP and Serlvet to achieve the simplest upload, for your reference, the specific contents are as follows
1, page index.jsp
<%@ page language= "java" pageencoding= "UTF-8"%> <% String Path = Request.get
ContextPath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
2, the action jump to the upload servlet, so to web.xml inside configuration, Web.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "http://java.sun.com/xml/ns/"
Java ee "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemalocation=" http://java.sun.com/ Xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
<servlet>
< Servlet-name>uploadaction</servlet-name>
<servlet-class>com.pop.action.smartuploadaction </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>uploadaction </servlet-name>
<url-pattern>/upload/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</ Welcome-file-list>
3, mapped to the action file, Smartuploadaction.java:
Package com.pop.action;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Javax.servlet.jsp.JspFactory;
Import Javax.servlet.jsp.PageContext;
Import Com.soft4j.httpupload4j.Request;
Import Com.soft4j.httpupload4j.SmartUpload;
Import com.soft4j.httpupload4j.SmartUploadException;
public class Smartuploadaction extends HttpServlet {private static final long serialversionuid = -8610555375032925108l; @Override protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, Ioexcep
tion {req.setcharacterencoding ("utf-8");
Resp.setcharacterencoding ("Utf-8");
Smartupload su = new Smartupload ();
Due to the transmission of multipart/form-data cause req can not be used, so use smartupload generated request request Reqest = Su.getrequest (); Get PageContext object PageContext PageContext = Jspfactory.getdefaultfactory ()
. Getpagecontext (This, req, resp, NULL, True, 8192, true);
Su.initialize (PageContext);
try {su.upload ();
Upload to the upload directory of this project Su.save ("Upload");
catch (Smartuploadexception e) {e.printstacktrace ();
///Use the Reqest object generated by Smartupload to get the parameter String uname = Reqest.getparameter ("uname") passed by the page;
System.out.println (uname);
}
}
Finally note: The component package used is smartupload.zip.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.