热门文章 | 热门软件| 热门源码 | 热门电影 | 知识库 | 联系我们
软件 源码 教程 影视 健康 招聘
  HTML | JavaScript | ASP | PHP | JSP | NET | VB | VC | VF | Windows | Linux | Mysql | Mssql | Oracle | Struts 
当前位置: 创世纪计算机资源网 -> 文章频道 ->spring 
站内搜索:
关于在一个form表单里同时上传多个文件和文本信息的解决方案(1)
作者:luna8418 来源:blog 整理日期:2007-3-13
运行环境:tomcat5.0.30+springframework

步骤:
1。定义一个form表单index.jsp和error.jsp
**************************************index.jsp*************************************************
<%@ page contentType="text/html; charset=GBK" %>

<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="up.htm" enctype="multipart/form-data">
<input type="file" name="file"/><br>
<input type="file" name="file1"/><br>
<TEXTAREA name="description" ROWS=5 COLS=30>
</TEXTAREA><br>
<input type="submit"/>
</form>
</body>
</html>
**************************************error.jsp**********************************
<%@ page language="java" isErrorPage="true" %>

<head><title>Doh!</title></head>

An Error has occurred in this application.  
2。定义控制类FileUploadController和commandClass UploadBean
**************************FileUploadController.java*************************************************************
public class FileUploadController extends SimpleFormController {
    private static Log log = LogFactory.getLog(getClass());
    public ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse res, Object obj, BindException errors)
            throws Exception {
        UploadBean bean = (UploadBean) obj;
        if(bean == null)return new ModelAndView("error");
        
        byte[] file = bean.getFile();
        byte[] file1 = bean.getFile1();
        String[] d= bean.getDescription();
        
        ==================================================
        在这里就可以对上面得到的数据进行处理,可以把上传的文件内容保存到本地或
        者数据库,对文本区的说明文字也可以进行处理。其最大的优点就是把request里面
        的数据绑定到了commandClass,有利于使用spring的验证机制和其他的辅助功能。    
        
        ==================================================
        
    }
    //注册一个spring的编辑器非常重要,没有这个方法,上传将不能进行
    protected void initBinder(HttpServletRequest request,
            ServletRequestDataBinder binder) throws ServletException {
        binder.registerCustomEditor(byte[].class,
                new ByteArrayMultipartFileEditor());
    }
    
}
****************************************UploadBean.java***********************************



public class UploadBean {
    private String[] description;
    private byte[] file;
    private byte[] file1;
    public void setDescription(String[] description) {
        this.description = description;
    }

    public String[] getDescription() {
        return this.description;
    }
    public void setFile(byte[] file) {
        this.file = file;
    }

    public byte[] getFile() {
        return file;
    }
    public void setFile1(byte[] file1){
        this.file1 = file1;
    }
    public byte[] getFile1(){
        return file1;
    }
}

================================================
[1]  [2]  
相关文章