Struts 1.x FormFile UploadFile

 有关文件上传的控件可能非常多,比如有用于学习的SmartUpload、性能非常好的COS组件、有Apache的FileUpload组件、有Struts的FormFile组件、有Spring上传文件,还有其他一些开发的控件等等。假如你是用Struts,那基于Struts FormFile组件应该是一个不错的选择。
      Struts FormFile跟Struts ActionForm结合得非常好,使用起来也非常简单,但是,对于同时上传多个附件,那就有点麻烦了!不管怎么样,它还是一个好东西,关键看大家怎么看待他了!
      第一步,我们创建一个新的Struts工程,创建一个FileUploadForm,FileUploadForm如下所示:

 程序代码
package zizz.struts;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

/**
* 基于Struts FormFile的文件上传.
* @author chen yuzhe
*
*/
public class FileUploadForm extends ActionForm{
    
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -7794872310588861856L;
    
    /**
     * 上传的文件 
     */
    private FormFile uploadFile;

    public FormFile getUploadFile() {
        return uploadFile;
    }

    public void setUploadFile(FormFile uploadFile) {
        this.uploadFile = uploadFile;
    }
    
}



      第二步,创建一个Action,用于处理上传的文件及保存在服务器的路径,FileUploadAction文件内容如下

 程序代码
package zizz.struts;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
* 文件上传的Action,该类用于处理FormFile及将上传的文件保存到磁盘的指定位置.
* @author ZIZZ
*
* @Create-Time:2007-12-26 下午03:35:37
*/
public class FileUploadAction extends Action {
    
    /**
     * 上传的文件保存在服务器的路径
     */
    private static String UPLOAD_FILE_PATH = "c:/income/";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        FileUploadForm uploadForm = (FileUploadForm)form;
        //得到上传的文件
        FormFile uploadFile = uploadForm.getUploadFile();
        //得到文件名
        String fileName = uploadFile.getFileName();
        //得到文件大小
        int fileSize = uploadFile.getFileSize();
        System.out.println("FileName = " + fileName);
        System.out.println("FileSize=" + fileSize);
        boolean result = true;
        try{
            //得到文件的输入流
            InputStream is = uploadFile.getInputStream();
            //上传文件
            uploadFile(fileName,is);
        }catch(IOException ex){
            ex.printStackTrace();
            //假如上传文件失败,设置一个失败的标记位
            result = false;
        }
        if(result){
            return mapping.findForward("success");
        } else {
            return mapping.findForward("fail");
        }        
    }
    
    /**
     * 上传文件
     * @param fileName
     * @param is
     * @throws IOException
     */
    private void uploadFile(String fileName,InputStream is) throws IOException{
        OutputStream os = new FileOutputStream(UPLOAD_FILE_PATH + fileName);
        //8k缓存数据
        byte[] buffer = new byte[1024 * 8];
        //设置读进缓存的字节数
        int len;
        while((len=is.read(buffer))!=-1){
            //将缓存数据写入磁盘
            os.write(buffer,0,len);
        }
        //关闭输出流
        os.close();
        //关闭输入流
        is.close();
    }

}



     第三步,编写文件上传的jsp文件

 程序代码
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>文件上传表单</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
    <html:form action="/upload" enctype="multipart/form-data">
        上传文件:<html:file property="uploadFile"></html:file> 
        <html:submit value="上传文件"></html:submit>
    </html:form>
  </body>
</html>


      第四步,配置struts-config.xml文件 
 程序代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans>
      <form-bean name="fileUploadForm" type="zizz.struts.FileUploadForm"></form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
      <action path="/upload" name="fileUploadForm" scope="request" type="zizz.struts.FileUploadAction">
          <forward name="success" path="/success.jsp" redirect="true"></forward>
          <forward name="fail" path="/fail.jsp" redirect="true"></forward>
      </action>
  </action-mappings>
  <message-resources parameter="zizz.struts.ApplicationResources" />
</struts-config>

 

你可能感兴趣的:(apache,html,jsp,struts,OS)