Java component smartupload implement upload file function _java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.