Struts upload file Dynavalidatorform instance

Source: Internet
Author: User

One, the relevant jar package
A jar package for an empty struts project:
Additional two jar packages for uploading files:

Second, the page

1. Upload page upload.jsp

1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="Utf-8"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">7 <title>Insert Title here</title>8 </Head>9 <Body>Ten <formAction= "Upload.do"Method= "POST"enctype= "Multipart/form-data"> One <inputtype= "File"name= "Upload_file"value=""ID= "Upload_file"style= "height:20px;"> A <inputtype= "Submit"ID= "Submitbutton"value= "Submit"> - </form> - </Body> the </HTML>

Note: A. The form form should have the attribute enctype= "Multipart/form-data".
B. Select file for upload file input Name property name= "Upload_file" to match Form-property name value under Form-bean below.

2, Struts-config.xml

1 <?XML version= "1.0" encoding= "Utf-8"?>2 3 <!DOCTYPE struts-config Public4 "-//apache software foundation//dtd Struts Configuration 1.3//en"5 "Http://struts.apache.org/dtds/struts-config_1_3.dtd">6 <Struts-config>7     <Form-beans>8         <Form-beanname= "Upload"9 type= "Org.apache.struts.validator.DynaValidatorForm">Ten             <Form-propertyname= "Upload_file"type= "Org.apache.struts.upload.FormFile" /> One         </Form-bean> A     </Form-beans> -  -     <action-mappings> the         <ActionPath= "/upload"type= "com. Uploadaction "name= "Upload" - Scope= "Request"> -             <forwardname= "sucess"Path= "/sucess.jsp"></forward> -         </Action> +     </action-mappings> -  +     <Plug-inClassName= "Org.apache.struts.validator.ValidatorPlugIn"> A         <Set-property Property= "Pathnames" at value= "/org/apache/struts/validator/validator-rules.xml, - /web-inf/validation.xml " /> -     </Plug-in> -  - </Struts-config>

Note: The Name property value of line 8th Form-bean is the same as the name value of line 15th action.

3, Uploadaction.java

1  Packagecom;2 3 ImportJava.io.BufferedOutputStream;4 ImportJava.io.File;5 Importjava.io.FileNotFoundException;6 ImportJava.io.FileOutputStream;7 Importjava.io.IOException;8 ImportJava.io.InputStream;9 Ten Importjavax.servlet.http.HttpServletRequest; One ImportJavax.servlet.http.HttpServletResponse; A  - Importorg.apache.struts.action.Action; - ImportOrg.apache.struts.action.ActionForm; the ImportOrg.apache.struts.action.ActionForward; - Importorg.apache.struts.action.ActionMapping; - ImportOrg.apache.struts.action.DynaActionForm; - ImportOrg.apache.struts.upload.FormFile; +  -  Public classUploadactionextendsAction { +  A      PublicActionforward Execute (actionmapping mapping, actionform form, at httpservletrequest request, httpservletresponse response) { -  -Dynaactionform DynaForm =(dynaactionform) Form; -         ////This upload_file corresponds to configure Form-property and JSP page to select file input -Formfile file = (formfile) dynaform.get ("Upload_file"); -          inString Loalpath =NULL; -String FilePath =NULL; to         if(File! =NULL&&!file.tostring (). Trim (). Equals ("")) { +Loalpath =file.getfilename (); -             Try { theInputStream in =File.getinputstream (); *                 intLength = 0; $String Path = This. Getservlet (). Getservletcontext (). Getrealpath ("/upload") + "\ \" +Loalpath;Panax NotoginsengFile OutFile =NewFile (path); -                 if(!Outfile.getparentfile (). exists ()) the outfile.getparentfile (). Mkdirs (); +                 if(Outfile.exists ()) { A Outfile.delete (); the                 } +Bufferedoutputstream out =NewBufferedoutputstream ( -                         NewFileOutputStream (OutFile)); $                 byte[] buffer =New byte[4096]; $                  while(length = in.read (buffer))! =-1) { -Out.write (buffer, 0, length); -                 } the in.close (); - out.close ();Wuyi System.out.println (filePath); the}Catch(FileNotFoundException e) { -                 //TODO auto-generated Catch block Wu e.printstacktrace (); -}Catch(IOException e) { About                 //TODO auto-generated Catch block $ e.printstacktrace (); -             } -         } -         returnMapping.findforward ("sucess"); A     } +  the}

Struts upload file Dynavalidatorform instance

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.