zoukankan      html  css  js  c++  java
  • Spring +SpringMVC 实现文件上传功能。。。

    要实现Spring +SpringMVC  实现文件上传功能。

    第一步:下载

    第二步:

    新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring的jar包这里就不多说)

    然后把刚刚下载的两个包放在lib目录下。

    第三步:

    在web.xml中把springMVC和spring合并。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!--配置编码格式--> <filter> <filter-name>filterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>filterEncoding</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping>
    <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 合并spring mvc的xml配置文件和spring的配置文件 如果没有合并那么每个control 都对应一个xml (servletname-servlet.xml) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
    <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>

    第四步:

    修改spring配置文件applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            ">
    
    
    <context:component-scan base-package="com.control"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- 上传文件必须的配置 -->
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
            <property name="defaultEncoding" value="utf-8"></property>   
            <property name="maxUploadSize" value="10485760000"></property>  
            <property name="maxInMemorySize" value="40960"></property>  
      </bean>  
    
    </beans>

    ok!环境就这样搭好了!接下来就真正实现功能了!

    首先书写一个上传文件的类

    @Controller
    @RequestMapping("/file")
    public class UploadControl {
    
        @RequestMapping("/upload.html")
        public String upload(@RequestParam (value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model){
            
               System.out.println("开始");  
              //文件上传到的位置
                String path = request.getSession().getServletContext().getRealPath("upload");  
               //上传的文件名
                String fileName = file.getOriginalFilename(); 
                //处理重复提交相同东西不被覆盖
               // String fileName = new Date().getTime()+".jpg";  
                File targetFile = new File(path, fileName);  
                //判断文件夹是否存在
                if(!targetFile.exists()){  
                    targetFile.mkdirs();  
                }  
          
                //保存  
                try {  
                    file.transferTo(targetFile);  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
                model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);  
          
                return "success";  
        }
    }

    然后是jsp

     <body>
      <form action="file/upload.html" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="上传" /> </form> <br/>

     就这样一个上传的功能就实现了!

  • 相关阅读:
    WebClient, HttpClient, HttpWebRequest ,RestSharp之间的区别与抉择(几种Http请求方法)
    关于c#里面的httpclient的调用
    mvc项目架构分享系列之架构搭建初步 架构搭建初步
    关于在.NET中 DAL+IDAL+Model+BLL+Web
    三层架构(DAL/BLL/UI)和MVC设计模式的关系
    c#开发初学者之mvc及架构分层
    c#串口编程(转)
    【WPF】对话框/消息弹窗
    WPF如何不显示最大化,最小化的按钮
    信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1103:陶陶摘苹果
  • 原文地址:https://www.cnblogs.com/susuhyc/p/5073605.html
Copyright © 2011-2022 走看看