Java

本类阅读TOP10

·使用MyEclipse开发Struts框架的Hello World!(录像1)
·hibernate配置笔记
·AOP编程入门--Java篇
·linux下Tomcat 5.0.20 与 Apache 2 安装/集成/配置
·在win2003下整合了整合Tomcat5.5+ apache_2.0.53+ mod_jk_2.0.47.dll
·构建Linux下IDE环境--Eclipse篇
·Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)
·ASP、JSP、PHP 三种技术比较
·Tomcat5.5.9的安装配置
·AWT GUI 设计笔记(二)

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
用JBuilder2005开发spring MVC应用-multipart (fileupload)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

JBuilder2005开发spring MVC应用-multipart (fileupload)

高科华

 

作者简介:高科华,南京航空学院计算数学专业硕士,有十年以上的企业信息化工作经验。目前的研究兴趣,J2EE企业应用、ERP软件研发、数据仓库系统研发。

1.   按照JBuilder2005开发spring MVC应用”一文建立基本的spring应用

2.       增加commons-fileupload.jar类库

3.       增加两个类文件

 

/src/fileupload/ FileUploadController.java

package fileupload;

 

import java.io.*;

 

import javax.servlet.*;

import javax.servlet.http.*;

 

import org.springframework.validation.*;

import org.springframework.web.bind.*;

import org.springframework.web.multipart.support.*;

import org.springframework.web.servlet.*;

import org.springframework.web.servlet.mvc.*;

 

// snippet from FileUploadController

public class FileUploadController extends SimpleFormController {

    protected ModelAndView onSubmit(

            HttpServletRequest request,

            HttpServletResponse response,

            Object command,

            BindException errors) throws ServletException, IOException {

        // cast the bean

        FileUploadBean bean = (FileUploadBean) command;

        // let's see if there's content there

        byte[] file = bean.getFile();

 

        if (file.length == 0) {

            // hmm, that's strange, the user did not upload anything

            return new ModelAndView(this.getFormView());

        }

        // well, let's do nothing with the bean for now and return:

        File myFile = new File("myfile.txt");

        myFile.createNewFile();

 

        try {

            return super.onSubmit(request, response, command, errors);

        } catch (Exception ex) {

            return null;

        }

    }

 

    protected void initBinder(

            HttpServletRequest request,

            ServletRequestDataBinder binder) throws ServletException {

        // to actually be able to convert Multipart instance to byte[]

        // we have to register a custom editor (in this case the

        // ByteArrayMultipartEditor

        binder.registerCustomEditor(byte[].class,

                                    new ByteArrayMultipartFileEditor());

        // now Spring knows how to handle multipart object and convert them

    }

}

上载文件时如果选定了要上载的文件,file.length就不为0,否则file.length0。上载文件的内容保存在byte[] file中,本例子没有对上载的文件进行处理。

 

 

 

/web-inf/fileupload/FileUploadBean.java

package fileupload;

 

// snippet from FileUploadBean

public class FileUploadBean {

    private byte[] file;

    public void setFile(byte[] file) {

        this.file = file;

    }

 

    public byte[] getFile() {

        return file;

    }

} 

 

 

4.          Springapp-servlet.xml文件如下:

/web-inf/springapp-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
  - Application context definition for "springapp" DispatcherServlet.
-->
<beans>
  <bean id="HomePageController" class="xslt.HomePageController"/>
  <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
      <props>
        <prop key="/hello.htm">HomePageController</prop>
        <prop key="/uploadfile.htm">fileUploadController</prop>
      </props>
    </property>
  </bean>
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass">
      <value>org.springframework.web.servlet.view.JstlView</value>
    </property>
    <property name="prefix">
      <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>
 
  
  <bean id="fileUploadController" class="fileupload.FileUploadController">
    <property name="commandClass">
      <value>fileupload.FileUploadBean</value>
    </property>
    <property name="formView">
      <value>fileuploadform</value>
    </property>
    <property name="successView">
      <value>confirmation</value>
    </property>
  </bean>
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize">
      <value>100000</value>
    </property>
  </bean>
</beans>
 

 

属性successView的值confirmation指的是confirmation.jspInternalResourceViewResolver使我们可以省略前缀(文件的路径)和后缀(.jsp

 

5.          文件Confirmation.jsp是上载文件成功后的确认页面

/web-inf/jsp/confirmation.jsp

<%@ page contentType="text/html; charset=Big5" %>
<html>
<head>
<title>
successView
</title>
</head>
<body bgcolor="#ffffff">
<h1>
JBuilder Generated JSP
successView
</h1>
</body>
</html>

 

6.          如果用户没有选择上载的文件就提交表单,文件fileuploadform.jsp就提示用户选择上载的文件后再提交

/web-inf/jsp/fileuploadform.jsp

<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>no file,Please upload a file</h1>
<form method="post" action="uploadfile.htm" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>

 

7.          文件upload.jsp把表单的处理交给uploadfile.htm(不知为什么,用uploadfile.form不行),springapp-servlet.xmluploadfile.xml映照到fileUploadController类。

Springapp/web-inf/jsp/upload.jsp

<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="uploadfile.htm" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>

 

8.          浏览http://localhost:8080/springapp/upload.jsp




相关文章

相关软件