资讯专栏INFORMATION COLUMN

SpringMVC文件上传

Karuru / 2694人阅读

摘要:为文件上传提供了直接的支持,这种支持是用即插即用的实现的。因此,的文件上传还需要依赖的组件。上传文件时,需要配置处理器文件上传文件上传是项目开发中最常见的功能。为了能上传文件,必须将表单的设置为,并将设置为。

</>复制代码

  1. SpringMVC为文件上传提供了直接的支持,这种支持是用即插即用的MultipartResolver实现的。SpringMVC使用Apache Commons FileUpload技术实现了一个MultipartResolver实现类:CommonsMultipartResolver。因此,SpringMVC的文件上传还需要依赖Apache Commons FileUpload的组件。
1. 添加pom依赖

</>复制代码

  1. commons-io
  2. commons-io
  3. 2.3
  4. commons-fileupload
  5. commons-fileupload
  6. 1.3.2
2. 配置文件上传bean

在spring mvc配置文件中增加一个文件上传bean。

</>复制代码

3.文件上传

</>复制代码

  1. 文件上传是项目开发中最常见的功能。为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data。只有在这样的情况下,浏览器才会把用户选择的文件以二进制数据发送给服务器。

上传文件界面:upload_form.jsp

</>复制代码

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. 文件上传


  3. 文件1:

  4. 文件2:

  5. 文件3:

  6. 用户名:

  7. 密码:

上传结果返回界面:upload_result.jsp

</>复制代码

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. Insert title here
  4. 上传结果为:${message}

注意:要提前创建好存储文件的文件夹,比如我的路径为:"D:staticResourcesTestimgupload"。

FileController.java

</>复制代码

  1. @Controller
  2. @RequestMapping("/SpringMVCDemo1")
  3. public class FileController {
  4. /**
  5. * 跳转到上传页面
  6. * @GetMapping 是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。
  7. */
  8. @GetMapping("/gotoUploadForm")
  9. public String index() {
  10. return "/upload_form.jsp";
  11. }
  12. /**
  13. * 上传单个文件
  14. * 通过MultipartFile读取文件信息,如果文件为空跳转到结果页并给出提示;
  15. * 如果不为空读取文件流并写入到指定目录,最后将结果展示到页面
  16. * @param multipartFile
  17. * @PostMapping 是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。
  18. */
  19. @PostMapping("/upload")
  20. public String uploadSingleFile(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request){
  21. if (multipartFile.isEmpty()){
  22. request.setAttribute("message", "Please select a file to upload "");
  23. return "/upload_result.jsp";
  24. }
  25. try {
  26. String contentType = multipartFile.getContentType();
  27. String originalFilename = multipartFile.getOriginalFilename();
  28. byte[] bytes = multipartFile.getBytes();
  29. System.out.println("上传文件名为-->" + originalFilename);
  30. System.out.println("上传文件类型为-->" + contentType);
  31. System.out.println("上传文件大小为-->"+bytes.length);
  32. //filePath为存储路径
  33. String filePath = "d:/staticResourcesTest";
  34. System.out.println("filePath-->" + filePath);
  35. //存储在staticResourcesTest下的imgupload文件夹下
  36. File parentPath = new File(filePath, "imgupload");
  37. System.out.println("上传目的地为-->"+parentPath.getAbsolutePath());
  38. try {
  39. File destFile = new File(parentPath,originalFilename);//上传目的地
  40. FileUtils.writeByteArrayToFile(destFile,multipartFile.getBytes());
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. request.setAttribute("message", "You successfully uploaded "" + multipartFile.getOriginalFilename() + """);
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. return "/upload_result.jsp";
  49. }
  50. /**
  51. * 上传多个文件,同时接受业务数据
  52. * @param origFiles
  53. * @param request
  54. * @param user
  55. * @return
  56. */
  57. @PostMapping("/uploadMultiFiles")
  58. public String uploadMultiFiles(@RequestParam("file") List origFiles, HttpServletRequest request, User user) {
  59. //User为实体类
  60. System.out.println("User=="+user);
  61. if (origFiles.isEmpty()) {
  62. request.setAttribute("message", "Please select a file to upload "");
  63. return "/upload_result.jsp";
  64. }
  65. try {
  66. for (MultipartFile origFile : origFiles) {
  67. String contentType = origFile.getContentType();
  68. String fileName = origFile.getOriginalFilename();
  69. byte[] bytes = origFile.getBytes();
  70. System.out.println("上传文件名为-->" + fileName);
  71. System.out.println("上传文件类型为-->" + contentType);
  72. System.out.println("上传文件大小为-->"+bytes.length);
  73. String filePath = "d:/staticResourcesTest";
  74. System.out.println("上传目的地为-->"+filePath);
  75. try {
  76. //上传目的地(staticResourcesTest文件夹下)
  77. File destFile = new File(filePath,fileName);
  78. FileUtils.writeByteArrayToFile(destFile,origFile.getBytes());
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. request.setAttribute("message", "You successfully uploaded "");
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. return "/upload_result.jsp";
  88. }
  89. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73859.html

相关文章

  • SpringMVC【参数绑定、数据回显、文件上传

    摘要:那我们就不用在每一个方法通过将数据传到页面。还能够配置该参数是否是必须的。方法的返回值有种重定向转发内部就是将数据绑定到域对象中的。注解能够将数据绑定到中也就是中,如果经常需要绑定到中的数据,抽取成方法来使用这个注解还是不错的。 前言 本文主要讲解的知识点如下: 参数绑定 数据回显 文件上传 参数绑定 我们在Controller使用方法参数接收值,就是把web端的值给接收到Cont...

    Flink_China 评论0 收藏0
  • SpringMVC 文件上传注意事项

    摘要:问题出现在没有熟练使用,采用上传文件主要注意几个事项。配置在文件中,增加一个处理文件上传同时要在文件夹下的下建立目录。控制器团队项目集合路径团队项目集合是上传文件的临时存储路径。这个步骤最关键,否则文件上传不上去。 简介 我在使用 idea 上传文件时遇到一些问题,费了好些时间,最后还是的队友来帮忙。 问题出现在没有熟练使用 IDE,采用 SpringMVC 上传文件主要注意几个事项。...

    aaron 评论0 收藏0
  • springmvc 接收上传图片并且存储至本地目录

    摘要:图片存储的方式为存储在虚拟目录下并返回虚拟目录的路径。如果是将开发的包直接部署在服务器中,则参考配置虚拟目录。 序言:项目环境 本次简单的demo是建立在springmvc框架上的,部署环境为tomcat,前段使用的bootstrap+jquery_file_upload组件。图片存储的方式为存储在虚拟目录下并返回虚拟目录的路径。建议先搭配好springmvc环境再继续参考本博客。没有...

    SimpleTriangle 评论0 收藏0

发表评论

0条评论

Karuru

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<