天天看點

struts2 檔案上傳(指定上傳圖檔,單檔案上傳和批量上傳放在一個jsp和Action中)

1.添加jar

Commons-fileupload.jar,

Commons-io.jar

2.定義Action類

package com.test.action;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpAction extends ActionSupport {

 private static final long serialVersionUID = 572146812454l;

 private static final int BUFFER_SIZE = 16 * 1024;

 //單檔案上傳

 private File myFile;

 private String contentType;

 private String fileName;

 private String imageFileName;

 private String caption;

 //批量上傳檔案

  private List < File > uploads = new ArrayList < File > ();

     private List < String > uploadFileNames = new ArrayList < String > ();

     private List < String > uploadContentTypes = new ArrayList < String > ();

     public List < File > getUpload() {

         return this .uploads;

    } 

     public void setUpload(List < File > uploads) {

         this .uploads = uploads;

    } 

     public List < String > getUploadFileName() {

         return this .uploadFileNames;

    } 

     public void setUploadFileName(List < String > uploadFileNames) {

         this .uploadFileNames = uploadFileNames;

    } 

     public List < String > getUploadContentType() {

         return this .uploadContentTypes;

    } 

     public void setUploadContentType(List < String > contentTypes) {

         this .uploadContentTypes = contentTypes;

    } 

 public void setMyFileContentType(String contentType) {

  this.contentType = contentType;

 }

 public void setMyFileFileName(String fileName) {

  this.fileName = fileName;

 }

 public void setMyFile(File myFile) {

  this.myFile = myFile;

 }

 public String getImageFileName() {

  return imageFileName;

 }

 public String getCaption() {

  return caption;

 }

 public void setCaption(String caption) {

  this.caption = caption;

 }

 private static void copy(File src, File dst) {

  try {

   InputStream in = null;

   OutputStream out = null;

   try {

    in = new BufferedInputStream(new FileInputStream(src),

      BUFFER_SIZE);

    out = new BufferedOutputStream(new FileOutputStream(dst),

      BUFFER_SIZE);

    byte[] buffer = new byte[BUFFER_SIZE];

    while (in.read(buffer) > 0) {

     out.write(buffer);

    }

   } finally {

    if (null != in) {

     in.close();

    }

    if (null != out) {

     out.close();

    }

   }

  } catch (Exception e) {

   e.printStackTrace();

  }

 }

 private static String getExtention(String fileName) {

  System.out.println("filename="+fileName);

  int pos = fileName.lastIndexOf(".");

  return fileName.substring(pos);

 }

 @Override

 public String execute() throws InterruptedException {

  SaveOneFile(myFile,fileName);//儲存單檔案上傳

  for(int i=0;i<uploadFileNames.size();i++)

  {

   Thread.sleep(200);//為了不使檔案名重複

   SaveOneFile(uploads.get(i),uploadFileNames.get(i));//儲存單檔案上傳

  }

  return SUCCESS;

 }

 private void SaveOneFile(File file,String fileName)

 {

  imageFileName = new Date().getTime() + getExtention(fileName);

  File imageFile = new File(ServletActionContext.getServletContext()

    .getRealPath("UploadImages")

    + "/" + imageFileName);

  copy(file, imageFile);

 }

}

(注意:儲存檔案的目錄在Web-Inf下名稱為:UploadImages

)

3.定義Struts.xml

 <action name ="fileUpload" class ="com.test.action.FileUpAction" > 

            <interceptor-ref name ="fileUpload" >

             <!-- 機關為位元組 這裡的大小是指每個檔案上傳的大小,若多個檔案上傳指每一個檔案的大小 -->

       <param name="maximumSize">4000000</param>

            <!--指定上傳的檔案類型-->

              <param name ="allowedTypes" > 

                    image/bmp,image/png,image/gif,image/pjpeg

                </param> 

            </interceptor-ref> 

            <interceptor-ref name="defaultStack"></interceptor-ref>

            <result name ="success" > /FileUp/ShowUpload.jsp </result > 

        </action > 

4.定義jsp檔案

 <s:form action ="fileUpload" theme="simple" method ="POST" enctype ="multipart/form-data" > 

        <s:file name ="myFile" label ="Image File" /> <br>

        <s:textfield name ="caption" label ="Caption" />   <br>   

        <br>

        批量上傳<br>

        <s:file label ="File (1)" name ="upload" /> <br>   

    <s:file label ="File (2)" name ="upload" /> <br>   

    <s:file label ="FIle (3)" name ="upload" /> <br>   

        <s:submit /> 

    </s:form >