Multiple file uploads

Source: Internet
Author: User

Preparing files

1. Upload multiple file pages

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <% @taglib uri= "/struts-tags" prefix= "s"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

2. Configure the appropriate action in the Struts.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "http://struts.apache.org/dtds/ Struts-2.0.dtd "><struts><!--dynamic method call--     <constant name=" Struts.devmode "value=" false "/>            <package name= "Default" namespace= "/" extends= "Struts-default" >            <action name= "upload" class= " Cn.happy.action.UploadAction "method=" execute ">        <param name=" Savepath ">/image</param>           <result name= "Success" >/upload/success.jsp</result>        </action>    </package></ Struts>

3. Find the corresponding class based on the action node

Package Cn.happy.action;import Java.io.file;import Java.io.fileinputstream;import java.io.fileoutputstream;import Org.apache.struts2.servletactioncontext;import Com.opensymphony.xwork2.actionsupport;public class UploadAction Extends Actionsupport {/*** * multi-File Upload * @return *///properties of uploaded files private file[] upload;//upload file type private string[] Uploadcontentty pe;//upload file name private string[] uploadfilename;//the address of the uploaded file is private string Savepath; @Overridepublic string Execute () throws Exception{byte[] buffer=new byte[1024];for (int i=0;i<upload.length;i++) {//Set up stream of uploaded files FileInputStream fis=new FileInputStream (Getupload () [i]);//build the output stream of the uploaded file, getimagefilename () [I]fileoutputstream fos=new FileOutputStream (    Getsavepath () + "\ \" +getuploadfilename () [i]);    int length=fis.read (buffer);    while (length>0) {fos.write (buffer,0,length);    Length=fis.read (buffer); } fis.close (); Fos.flush (); Fos.close ();} return SUCCESS;} Public file[] Getupload () {return upload;} public void Setupload (file[] upload) {this.upload = uPload;} Public string[] Getuploadcontenttype () {return uploadcontenttype;} public void Setuploadcontenttype (string[] uploadcontenttype) {this.uploadcontenttype = Uploadcontenttype;} Public string[] Getuploadfilename () {return uploadfilename;} public void Setuploadfilename (string[] uploadfilename) {this.uploadfilename = Uploadfilename;} Public String Getsavepath () {return Servletactioncontext.getservletcontext (). Getrealpath (Savepath);} public void Setsavepath (String savepath) {this.savepath = Savepath;}}

4. If successful, find a success page

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <% @taglib uri= "/struts-tags" prefix= "s"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

  

Multiple file uploads

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.