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 > 

發佈了36 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章