七、Spring Boot 实现本地文件下载

  • 此项目只能从指定的地址中下载指定文件夹中文件。

一、项目结构
七、Spring Boot 实现本地文件下载_第1张图片
二、HTML页面:fileDownload.html



    
    文件下载示例


文件下载示例


文件:

三、转发文件:ToHtmlController.java

package com.example.demo;

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

/**
 * @author ${fwc}
 * @data 2019/4/18
 * @time 17:32
 */
@Controller
public class ToHtmlController {

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public String Download() {
        return "/fileDownload";

    }
}

四、控制器文件

package com.example.demo;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author ${fwc}
 * @data 2019/4/20
 * @time 8:51
 */
@Controller
public class DownLoadController {
    //@RequestMapping(value = "/testDownload", method = RequestMethod.GET)
    @RequestMapping("/testDownload")
    /**
    *文件下载相关代码
     */
    public String downloadFile(@RequestParam("fileUpload") MultipartFile file2,HttpServletRequest request, HttpServletResponse response) {
        // 选择下载的文件
        String fileName = file2.getOriginalFilename();
        //选择下载文件的
        if (fileName != null) {
            //设置文件路径
            String realPath = "D:\\JavaSubject\\video\\学习阶段二\\java进阶\\temp";
            File file = new File(realPath , fileName);
            if (file.exists()) {
                // 设置强制下载不打开
                response.setContentType("application/force-download");
                // 设置文件名
                response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }
}

五、测试

  • 在浏览器中输入localhost:8080/download

七、Spring Boot 实现本地文件下载_第2张图片

你可能感兴趣的:(七、Spring Boot 实现本地文件下载)