分享一个简单的文件下载器

抽空写了一个用于下载文件的控制器类,只需要把文件的路径通过参数name传递到后台即可完成文件下载到本地,非常方便~

控制器类代码

package cn.edu.sgu.www.download.controller;

import cn.edu.sgu.www.download.entity.RequestURI;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * @author heyunlin
 * @version 1.0
 */
@RestController
@RequestMapping(produces = "application/json;charset=utf-8")
public class DownLoadController {

    /**
     * 下载单张图片
     * 接口请求路径:localhost:8083/download?name=文件路径
     */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(HttpServletRequest request, HttpServletResponse response)  {
        StringBuilder sb = new StringBuilder();
        ServletOutputStream outputStream = null;
        InputStream inputStream = null;

        try {
            String name = request.getParameter("name");
            RequestURI requestURI = parse(name);

            String fileName = requestURI.getName();

            sb.append(requestURI.getPath()).append(fileName);
            URL url = new URL(requestURI.getProtocol(), requestURI.getHost(), sb.toString());

            // 设置响应头
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            outputStream = response.getOutputStream();

            inputStream = url.openConnection().getInputStream();

            byte[] buffer = new byte[1024];
            int len;

            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(inputStream != null) {
                    inputStream.close();
                }
                if(outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 解析URL
     * @param name 文件url
     * @return RequestURI
     */
    private RequestURI parse(String name) {
        int index = name.indexOf("//"); // 第一个//的位置
        int firstIndex; // 第1个/的位置

        String host; // 主机
        String path = ""; // 文件路径
        String protocol; // 协议

        // 没有指定协议,默认为http
        if (index < 0) {
            firstIndex = name.indexOf("/"); // 第1个/的位置
            host = name.substring(0, firstIndex);
            protocol = "http";
        } else {
            firstIndex = name.indexOf("/", index + 2); // 第1个/的位置
            host = name.substring(index + 2, firstIndex);
            protocol = name.substring(0, index -1);
        }

        // 得到最后一个/的位置
        int lastIndex = name.lastIndexOf("/");

        if (firstIndex != lastIndex) {
            path = name.substring(firstIndex, lastIndex + 1);
        }

        String filename = name.substring(lastIndex + 1);

        System.out.println("协议 ==> " + protocol);
        System.out.println("服务器 ==> " + host);
        System.out.println("文件路径 ==> " + path);
        System.out.println("文件名 ==> " + filename);

        RequestURI requestURI = new RequestURI();

        requestURI.setProtocol(protocol);
        requestURI.setHost(host);
        requestURI.setPath(path);
        requestURI.setName(filename);

        return requestURI;
    }

}

实体类代码

package cn.edu.sgu.www.download.entity;

import lombok.Data;

/**
 * @author heyunlin
 * @version 1.0
 */
@Data
public class RequestURI {

    /**
     * 协议
     */
    private String protocol;

    /**
     * 主机域名
     */
    private String host;

    /**
     * 文件路径
     */
    private String path;

    /**
     * 文件名
     */
    private String name;
}

相关推荐

  1. 分享一个简单文件下载

    2024-06-07 08:54:03       8 阅读
  2. 简单文件管理

    2024-06-07 08:54:03       42 阅读
  3. 一个简单 C# 算术表达式 Eval 解析 MathEvalor

    2024-06-07 08:54:03       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-07 08:54:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-07 08:54:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-07 08:54:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-07 08:54:03       20 阅读

热门阅读

  1. 三步问题【python,算法,leetcode】

    2024-06-07 08:54:03       12 阅读
  2. SwiftUI三处理用户输入

    2024-06-07 08:54:03       8 阅读
  3. 浅析人工智能技术在网络安全领域中的应用

    2024-06-07 08:54:03       11 阅读
  4. 「C系列」C 运算符

    2024-06-07 08:54:03       10 阅读
  5. SASS模块化与组织文件

    2024-06-07 08:54:03       10 阅读
  6. yum进阶

    yum进阶

    2024-06-07 08:54:03      7 阅读
  7. Spring Boot:(十二)常用参数注解使用

    2024-06-07 08:54:03       11 阅读
  8. 常用Linux命令的具体使用示例

    2024-06-07 08:54:03       9 阅读
  9. python的df.describe()函数

    2024-06-07 08:54:03       8 阅读