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

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

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

[html]  view plain copy print ?
  1. <servlet>  
  2.   <servlet-name>upload</servlet-name>  
  3.   <servlet-class>org.springframework.web.servlet.DispatcherServlet  
  4.   </servlet-class>  
  5.   <load-on-startup>1</load-on-startup>  
  6.  </servlet>  

[html]  view plain copy print ?
  1. <servlet-mapping>  
  2.   <servlet-name>upload</servlet-name>  
  3.   <url-pattern>*.htm</url-pattern>  
  4.  </servlet-mapping>  

2:定义upload-servlet.xml文件。

[html]  view plain copy print ?
  1. <bean id="multipartResolver"  
  2.        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  3.         <!-- set the max upload size100MB -->  
  4.         <property name="maxUploadSize">  
  5.         <value>104857600</value>  
  6.     </property>  
  7.     <property name="maxInMemorySize">  
  8.         <value>4096</value>  
  9.     </property>  
  10.    </bean>  
  11.  <bean id="urlMapping"  
  12.   class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">         
  13.  <property name="mappings">             
  14.   <props>                 
  15.   <prop key="/upload.htm">uploadController</prop>             
  16.   </props>         
  17.  </property>     
  18.  </bean>  
  19.      <bean id="uploadController" class="FileUploadController">  
  20.       <property name="commandClass"><value>FileUploadBean</value></property>  
  21.       <property name="uploadDir"><value>E:/</value></property>  
  22.       <property name="formView"><value>fail</value></property>  
  23.   <property name="successView"><value>confirmation</value></property>  
  24. </bean>    
3:定义控制类,commandClass及方法。控制类中最重要的方法是initBinder()它给spring注册了一个编辑器对
request中的multipart实体进行处理,如果没有这个方法,上传将不能进行。

<--------------------------控制类------------------------->

[java]  view plain copy print ?
  1. public class FileUploadController extends SimpleFormController {  
  2.     private static Log log =  
  3.         LogFactory.getLog(FileUploadController.class);  
  4.     private String uploadDir;//上传文件路径  
  5.   
  6.     protected ModelAndView onSubmit(HttpServletRequest request,  
  7.             HttpServletResponse response, Object cmd, BindException errors)  
  8.             throws Exception {  
  9.   
  10.             FileUploadBean bean = (FileUploadBean) cmd;  
  11.             byte[] bytes = bean.getFile();  
  12.             
  13.             //cast to multipart file so we can get additional information  
  14.             MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  
  15.             CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");  
  16.   
  17.             String uploadDir = this.getUploadDir();  
  18.   
  19.             File dirPath = new File(uploadDir);  
  20.             if (!dirPath.exists()) {  
  21.                 dirPath.mkdirs();  
  22.             }  
  23.             String sep = System.getProperty("file.separator");  
  24.             if (log.isDebugEnabled()) {  
  25.                 log.debug("uploading to: " + uploadDir + sep +  
  26.                 file.getOriginalFilename());  
  27.                 }  
  28.             File uploadedFile = new File(uploadDir + sep  
  29.                     + file.getOriginalFilename());  
  30.             FileCopyUtils.copy(bytes, uploadedFile);  
  31.             System.out.println("********************************");  
  32.             System.out.println(uploadedFile.getAbsolutePath());  
  33.             System.out.println(bytes.length);  
  34.             System.out.println("********************************");  
  35.              
  36.        
  37.         return new ModelAndView(getSuccessView() + ".jsp");  
  38.     }  
  39.   
  40.     protected void initBinder(HttpServletRequest request,  
  41.             ServletRequestDataBinder binder) throws ServletException {  
  42.         binder.registerCustomEditor(byte[].class,  
  43.                 new ByteArrayMultipartFileEditor());  
  44.     }  
  45.     public void setUploadDir(String uploadDir){  
  46.         this.uploadDir = uploadDir;  
  47.     }  
  48.     public String getUploadDir(){  
  49.         return this.uploadDir;  
  50.     }  
  51. }  
<---------------------定义commandClass-------------------->
[java]  view plain copy print ?
  1. public class FileUploadBean {  
  2.   
  3.     private byte[] file;  
  4.   
  5.     public void setFile(byte[] file) {  
  6.         this.file = file;  
  7.     }  
  8.   
  9.     public byte[] getFile() {  
  10.         return file;  
  11.     }  
  12.   
  13. }  

4:定义一个form表单index.jsp

[plain]  view plain copy print ?
  1. <form method="post" action="upload.htm" enctype="multipart/form-data">  
  2. <input type="file" name="file"/>  
  3. <input type="submit"/>  
  4. </form>  
5:定义confirmation.jsp及fail.jsp

confirmation.jsp如下:

[plain]  view plain copy print ?
  1. <%@ page contentType="text/html; charset=GBK" %>  
  2. <html>  
  3. <head>  
  4. <title>  
  5. successView  
  6. </title>  
  7. </head>  
  8. <body bgcolor="#ffffff">  
  9. <h1>  
  10. Upload Successful  
  11. </h1>  
  12. </body>  
  13. </html>  

fail.jsp如下:

[plain]  view plain copy print ?
  1. <html>  
  2. <head>  
  3. <title>Upload a file please</title>  
  4. </head>  
  5. <body>  
  6. <h1>no file,Please upload a file</h1>  
  7. <form method="post" action="uploadfile.htm" enctype="multipart/form-data">  
  8. <input type="file" name="file"/>  
  9. <input type="submit"/>  
  10. </form>  
  11. </body>  
  12. </html>  

6:运行tomcat。
预览 ie里面:http://localhost/springmvc/index.jsp
注:
a:文件目录为tomcat-HOME/webapps/springmvc/
.jsp文件都放在根目录下,.class文件放在/WEB-INF/classes/中

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
辽B代驾管理系统对代驾订单管理、用户咨询管理、代驾订单评价管理、代驾订单投诉管理、字典管理、论坛管理、公告管理、新闻信息管理、司机管理、用户管理、管理员管理等进行化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行辽B代驾管理系统程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。辽B代驾管理系统的开发让用户查看代驾订单信息变得容易,让管理员高效管理代驾订单信息。 辽B代驾管理系统具有管理员角色,用户角色,这几个操作权限。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看代驾订单,删除代驾订单操作,新增代驾订单操作,修改代驾订单操作。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告型管理页面显示所有公告型,在此页面既可以让管理员添加新的公告信息型,也能对已有的公告型信息执行编辑更新,失效的公告型信息也能让管理员快速删除。新闻管理页面,此页面提供给管理员的功能有:新增新闻,修改新闻,删除新闻。新闻型管理页面,此页面提供给管理员的功能有:新增新闻型,修改新闻型,删除新闻型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值