文件上传(StringMVC)

文件上传(StringMVC)

我们经常会使用的一个功能是文件下载,既然有文件下载就会有文件上传,下面我们来看一下文件上传是如何实现的

首先准备好一个页面

jsp

<style type="text/css">
        form{
            margin:0px auto;
            border:1px solid red;
            width:500px;
            padding:20px;
        }
    </style>
  </head>

  <body>
   <form action="${pageContext.request.contextPath }/frist.do" method="post" enctype="multipart/form-data">
       <h1>文件上传</h1>
       文件:<input type="file" name="uploadFile"/><br/>
       文件:<input type="file" name="uploadFile"/><br/>
       文件:<input type="file" name="uploadFile"/><br/>
       <input type="submit" value="上传">
   </form>
  </body>

applicationContext.xml配置

web.xml

  

多文件上传(多文件上传与单文件上传配置相同下面介绍一下不同的地方)

标记为红色的字段为多文件上传 与单文件上传的区别

文件上传(StringMVC)

我们经常会使用的一个功能是文件下载,既然有文件下载就会有文件上传,下面我们来看一下文件上传是如何实现的

首先准备好一个页面

jsp

<style type="text/css">
        form{
            margin:0px auto;
            border:1px solid red;
            width:500px;
            padding:20px;
        }
    </style>
  </head>

  <body>
   <form action="${pageContext.request.contextPath }/frist.do" method="post" enctype="multipart/form-data">
       <h1>文件上传</h1>
       文件:<input type="file" name="uploadFile"/><br/>
       文件:<input type="file" name="uploadFile"/><br/>
       文件:<input type="file" name="uploadFile"/><br/>
       <input type="submit" value="上传">
   </form>
  </body>

单文件上传

通过对文件的大小来判断是否有文件

通过文件的类型来判断是否是允许


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

@Controller

public class MyController {

    @RequestMapping(value="/frist.do", method=RequestMethod.POST)

    public String doFirst(HttpSession session,MultipartFile uploadFile)throws Exception{

        if(uploadFile.getSize()>0){

            //02.获取前半部分路径,jiangWebRoot下一个名称为images文件夹  转换成绝对路径

            String path = session.getServletContext().getRealPath("/upload");

            //01.获取文件名作为保存到服务器的文件名称

            String fileName=uploadFile.getOriginalFilename();

            if(fileName.endsWith(".jpg")||fileName.endsWith(".JPG")){

                //03.路径拼接

                File file = new File(path,fileName);

                uploadFile.transferTo(file);

            }

            return "welcome.jsp";

        }

        return "error.jsp";

    }

applicationContext.xml配置


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"

    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.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

        ">

    <!-- 配置包扫描器 -->

    <context:component-scan base-package="cn.controller"></context:component-scan>

    <bean id="multipartResolver"

        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <property name="defaultEncoding" value="utf-8"></property><!-- 客户端发送数据的编码 -->

        <property name="maxUploadSize" value="5242880"></property><!-- 上传文件的大小 -->

        <!-- <property name="uploadTempDir" value="/upload"></property> -->

    </bean>

    <!-- mvc注解驱动 -->

    <mvc:annotation-driven />

</beans>

web.xml


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<?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">

  <display-name></display-name>

  <!-- ================spring mvc 适配器================ -->  

  <servlet>

    <servlet-name>springmvc</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <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>springmvc</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>

 <!-- ================================================== --> 

   

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

  

多文件上传(多文件上传与单文件上传配置相同下面介绍一下不同的地方)

标记为红色的字段为多文件上传 与单文件上传的区别


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

 @RequestMapping(value="/firstdown.do")

public String doFirst(@RequestParam MultipartFile[] uploadFile,HttpSession session)throws Exception{

    for (MultipartFile item : uploadFile) {

        

        if(item.getSize()>0){

            //02.获取前半部分路径,jiangWebRoot下一个名称为images文件夹  转换成绝对路径

            String path = session.getServletContext().getRealPath("/upload");

            //01.获取文件名作为保存到服务器的文件名称

            String fileName=item.getOriginalFilename();

            if(fileName.endsWith(".jpg")||fileName.endsWith(".JPG")){

                //03.路径拼接

                File file = new File(path,fileName);

                item.transferTo(file);

            }

            return "welcome.jsp";

        }

    }

    return "error.jsp";

}

文件下载


1

@RequestMapping(value="/first.do")


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<br> public static void download(HttpServletRequest request, 

            HttpServletResponse response, String storeName, String contentType

           throws Exception { 

        

        request.setCharacterEncoding("UTF-8"); 

        BufferedInputStream bis = null

        BufferedOutputStream bos = null

  

        //获取项目根目录

        String ctxPath = request.getSession().getServletContext() 

                .getRealPath(""); 

        

        //获取下载文件露肩

        String downLoadPath = ctxPath+"/uploadFile/"+ storeName; 

  

        //获取文件的长度

        long fileLength = new File(downLoadPath).length(); 

        //设置文件输出类型

        response.setContentType("application/octet-stream"); 

        response.setHeader("Content-disposition""attachment; filename="

                new String(storeName.getBytes("utf-8"), "ISO8859-1"));

        //设置输出长度

        response.setHeader("Content-Length", String.valueOf(fileLength)); 

        //获取输入流

        bis = new BufferedInputStream(new FileInputStream(downLoadPath)); 

        //输出流

        bos = new BufferedOutputStream(response.getOutputStream()); 

        byte[] buff = new byte[2048]; 

        int bytesRead; 

        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { 

            bos.write(buff, 0, bytesRead); 

        

        //关闭流

        bis.close(); 

        bos.close(); 

    

下载直接访问控制器如:http:\\localhost:8080/springmvc/download.do

或者通过JSP页面


1

<a href="./downloadFile/download" >下载</a> 

  

时间: 2024-08-07 08:35:53

文件上传(StringMVC)的相关文章

【springMVC】之文件上传

通过前两篇博客的学习,想必大家对springMVC已经有了一个基本的认识.今天我们主要来学习一下springMVC两种文件上传的方式. 首先介绍第一种,通过字节流的方式实现文件上传.首先创建一个upload.jsp页面 <body> <h>添加用户</h> <!-- entype要声音和支撑这种类型的,保证文件上传不会被解码--> <!-- form表单,action是说讲这个表单提交到什么路径: method="post"是提交方

简单利用filetype进行文件上传

对于文件上传大家都很熟悉了,毕竟文件上传是获取webshell的一个重要方式之一,理论性的东西参考我的另一篇汇总文章<浅谈文件解析及上传漏洞>,这里主要是实战补充一下理论内容--filetype漏洞! filetype漏洞主要是针对content-type字段,主要有两种利用方式:    1.先上传一个图片,然后将content-type:image/jpeg改为content-type:text/asp,然后对filename进行00截断,将图片内容替换为一句话木马. 2.直接使用burp抓

jquery-ajax实现文件上传异常处理web.multipart.MultipartException

异常如下: org.springframework.web.multipart.MultipartException: The current request is not a multipart request 原因分析: 可能原因1: form表单中没有添加 enctype="multipart/form-data" 属性 可能原因2: 请求方式必须为post,如果不是则必定出错 可能原因3: 请求的contentType不是"multipart/form-data&qu

SpringMVC中文件上传的客户端验证

SpringMVC中文件上传的客户端验证 客户端验证主要思想:在jsp页面中利用javascript进行对文件的判断,完成验证后允许上传 验证步骤:1.文件名称 2.获取文件的后缀名称 3.判断哪些文件类型允许上传 4.判断文件大小 5.满足条件后跳转后台实现上传 前台界面(验证上传文件是否格式满足要求): <body> <h2>文件上传</h2> <form action="upload01" method="post" 

文件上传

1.上传的步骤: a.导入SmartUpload.jar b.创建一个上传的类的对象 c.初始化 d.上传至服务器 e.保存 表单提交时需要指定enctype="multipart/form-data"(多数据类型提交) http://www.atguigu.com/opensource.shtml#3(包下载地址) package com.zuxia.servlet; import java.io.IOException;import java.io.PrintWriter; imp

python+selenium文件上传

1.input标签类元素文件上传 先定位到文件上传元素id,再使用方法send_keys(文件路径) 2.非input标签 备注:非input标签的文件上传,就不适用于此方法了,需要借助autoit工具或者SendKeys第三方库.

任意文件上传漏洞

漏洞产生原因:①代码层:开发者在编写代码的时候不严谨,未对文件上传的合法性进行检验: ②应用层:web容器漏洞,cgi,配置不当: 有网站到服务器上传文件的常用检测手段:①js(一般是检测文件后缀名)-可修改本地js代码或通过浏览器自带功能"No-script"进行绕过: ②服务器端MIME检测-对contenttype的额检测:   ③服务端目录路径检测,一般是检测目录路径是否合理,漏洞原因是对目录路径的检测不够严谨,可以用0x00截断进行攻击 ④服务器端文件拓展名检测绕过,分为白名

day20 文件上传

引入至easymall今天好累啊,就不一一拓展了 1. 文件上传步骤 实现web开发中的文件上传功能只需要两个步骤: (1)提供一个带有文件上传项的表单. (2)在servlet中读取处理上传的文件,保存到服务器中. 2. 文件上传实现 3.上传文件的监听 4.上传文件注意问题  

uploadify插件实现多文件上传

前台HTML代码: <div class="control-group" id="title-control-group"> <label class="control-label"><?=_('关于图片(*)')?></label> <div class="controls"> <div><input id="about_file_upl