1. 1 不可撤销
  2. 2 小年兽 程嘉敏
  3. 3 手放开 李圣杰
  4. 4 迷人的危险3(翻自 dance flow) FAFA
  5. 5 山楂树之恋 程佳佳
  6. 6 summertime cinnamons / evening cinema
  7. 7 不谓侠(Cover 萧忆情Alex) CRITTY
  8. 8 神武醉相思(翻自 优我女团) 双笙
  9. 9 空山新雨后 音阙诗听 / 锦零
  10. 10 Wonderful U (Demo Version) AGA
  11. 11 广寒宫 丸子呦
  12. 12 陪我看日出 回音哥
  13. 13 春夏秋冬的你 王宇良
  14. 14 世界が终わるまでは… WANDS
  15. 15 多想在平庸的生活拥抱你 隔壁老樊
  16. 16 千禧 徐秉龙
  17. 17 我的一个道姑朋友 双笙
  18. 18 大鱼  (Cover 周深) 双笙
  19. 19 霜雪千年(Cover 洛天依 / 乐正绫) 双笙 / 封茗囧菌
  20. 20 云烟成雨(翻自 房东的猫) 周玥
  21. 21 情深深雨濛濛 杨胖雨
  22. 22 Five Hundred Miles Justin Timberlake / Carey Mulligan / Stark Sands
  23. 23 斑马斑马 房东的猫
  24. 24 See You Again Wiz Khalifa / Charlie Puth
  25. 25 Faded Alan Walker / Iselin Solheim
  26. 26 Natural J.Fla
  27. 27 New Soul Vox Angeli
  28. 28 ハレハレヤ(朗朗晴天)(翻自 v flower) 猫瑾
  29. 29 像鱼 王贰浪
  30. 30 Bye Bye Bye Lovestoned
  31. 31 Blame You 眠 / Lopu$
  32. 32 Believer J.Fla
  33. 33 书信 戴羽彤
  34. 34 柴 鱼 の c a l l i n g【已售】 幸子小姐拜托了
  35. 35 夜空中最亮的星(翻自 逃跑计划) 戴羽彤
  36. 36 慢慢喜欢你 LIve版(翻自 莫文蔚) 戴羽彤
  37. 37 病变(翻自 cubi) 戴羽彤
  38. 38 那女孩对我说 (完整版) Uu
  39. 39 绿色 陈雪凝
  40. 40 月牙湾 LIve版(翻自 F.I.R.) 戴羽彤
夜空中最亮的星(翻自 逃跑计划) - 戴羽彤
00:00 / 04:10

夜空中最亮的星 能否听清

那仰望的人 心底的孤独和叹息

夜空中最亮的星 能否记起

那曾与我同行 消失在风里的身影

我祈祷拥有一颗透明的心灵

和会流泪的眼睛

给我再去相信的勇气

越过谎言去拥抱你

每当我找不到存在的意义

每当我迷失在黑夜里

噢喔喔 夜空中最亮的星

请指引我靠近你

夜空中最亮的星 是否知道

那曾与我同行的身影 如今在哪里

夜空中最亮的星 是否在意

是等太阳先升起 还是意外先来临

我宁愿所有痛苦都留在心底

也不愿忘记你的眼睛

哦 给我再去相信的勇气

哦 越过谎言去拥抱你

每当我找不到存在的意义

每当我迷失在黑夜里

噢喔喔 夜空中最亮的星

请照亮我向前行 哒~

我祈祷拥有一颗透明的心灵

和会流泪的眼睛 哦

给我再去相信的勇气

哦 越过谎言去拥抱你

每当我找不到存在的意义

每当我迷失在黑夜里

噢喔喔 夜空中最亮的星

请照亮我向前行

Spring MVC文件上传

1、导包

  • Spring
  • commons-fileupload
  • commons-io

2、项目配置web.xml

  • 配置文件没有什么特别之处
<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>uploadmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-servlet.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>springServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

3、DispatcherServlet配置

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    

    <!-- scan the package and the sub package -->
    <context:component-scan base-package="com.sysker"/>

    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />

    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />
    <!-- 文件上传解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	<!-- 文件总大小 -->
    	<property name="maxUploadSize" value="500000"></property>
    	<!-- 单个文件总大小 -->
    	<property name="maxUploadSizePerFile" value="500000"></property>
    	<!-- 编码方式 -->
    	<property name="defaultEncoding" value="utf-8"></property>
    </bean>
    <!-- configure the InternalResourceViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/view/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
  • 在该配置文件中需要配置文件上传的解析器
 <!-- 文件上传解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	<!-- 文件总大小 -->
    	<property name="maxUploadSize" value="500000"></property>
    	<!-- 单个文件总大小 -->
    	<property name="maxUploadSizePerFile" value="500000"></property>
    	<!-- 编码方式 -->
    	<property name="defaultEncoding" value="utf-8"></property>
    </bean>

4、创建Controller

package com.sysker.util;

import java.io.File;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadAndDownloadController {

    @RequestMapping(value = "/")
    public String index() {
        return "index";
    }

    /**
     * 单个文件上传
     * 
     * @param uploadFile
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upload")
    public String doUpload(MultipartFile uploadFile, HttpSession session)
            throws Exception {
        String fileName = uploadFile.getOriginalFilename();

        String leftPath = session.getServletContext().getRealPath("/images");

        File file = new File(leftPath, fileName);
        uploadFile.transferTo(file);
        return "welcome";

    }

    /**
     * 限制文件上传格式
     * 
     * @param uploadFile
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadlim")
    public String doUploadLim(MultipartFile uploadFile, HttpSession session)
            throws Exception {
        if (uploadFile.getSize() > 0) {
            String fileName = uploadFile.getOriginalFilename();
            if (fileName.endsWith("jpg") || fileName.endsWith("gif")
                    || fileName.endsWith("png")) {
                String leftPath = session.getServletContext()
                        .getRealPath("/images");
                File file = new File(leftPath, fileName);
                uploadFile.transferTo(file);

            } else {
                return "error";
            }
        } else {
            return "error";
        }

        return "welcome";

    }

    /**
     * 多文件上传
     * 
     * @param uploadFile
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadmany")
    public String doUploadMany(@RequestParam MultipartFile[] uploadFile,
            HttpSession session) throws Exception {
        for (MultipartFile multipartFile : uploadFile) {
            String fileName = multipartFile.getOriginalFilename();
            if (fileName.endsWith("jpg") || fileName.endsWith("gif")
                    || fileName.endsWith("png")) {

                String leftPath = session.getServletContext()
                        .getRealPath("/images");

                File file = new File(leftPath, fileName);

                multipartFile.transferTo(file);

            } else {
                return "error";
            }
        }

        return "welcome";

    }
}


  • 这里需要注意的是代码中指定的的文件夹要手动创建,比如在我的代码中指定的路径为/images,那么我就必须在WebRoot下创建该目录,否则会抛出异常:
java.io.FileNotFoundException: D:\tools\eclipse-jee-oxygen-3a-win32-x86_64\eclipse-workspace\uploadmvc\WebContent\images
\58ba68439f0ea21d2ec37ffbe25f9e73.jpg (系统找不到指定的路径。)
  • jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data">
   <h2>文件上传</h2>
                文件:<input type="file" name="uploadFile"/><br/><br/>
      <input type="submit" value="上传"/>
   </form>
   
   <form action="${pageContext.request.contextPath }/uploadlim" method="post" enctype="multipart/form-data">
   <h2>文件上传:限定文件格式</h2>
                文件:<input type="file" name="uploadFile "/><br/><br/>
      <input type="submit" value="上传"/>
   </form>
   
   <form action="${pageContext.request.contextPath }/uploadmany" method="post" enctype="multipart/form-data">
   <h2>文件上传:多文件限定文件格式</h2>
                文件1:<input type="file" name="uploadFile "/><br/><br/>
                文件2:<input type="file" name="uploadFile "/><br/><br/>
                文件3:<input type="file" name="uploadFile "/><br/><br/>
      <input type="submit" value="上传"/>
   </form>
</body>
</html>
posted @ 2018-07-07 15:32  云中志  阅读(275)  评论(0编辑  收藏  举报