当前位置: 首页 > news >正文

Java下载文件(特殊字符编码处理)

当你在这个问题上花费了数小时而解决不了,你才会知道这篇文章对你的帮助

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;public class Test {@AutowiredRestTemplate restTemplate;public void test(HttpServletResponse response) {String serverPrefix = "http://aa.com:8080";String filePath = "/testo1/test2/測武特殊字付@#&$%^~+[】・副本一副本・pom";HttpHeaders headers = new HttpHeaders();headers.add("Authorization", "Auth");URI uri = UriComponentsBuilder.fromHttpUrl(serverPrefix).pathSegment(filePath.split("/")).build().encode().toUri();ResponseEntity<Resource> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(headers), Resource.class);if (responseEntity.getStatusCode().is2xxSuccessful() && responseEntity.hasBody()) {try (InputStream input = responseEntity.getBody().getInputStream();OutputStream output = response.getOutputStream()) {StreamUtils.copy(input, output);response.setContentLengthLong(responseEntity.getBody().contentLength());} catch (IOException e) {throw new RuntimeException("流传输异常", e);}} else {throw new RuntimeException("请求失败:" + responseEntity.getStatusCode());}}
}
http://www.xdnf.cn/news/905113.html

相关文章:

  • 基于React + FastAPI + LangChain + 通义千问的智能医疗问答系统
  • QT: `long long` 类型转换为 `QString` 2025.6.5
  • ruoyi-plus-could 负载均衡 通过 Gateway模块配置负载均衡
  • Curtain MonGuard:智能水印颜色适配,提升屏幕信息安全
  • LabVIEW实时系统数据监控与本地存储
  • C++ 基础特性深度解析
  • 化学小工具之OpenBabel
  • idea中 maven 本地仓库有jar包,但还是找不到,解决打包失败和无法引用的问题———————————————— 版权声明:本文为博
  • 第16节 Node.js 文件系统
  • MySQL性能调优:Mysql8高频面试题汇总
  • Elasticsearch集群手动分片分配指南:原理与实践
  • Python实现快速排序的三种经典写法及算法解析
  • 【知识扫盲】如何由inq,ouq和totaltime计算tokens/s
  • 栈的概念以及实现
  • SOC-ESP32S3部分:32-LVGL显示框架
  • ComfyUI 工作流
  • Numpy 之 reshape 教程
  • 【OpenGL学习】(五)自定义着色器类
  • Redis知识
  • 强化学习基础概念图文版笔记
  • 【QT常用技术讲解】多线程执行后台命令行的两种方式(后台运行和返回打印信息)
  • 【Linux】grep 命令详解及使用示例:搜索匹配指定模式的文本行
  • 【JJ斗地主-注册安全分析报告】
  • 20250606-C#知识:匿名函数、Lambda表达式与闭包
  • 动态IP与静态IP:数字世界的“变脸术”与“身份证”
  • CSS 轮廓(Outline)与边框(Border)的深度解析
  • 【Zephyr 系列 12】BLE + NVS + 低功耗融合实战:打造可配置蓝牙信标系统
  • Codeforces EDU Round 179 A~D
  • 【自动驾驶避障开发】如何让障碍物在 RViz 中‘显形’?呈现感知数据转 Polygon 全流程
  • AlphaDrive:通过强化学习和推理释放自动驾驶中 VLM 的力量