struts实现上传的例子

制作环境:
    Eclipse3.1.1 、 MyEclipse4.1 、 Tomcat5.5.x
 
步骤:
    新建工程 upload
 
    添加 Struts 框架,使用 Struts1.2 版本
 
    新建 一个 jsp + action 这里将使用动态的 ActionForm
 
    在新建表单对像时使用一个文件名和一个文件对像进行提交
 
    修改动态 From 的类型为 org.apache.struts.upload.FormFile
 
    < form-bean name = "upfileForm" type = "org.apache.struts.action.DynaActionForm" >
      < form-property name = "filename" type = "java.lang.String" />
      < form-property name = "filedata" type = "java.lang.String" />
    </ form-bean >
 
    改为
 
    < form-bean name = "upfileForm" type = "org.apache.struts.action.DynaActionForm" >
      < form-property name = "filename" type = "java.lang.String" />
      < form-property name = "filedata" type = " org.apache.struts.upload.FormFile " />
    </ form-bean >
 
    修改 upfile.jsp 文件,在<Form> 中加入 enctype = "multipart/form-data" ,这样才可以提交二进制类型的文件
 
    修改文件第一行代码
       <%@ page language = "java" %>
       改为
    <%@ page contentType = "text/html;charset=UTF-8" language = "java" %>
       这里将使用 UTF-8 的编码格式
 
    修改 upfileAction.java 文件,修改后的内容如下:
 
package com.test.struts.action;
 
import java.io.FileNotFoundException;
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.action.DynaActionForm;
import org.apache.struts.upload.FormFile;
 
/**
 * MyEclipse Struts
 * Creation date: 07-05-2006
 *
 * XDoclet definition:
 * @struts.action path="/upfile" name="upfileForm" input="/upfile.jsp" scope="request" validate="true"
 */
public class UpfileAction extends Action {
 
    // --------------------------------------------------------- Instance Variables
 
    // --------------------------------------------------------- Methods
 
    /**
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(
       ActionMapping mapping,
       ActionForm form,
       HttpServletRequest request,
       HttpServletResponse response) {
       DynaActionForm upfileForm = (DynaActionForm) form;
       // 声明并获取对像
                   String filename = upfileForm.getString("filename");
        // 输出文件名
                   System.out.println(filename);
        FormFile filedata = (FormFile) upfileForm.get("filedata");
        // 取当前系统路径
                   String filePath = request.getRealPath("/");
        try {
            // 转换文件为数据流
                            InputStream stream = filedata.getInputStream();
            // 建立输出流
                            OutputStream bos = new FileOutputStream(filePath + "/" +
                    filedata.getFileName());
            // 将文件写入网站根目录下
                            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
            bos.write(buffer, 0, bytesRead);
            }
            bos.close();
            stream.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 返回到提交页面
                   return mapping.getInputForward();
    }
 
}
 
    现在可以进行测试了
 
    这时将会发现,提交的文件及文件名称都是乱码!下面将解决乱码
 
    增加一个过滤器,过滤器的代码请查看包中的具体文件
 
    在 web.xml 文件中加入以下配置内容,过滤器的编码设置为 UTF-8
       < filter >
       < filter-name > Set Character Encoding </ filter-name >
       < filter-class > com.test.SetCharacterEncodingFilter </ filter-class >
       < init-param >
           < param-name > encoding </ param-name >
           < param-value > UTF-8 </ param-value >
       </ init-param >
    </ filter >
    < filter-mapping >
       < filter-name > Set Character Encoding </ filter-name >
       < url-pattern > /* </ url-pattern >
    </ filter-mapping >
    < filter-mapping >
       < filter-name > Set Character Encoding </ filter-name >
       < servlet-name > action </ servlet-name >
    </ filter-mapping >
 
    配置 Tomcat 的 server.xml 文件,文件在 Tomcat_Home/conf 中
 

你可能感兴趣的:(java,apache,tomcat,bean,struts)