SpringMVC 两种上传文件方法

     在该示例中,阐述了SpringMVC如何上传文件。

1、上传页面upload.jsp


  
  
  1. <body>
  2. <form action="/TestSpringMVC3/data/uploadfile" enctype="multipart/form-data" method="post">
  3. file: <input type="file" name="file"> <br>
  4. <input type="submit" value="upload file">
  5. </form>
  6. </body>
2、controller配置文件

  
  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc= "http://www.springframework.org/schema/mvc"
  5. xmlns:p= "http://www.springframework.org/schema/p"
  6. xmlns:context= "http://www.springframework.org/schema/context"
  7. xmlns:aop= "http://www.springframework.org/schema/aop"
  8. xmlns:tx= "http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation= "http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  15. http://www.springframework.org/schema/tx
  16. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  17. http://www.springframework.org/schema/mvc
  18. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  19. http://www.springframework.org/schema/context
  20. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  21. <!--
  22. 使Spring支持自动检测组件,如注解的Controller
  23. -->
  24. <context:component-scan base-package="cn.com.yy.controller"/>
  25. <!-- 开启注解配置 -->
  26. <mvc:annotation-driven/>
  27. <!-- 支持JSP JSTL的解析器 -->
  28. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  29. <property name="prefix" value="/WEB-INF/page/"/>
  30. <property name="suffix" value=".jsp"/>
  31. </bean>
  32. <!-- 配置文件上传解析器 -->
  33. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  34. <property name="defaultEncoding" value="utf-8"/>
  35. <property name="maxUploadSize" value="10485760000"/>
  36. <property name="maxInMemorySize" value="40960"/>
  37. </bean>
  38. </beans>
    主要是添加了文件上传的解析器,配置了默认编码,最大的上传大小以及缓存大小等参数。

3、Controller


  
  
  1. package cn.com.yy.controller;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.multipart.commons.CommonsMultipartFile;
  12. @Controller
  13. @RequestMapping( "/data")
  14. public class FileUploadController {
  15. /**
  16. * method1:通过参数CommonsMultipartFile进行解析
  17. * @RequestParam("file")中的file对应于upload.jsp中的file类型的name对应的名称
  18. * @param file
  19. * @return
  20. * @throws IOException
  21. */
  22. @RequestMapping(value= "/uploadfile")
  23. public String upload1(@RequestParam("file") CommonsMultipartFile file) throws IOException{
  24. //获取文件名称
  25. String fileName = file.getOriginalFilename();
  26. //写入本地磁盘
  27. InputStream is = file.getInputStream();
  28. byte[] bs = new byte[ 1024];
  29. int len;
  30. OutputStream os = new FileOutputStream( new File( "D:/temp/" + fileName));
  31. while ((len = is.read(bs)) != - 1) {
  32. os.write(bs, 0, len);
  33. }
  34. os.close();
  35. is.close();
  36. return "upload_success";
  37. }
  38. @RequestMapping( "/upload")
  39. public String toPage(){
  40. return "upload";
  41. }
  42. }


4、返回页面upload_success.jsp


  
  
  1. <body>
  2. upload file success!!
  3. </body>

5、测试

     访问  http://localhost:8080/TestSpringMVC3/data/upload  跳转到上传页面

    

    选择文件上传

                    

  点击上传会跳转到上传成功页面。

      上述方法只是简单的讲解了SpringMVC如何上传文件。

    

  第二种方法:使用SpringMVC封装的方法进行文件上传


  
  
  1. /**
  2. * 使用SpringMVC封装好的方法进行文件上传
  3. * @param request
  4. * @param response
  5. * @throws IllegalStateException
  6. * @throws IOException
  7. */
  8. @RequestMapping( "/uploadfile2")
  9. public void upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{
  10. //获取解析器
  11. CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
  12. //判断是否是文件
  13. if(resolver.isMultipart(request)){
  14. //进行转换
  15. MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request);
  16. //获取所有文件名称
  17. Iterator<String> it = multiRequest.getFileNames();
  18. while(it.hasNext()){
  19. //根据文件名称取文件
  20. MultipartFile file = multiRequest.getFile(it.next());
  21. String fileName = file.getOriginalFilename();
  22. String localPath = "D:/temp/" + fileName;
  23. File newFile = new File(localPath);
  24. //上传的文件写入到指定的文件中
  25. file.transferTo(newFile);
  26. }
  27. }
  28. }
   该方法上传文件效率更优。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值