SpringMvc上传文件

比眉伴天荒 2022-03-15 03:36 507阅读 0赞

工程目录:

20190228175715264.png

jar包

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lvbmdidXRpbmd4aWRl_size_16_color_FFFFFF_t_70

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3. <display-name>SpringMvc-01</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. </welcome-file-list>
  7. <servlet>
  8. <servlet-name>springmvc</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <init-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <param-value>classpath:spring-mvc.xml</param-value>
  13. </init-param>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>springmvc</servlet-name>
  17. <url-pattern>*.do</url-pattern>
  18. </servlet-mapping>
  19. </web-app>

spring-mvc.xml

  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:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd">
  11. <!-- 使用注解的包,包括子集 -->
  12. <context:component-scan base-package="com.java1234"/>
  13. <!-- 视图解析器 -->
  14. <bean id="viewResolver"
  15. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  16. <property name="prefix" value="/WEB-INF/jsp/" />
  17. <property name="suffix" value=".jsp"></property>
  18. </bean>
  19. <bean id="multipartResolver"
  20. class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  21. <property name="defaultEncoding" value="UTF-8"/>
  22. <property name="maxUploadSize" value="10000000"/>
  23. </bean>
  24. </beans>

UploadController.java

  1. package com.java1234.controller;
  2. import java.io.File;
  3. import javax.servlet.http.HttpServletRequest;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.multipart.MultipartFile;
  8. @Controller
  9. public class UploadController {
  10. /**
  11. * 上传单个文件
  12. * @param file1
  13. * @param request
  14. * @return
  15. * @throws Exception
  16. */
  17. @RequestMapping("/upload")
  18. public String uploadFile(@RequestParam("file1") MultipartFile file1,HttpServletRequest request) throws Exception{
  19. String filePath=request.getServletContext().getRealPath("/");
  20. System.out.println(filePath);
  21. file1.transferTo(new File(filePath+"upload/"+file1.getOriginalFilename()));
  22. return "redirect:success.jsp";
  23. }
  24. /**
  25. * 上传多个文件
  26. * @param files
  27. * @param request
  28. * @return
  29. * @throws Exception
  30. */
  31. @RequestMapping("/upload2")
  32. public String uploadFiles(@RequestParam("file") MultipartFile []files,HttpServletRequest request) throws Exception{
  33. String filePath=request.getServletContext().getRealPath("/");
  34. System.out.println(filePath);
  35. for(MultipartFile file:files) {
  36. file.transferTo(new File(filePath+"upload/"+file.getOriginalFilename()));
  37. }
  38. return "redirect:success.jsp";
  39. }
  40. }

index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="upload2.do" method="post" enctype="multipart/form-data">
  11. <input type="file" name="file"/><br>
  12. <input type="file" name="file"/><br>
  13. <input type="file" name="file"/><br>
  14. <input type="submit" value="上传"/>
  15. </form>
  16. </body>
  17. </html>

success.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. 上传文件成功
  11. </body>
  12. </html>

上传成功后文件会存储在工程下的upload文件夹下面。暂时看不到,可以通过输出文件的路径来得知。

D:\eclipse-jee-photon-R-win32-x86_64\projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SpringMvc-04\

发表评论

表情:
评论列表 (有 0 条评论,507人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringMVC文件

    文件上传自然是一个网站必不可少的元素之一,SpringMVC这个网站编程框架自然也有这个东西,下一面举一个例子说明这个问题。 如下图所示,一个简单的上传控件,只让上传bmp、

    相关 SpringMVC_文件

    一、文件上传 1、说明 SpringMVC为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver 实现的。Spring用 Jak