第六天:SpringMVC文件上传和下载(代码篇)

上传:

前端:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>上传</title>

</head>
<body>
    <%--  multiple="multiple" 多选 --%>
    <form action="${pageContext.request.contextPath}/demo3001" method="post" enctype="multipart/form-data">
        <input type="file" name="fileInfo" multiple="multiple">
        <button>提交</button>
    </form>
</body>
</html>

后端:

package com.hxh.controller;

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 javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/*
    文件上传:
        1.form表单提交格式为post   enctype="multipart/form-data"
        2.设置上传下载解析器 + web.xml也要设置上传下载环境
        3.MultiparFile 接收资源文件
*/
@Controller
public class FileUpCoantroller {
    /*  demo3001
        步骤:
            1.接收上传对象
            2.创建 路径资源 对象
            3.将接收到的资源对象  复制到  路径资源对象上面去

        坑 : 使用 MultipartFile 必须加上一个注解@RequestParam
    */
    @RequestMapping("demo3001")
    public String demo3001(@RequestParam MultipartFile[] fileInfo, HttpServletRequest request) throws IOException {

        for (MultipartFile mm : fileInfo) {


            // 获取img路径
            String realPath = request.getSession().getServletContext().getRealPath("/img");

            // 得到资源名称
            String fileInfoName = mm.getOriginalFilename();

            // 路径资源对象
            File file = new File(realPath, fileInfoName);

            // 把接收到的资源对象  复制到路径资源对象上面去
            mm.transferTo(file);

            System.out.println("--上传成功--");

        }
        return "demo1002";
    }
}

下载:

前端:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>下载</title>
    <script  type="text/javascript" src="js/jquery.js"></script>

    <script type="text/javascript">

        $(function(){
            function dowload(Obj){
                window.location.href = "fileDownLoad?fileName="+Obj;
            }
        })

    </script>
</head>
<body>
<img src="4.jpg" href="javascript:dowload(this.val)">
</body>
</html>

后端:

package com.hxh.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;

@Controller
public class FileDownLoadController {

    /*
        下载
    */
    @RequestMapping("fileDownLoad")
    public void fileDownLoad(String fileName , HttpServletRequest request , HttpServletResponse response) throws IOException {

        // 设置浏览器的头 为下载模式
        response.setHeader("content-disposition","attachment;filename="
                + URLEncoder.encode(fileName,"UTF-8"));


        // 获取资源名称(图片名称)String fileName
        // 获取本地资源路径
        String realPath = request.getSession().getServletContext().getRealPath("/img");
        // 拼接出一个虚拟的资源对象
        File file = new File(realPath, fileName);

        // 想:将此图片放到浏览器
        ServletOutputStream os = response.getOutputStream();
        int len = -1;
        byte[] b = new byte[1024];
        FileInputStream fis = new FileInputStream(file);
        while ((len = fis.read(b))!= -1){
            os.write(b,0,len);
            os.flush();
        }

        fis.close();
        os.close();

    }

}
原文地址:https://www.cnblogs.com/masterhxh/p/13871567.html