利用MultipartFile实现文件上传功能
程序员文章站
2023-12-17 20:59:10
在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartupload实现文件上传,最近在工作中使用spring的multipartfile实现文...
在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartupload实现文件上传,最近在工作中使用spring的multipartfile实现文件上传,感觉挺简单,在这里和大家分享一下。
一.主要有两个java类,和一般的servlet放在一起即可.
1.fileuploadbean.java
package chb.demo.web; import org.springframework.web.multipart.multipartfile; /** * @author chb * */ public class fileuploadbean { private multipartfile file; public void setfile(multipartfile file) { this.file = file; } public multipartfile getfile() { return file; } }
2.fileuploadcontroller.java
package chb.demo.web; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.springframework.validation.bindexception; import org.springframework.web.multipart.multipartfile; import org.springframework.web.servlet.modelandview; import org.springframework.web.servlet.mvc.simpleformcontroller; /** * @author chb * */ public class fileuploadcontroller extends simpleformcontroller { protected modelandview onsubmit( httpservletrequest request, httpservletresponse response, object command, bindexception errors){ try { // cast the bean fileuploadbean bean = (fileuploadbean) command; // let's see if there's content there multipartfile file = bean.getfile(); if (file == null) { throw new exception("上传失败:文件为�空"); } if(file.getsize()>10000000) { throw new exception("上传失败:文件大小不能超过10m"); } //得到文件�名 string filename=file.getoriginalfilename(); if(file.getsize()>0){ try { savefilefrominputstream(file.getinputstream(),"d:/",filename); } catch (ioexception e) { system.out.println(e.getmessage()); return null; } } else{ throw new exception("上传失败:上传文件不能为�空"); } // well, let's do nothing with the bean for now and return: try { return super.onsubmit(request, response, command, errors); } catch (exception e) { system.out.println(e.getmessage()); return null; } } catch(exception ex) { system.out.println(ex.getmessage()); return null; } } /**保存文件 * @param stream * @param path * @param filename * @throws ioexception */ public void savefilefrominputstream(inputstream stream,string path,string filename) throws ioexception { fileoutputstream fs=new fileoutputstream( path + "/"+ filename); byte[] buffer =new byte[1024*1024]; int bytesum = 0; int byteread = 0; while ((byteread=stream.read(buffer))!=-1) { bytesum+=byteread; fs.write(buffer,0,byteread); fs.flush(); } fs.close(); stream.close(); } }
二.配置文件中如下配置:
1.web.xml,利用spring mvc模式,大家应该都很熟悉了
<servlet> <servlet-name>chb</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>chb</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
2.chb-servlet.xml,这里要配置映射,并可以设定最大可上传文件的大小
<?xml version="1.0" encoding="utf-8"?> <!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- multi-action 用来标识method的变量名定义--> <bean id="methodnameresolver" class="org.springframework.web.servlet.mvc.multiaction.parametermethodnameresolver"> <property name="paramname"> <value>action</value> </property> <property name="defaultmethodname"> <value>index</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="10000000"/> </bean> <bean id="handlermapping" class="org.springframework.web.servlet.handler.simpleurlhandlermapping"> <property name="mappings"> <props> <prop key="/upload.do">fileuploadcontroller</prop> </props> </property> </bean> <bean id="fileuploadcontroller" class="chb.demo.web.fileuploadcontroller"> <property name="commandclass" value="chb.demo.web.fileuploadbean"/> <!-- 上传失败时跳转页面 --> <property name="formview" value="/user/err.jsp"/> <!-- 上传成功时跳转页面 --> <property name="successview" value="/user/confirmation.jsp"/> </bean> </beans>
三.设定jsp页面
<form id="form1" method="post" action="upload.do" enctype="multipart/form-data"> <tr> <td width="25%" align="right">上传文件:</td> <td><input id="file" type="file" name="file" style="width:300px;"></td> </tr> <tr align="center" valign="middle"> <td height="60" colspan="2"><input type="submit" id="btnok" value="确认上传"></td> </tr> </form>
ok,现在就可以上传文件了,挺简单吧?这里我只列出了基本步骤,至于具体的操作(比如中文问题)可能就需要大家自己再完善完善了.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。