zoukankan      html  css  js  c++  java
  • SpringMvc (注解)中的上传文件

    第一步:导入commons-fileupload-1.3.1.jar 和commons-io-2.2.jar 架包

    第二步:在applicationContext.xml中 配置

    <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”/>

    第三步:在页面 <form>标签添加属性enctype=”multipart/form-data” 这个是上传文件必须的

    具体一个简单方法如下

     1.web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app version="3.0" 
     3     xmlns="http://java.sun.com/xml/ns/javaee" 
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
     7   <display-name></display-name>    
     8   <welcome-file-list>
     9     <welcome-file>index.jsp</welcome-file>
    10   </welcome-file-list>
    11 
    12     <!-- 配置pringmvc前端控制器 -->
    13     <servlet>
    14           <servlet-name>dispatcherServlet</servlet-name>
    15           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    16       <!-- 设置springmvc的配置文件 -->
    17           <init-param>
    18             <param-name>contextConfigLocation</param-name>
    19             <param-value>/WEB-INF/shopping-servlet.xml</param-value>
    20           </init-param>
    21           <load-on-startup>1</load-on-startup>
    22    </servlet>
    23    <servlet-mapping>
    24           <servlet-name>dispatcherServlet</servlet-name>
    25           <url-pattern>/</url-pattern>
    26    </servlet-mapping>
    27 </web-app>

    2.shopping-servlet.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.xsd
     9         http://www.springframework.org/schema/context
    10         http://www.springframework.org/schema/context/spring-context.xsd">
    11 
    12     <!-- 让spring容器自动探知bean   Controller    Service    Respoise   Componet -->
    13     <!-- 
    14     <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
    15      -->
    16     
    17     <context:component-scan base-package="com.bdqnsjz.shopping.controller"></context:component-scan>
    18 
    19     <bean id="multipartResolver"       
    20           class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:maxUploadSize="34564356345456" p:maxUploadSizePerFile="3456435634545" p:defaultEncoding="utf-8">
    21           <!-- 
    22           <property name="defaultEncoding" value="utf-8"></property>
    23            -->
    24     </bean>
    25 
    26 
    27     <!-- 配置视图解析器 -->
    28     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    29         <property name="prefix" value="/WEB-INF/pages/"></property>
    30         <property name="suffix" value=".jsp"></property>
    31     </bean>
    32 
    33     
    34 
    35 </beans>

    3.controller

     1 @RequestMapping(value="/upload1", method=RequestMethod.POST)
     2     public String upload1(@RequestParam(value = "file", required = false) MultipartFile file,
     3             HttpServletRequest request){
     4         
     5         String fileName = file.getOriginalFilename();
     6         System.out.println(fileName);
     7         
     8         //获取扩展名
     9         String extension = fileName.substring(fileName.lastIndexOf("."));
    10         
    11         //新的文件名    234-135234532l34kjlkj.jpg
    12         String savedFileName = java.util.UUID.randomUUID().toString() + extension;
    13         
    14         String folderPath = request.getRealPath("/WEB-INF/upload");
    15         
    16         File savedFile = new File(folderPath, savedFileName);
    17         
    18         
    19         //保存文件
    20         try {
    21             file.transferTo(savedFile);
    22         } catch (IOException e) {
    23             e.printStackTrace();
    24         }
    25         
    26         return "upload1";
    27     }
    海底一小鱼 https://www.cnblogs.com/Y-S-X/
  • 相关阅读:
    牛客练习赛51 D题
    Educational Codeforces Round 72 (Rated for Div. 2) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) C题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Codeforces Round #583 (Div. 1 + Div. 2, based on Olympiad of Metropolises) A题
    Educational Codeforces Round 72 (Rated for Div. 2) B题
    Educational Codeforces Round 72 (Rated for Div. 2) A题
    《DSP using MATLAB》Problem 7.2
    《DSP using MATLAB》Problem 7.1
    《DSP using MATLAB》Problem 6.24
  • 原文地址:https://www.cnblogs.com/Y-S-X/p/5293330.html
Copyright © 2011-2022 走看看