SpringMvc的文件上传与文件下载(源码)

1.  首先先配置web.xml:

<web-app
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID" version="3.1">
<!--  <welcome-file-list>-->
<!--    <welcome-file>index.jsp</welcome-file>-->
<!--  </welcome-file-list>-->

  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <filter>
    <filter-name>fileencoding</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>fileencoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

2.创建springmvc.xml并配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <!--自动扫描包-->
<context:component-scan base-package="zpy.controller"></context:component-scan>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

    <!--注入视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀和后缀-->
        <!--把页面放在什么地方-->
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize" value="10240000"/>
     </bean>
</beans>

3.  index.jsp页面:写的多个上传,不需要的话就把另外两个文件上传删了,另外等会的Contorller页面里的for循环以及里面的[i]删掉即可

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传</title>
</head>
<body>
<form action="api/upload" method="post" enctype="multipart/form-data">
    文件上传 <input type="file" name="file"><br/>
    文件上传 <input type="file" name="file"><br/>
    文件上传 <input type="file" name="file"><br/>
       <input type="submit" value="上传">


    <a href="api/download?fileName=zpy.jpg">下载</a>
</form>
</body>
</html>

4.  Controller页面代码:    上面是上传下面的是下载

package zpy.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Random;

@Controller
@RequestMapping("/api")
public class HiContorller {


    @RequestMapping("/upload")
    public String Uploading(MultipartFile[] file, HttpServletRequest request) {
        for (int i = 0; i < file.length; i++) {
            //获取上传的路经
            String path = request.getServletContext().getRealPath("/upload/");

            //拿到页面上传过来的文件
            String name = file[i].getOriginalFilename();

            //拿到文件名改名
            String newName = new Date().getTime() + new Random(9999999).nextInt() + name;

            //上传的路径
            File f = new File(path + newName);
            System.out.println(f);
//    System.out.println(path+newName);

            //上传

            try {
                file[i].transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "show";
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(@RequestParam("fileName") String fileName, HttpServletRequest request) throws IOException {



        //下载的路径
        String path = request.getServletContext().getRealPath("/download/");

        //下载的完整路径
        File f = new File(path + fileName);

        //转格式

        String name = new String(fileName.getBytes("utf-8"), "iso-8859-1");

        HttpHeaders hh = new HttpHeaders();

        hh.setContentDispositionFormData("attachment", name);

        hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(f),hh, HttpStatus.CREATED);
    }
}

5.   注意要创建俩个文件夹以及idea的webapp下面也要创建相同的文件夹:

分别是:上传   :  download      

               下载   :  upload

另外需要注意的是要在tomcat的文件夹里去创建这两个文件夹

以上就是上传下载的全部内容了,希望能帮助到大家。

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
源码是经过本地编译可运行的,下载完成之后配置相应环境即可使用。源码功能都是经过老师肯定的,都能满足要求,有需要放心下载即可。源码是经过本地编译可运行的,下载完成之后配置相应环境即可使用。源码功能都是经过老师肯定的,都能满足要求,有需要放心下载即可。源码是经过本地编译可运行的,下载完成之后配置相应环境即可使用。源码功能都是经过老师肯定的,都能满足要求,有需要放心下载即可。源码是经过本地编译可运行的,下载完成之后配置相应环境即可使用。源码功能都是经过老师肯定的,都能满足要求,有需要放心下载即可。源码是经过本地编译可运行的,下载完成之后配置相应环境即可使用。源码功能都是经过老师肯定的,都能满足要求,有需要放心下载即可。源码是经过本地编译可运行的,下载完成之后配置相应环境即可使用。源码功能都是经过老师肯定的,都能满足要求,有需要放心下载即可。源码是经过本地编译可运行的,下载完成之后配置相应环境即可使用。源码功能都是经过老师肯定的,都能满足要求,有需要放心下载即可。源码是经过本地编译可运行的,下载完成之后配置相应环境即可使用。源码功能都是经过老师肯定的,都能满足要求,有需要放心下载即可。源码是经

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值