服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用spring mvc框架进行文件上传

使用spring mvc框架进行文件上传,步骤如下:

1:配置web.xml文件。定义dispatcherservlet,dispatcherservlet处理的请求(.htm)也在同一个web.xml文件里使用url-mapping定义映射。

 <servlet>
  <servlet-name>upload</servlet-name>
  <servlet-class>org.springframework.web.servlet.dispatcherservlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>upload</servlet-name>
  <url-pattern>*.htm</url-pattern>
 </servlet-mapping>
2:定义upload-servlet.xml文件。
<bean id="multipartresolver"
       class="org.springframework.web.multipart.commons.commonsmultipartresolver">
        <!-- set the max upload size100mb -->
        <property name="maxuploadsize">
        <value>104857600</value>
    </property>
    <property name="maxinmemorysize">
        <value>4096</value>
    </property>
   </bean>
 <bean id="urlmapping"
  class="org.springframework.web.servlet.handler.simpleurlhandlermapping">       
 <property name="mappings">           
  <props>               
  <prop key="/upload.htm">uploadcontroller</prop>           
  </props>       
 </property>   
 </bean>
     <bean id="uploadcontroller" class="fileuploadcontroller">
      <property name="commandclass"><value>fileuploadbean</value></property>
      <property name="uploaddir"><value>e:/</value></property>
      <property name="formview"><value>fail</value></property>
  <property name="successview"><value>confirmation</value></property>
</bean>  
3:定义控制类,commandclass及方法。控制类中最重要的方法是initbinder()它给spring注册了一个编辑器对
request中的multipart实体进行处理,如果没有这个方法,上传将不能进行。
<--------------------------控制类------------------->
public class fileuploadcontroller extends simpleformcontroller {
    private static log log =
        logfactory.getlog(fileuploadcontroller.class);
    private string uploaddir;//上传文件路径

    protected modelandview onsubmit(httpservletrequest request,
            httpservletresponse response, object cmd, bindexception errors)
            throws exception {

            fileuploadbean bean = (fileuploadbean) cmd;
            byte[] bytes = bean.getfile();
          
            //cast to multipart file so we can get additional information
            multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
            commonsmultipartfile file = (commonsmultipartfile) multipartrequest.getfile("file");

            string uploaddir = this.getuploaddir();

            file dirpath = new file(uploaddir);
            if (!dirpath.exists()) {
                dirpath.mkdirs();
            }
            string sep = system.getproperty("file.separator");
            if (log.isdebugenabled()) {
                log.debug("uploading to: " + uploaddir + sep +
                file.getoriginalfilename());
                }
            file uploadedfile = new file(uploaddir + sep
                    + file.getoriginalfilename());
            filecopyutils.copy(bytes, uploadedfile);
            system.out.println("********************************");
            system.out.println(uploadedfile.getabsolutepath());
            system.out.println(bytes.length);
            system.out.println("********************************");
           
     
        return new modelandview(getsuccessview() + ".jsp");
    }

    protected void initbinder(httpservletrequest request,
            servletrequestdatabinder binder) throws servletexception {
        binder.registercustomeditor(byte[].class,
                new bytearraymultipartfileeditor());
    }
    public void setuploaddir(string uploaddir){
        this.uploaddir = uploaddir;
    }
    public string getuploaddir(){
        return this.uploaddir;
    }
}
<--------------------------控制类------------------------->
<---------------------定义commandclass-------------------->
public class fileuploadbean {

    private byte[] file;

    public void setfile(byte[] file) {
        this.file = file;
    }

    public byte[] getfile() {
        return file;
    }

}
<---------------------定义commandclass-------------------->
4:定义一个form表单index.jsp
<form method="post" action="upload.htm" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
5:定义confirmation.jsp及fail.jsp
confirmation.jsp如下:
<%@ page contenttype="text/html; charset=gbk" %>
<html>
<head>
<title>
successview
</title>
</head>
<body bgcolor="#ffffff">
<h1>
upload successful
</h1>
</body>
</html>

fail.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>
6:运行tomcat。
预览 ie里面:http://localhost/springmvc/index.jsp
注:
a:文件目录为tomcat-home/webapps/springmvc/
.jsp文件都放在根目录下,.class文件放在/web-inf/classes/中

其他文件放在/web-inf/里面。
b:如果连上面的你都有疑问,那还是去看看spring的基础知识吧。

扫描关注微信公众号