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

ops/2024/10/22 16:50:09/

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

控制器类代码

java">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; // 协议// 没有指定协议,默认为httpif (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;}}

实体类代码

java">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;
}

http://www.ppmy.cn/ops/47963.html

相关文章

【C语言】一篇带你高强度解析精通 字符串函数和内存函数 (万字总结大全,含思维导图)(建议收藏!!!)

【 库函数】——字符串函数和内存函数 目录 思维导图&#xff1a; 一&#xff1a;字符串函数 1.1&#xff1a;字符串常规函数 1.1.1&#xff1a;长度不受限制的字符串函数 1.1.1.1&#xff1a;strlen函数 1.1.1.2&#xff1a;strcpy函数 1.1.1.3&#xff1a;strcat函数 …

webman中创建udp服务

webman是workerman的web开发框架 可以很容易的开启udp服务 tcp建议使用gatewayworker webman GatewayWorker插件 创建udp服务: config/process.php中加入: return [// File update detection and automatic reloadmonitor > [ ...........], udp > [handler > p…

C#的线程锁

在 C# 中&#xff0c;线程锁是用于确保在多线程环境下对共享资源进行安全访问的机制。最常见的线程锁是通过 lock 关键字实现的。 使用 lock 关键字实现线程锁 class Example {private object lockObject new object();private int count 0;public void IncrementCount(){l…

【Excel技巧】Excel打开密码的两种设置方法!

excel文件打开密码可以再打开文件时输入密码查看文件内容&#xff0c;这样就可以保护文件内容不被任何人查看了&#xff0c;今天分享excel打开密码的两种设置方法给大家。 方法一&#xff1a; 点击excel中的【文件】功能&#xff0c;找到【信息】-【保护工作表】-【用密码进行…

WebGL开发时尚设计系统

开发一个基于WebGL的时尚设计系统可以为用户提供一个互动、实时的3D体验&#xff0c;允许他们设计和试穿虚拟服装。这个系统可以广泛应用于时尚设计、电子商务、虚拟试衣间等领域。以下是开发此系统的主要步骤和关键技术。北京木奇移动技术有限公司&#xff0c;专业的软件外包开…

经纬恒润助力红旗转向技术新突破

近日&#xff0c;红旗研发新视界发布《国内首发&#xff01;红旗大输出力冗余平行轴式电动助力转向器让用户出行经济又安全&#xff01;》 &#xff0c;创新突破“输出力20kN以上的冗余平行轴式电动助力转向器&#xff08;R-EPS&#xff09;”。该产品支持整车实现L2/L3级自动驾…

k8s使用yml文件部署

1.K8S可以使用yml部署资源 apiVersion: apps/v1 kind: Deployment metadata:name: gateway-app spec:replicas: 1selector:matchLabels:app: gateway-apptemplate:metadata:labels:app: gateway-appspec:containers:- name: gateway-appimage: registry.cn-beijing.aliyuncs.c…

利用Axios封装及泛型实现定制化HTTP请求处理

本案例旨在教授如何使用Axios库结合TypeScript泛型进行HTTP请求的高级封装&#xff0c;以提升代码的可复用性和类型安全性。我们将通过一个具体的示例&#xff0c;学习如何创建一个通用的请求函数&#xff0c;它能够适应不同类型的API响应&#xff0c;并在请求前后加入自定义逻…