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

抽空写了一个用于下载文件的控制器类,只需要把文件的路径通过参数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;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值