Struts2.0实现单个文件上传实例

时间:2022-02-23 15:17:07

一:FileUpload.jsp上传页

<%@ taglib prefix="s" uri="/struts-tags"%>

<body>
   <s:form action ="fileUpload" method ="POST" enctype ="multipart/form-data" >
        <s:file name ="myFile" label ="请选择文件" />   
        <s:submit />
    </s:form > 
 </body>

二:struts.xml配置

<action name ="fileUpload" class ="com.yysoft.uploadfile.FileUploadAction">
            <interceptor-ref name ="fileUpload">
                <param name ="allowedTypes">
                 image/bmp,image/png,image/gif,image/jpeg
                </param>
            </interceptor-ref >
           
            <interceptor-ref name ="fileUploadStack"/>
            <result name ="input">/FileUpload.jsp</result>
            <result name ="success">/uploadsuccess.jsp</result>
        </action>

三:FileUploadAction.java 编写

package com.yysoft.uploadfile;

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.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

 private static final long serialVersionUID = 163949767040255874L;

 private static final int BUFFER_SIZE = 16 * 1024 ;
   
    private File myFile;
    private String contentType;
    private String fileName;
    private String imageFileName;
   
    //必需要这样写 set?ContentType //取得文件类型
    //比如:myFile(jsp页面中的文件列表框名),则为 setMyFileContentType  --加上ContentType
    //如果是:uploadFile(jsp页面中的文件列表框名),则为setUploadFileContentType
    //必需要这样写 set?FileName //取得文件名称  --加上FileName
    //同理,要设置取得文件名刚这样写:setMyFileName 和 setUploadFileFileName; //取得文件名 
    public void setMyFileContentType(String contenType){
     this.contentType = contenType;
    }
    public void setMyFileFileName(String fileName){
     this.fileName = fileName;
    }
      
    public void setMyFile(File myFile)  {
        this.myFile = myFile;
   }
  
    public String getImageFileName()  {
        return imageFileName;
   }
   
   

 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)  {
        int pos = fileName.lastIndexOf( "." );
        return fileName.substring(pos);
   }

   @Override
    public String execute()throws Exception {
    try {
     System.out.println("上传来了....................");
     System.out.println("myFile======="+myFile);
     System.out.println("contentType======="+contentType);
     System.out.println("fileName========="+fileName);
     System.out.println("imageFileName========"+imageFileName);
        imageFileName = new Date().getTime() + getExtention(fileName);
        System.out.println("imageFileName:"+imageFileName);
        File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/images" ) + "/" + imageFileName);
        System.out.println(myFile);
        System.out.println(imageFile);
        copy(myFile, imageFile);
        return SUCCESS;
    } catch (Exception e) {
   e.printStackTrace();
   return INPUT;
    }
   }

}

四.struts.properties Struts的资源文件的配置

struts.multipart.saveDir = /tmp
struts.messages.error.uploading=上传文件失败
struts.messages.error.file.too.large=上传的文件太大
struts.messages.error.content.type.not.allowed=上传文件格式不正确
五:所用到的包

commons-fileupload-1.2.1.jar

commons-io-1.4.jar

commons-logging-1.0.4.jar

commons-logging-api-1.1.jar

freemarker-2.3.8.jar

jakarta.jar

ognl-2.6.11.jar

struts2-core-2.0.8.jar

xwork-2.0.4.jar