通过地址请求获取文件流,并将其保存到本地
package cn.oyun.speech.module.system.service; import java.io.IOException; import java.io.OutputStream; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; public class FileDownload { public static void download(String keyId) { String url = "你的请求地址" + keyId; HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url)) .GET() .build(); try { HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray()); if (response.statusCode() == 200) { String fileName = "D:\\upload\\" + keyId + ".docx"; try (OutputStream os = java.nio.file.Files.newOutputStream(java.nio.file.Paths.get(fileName))) { os.write(response.body()); System.out.println("识别结果已下载到 " + fileName); } } else { System.out.println("下载失败: " + response.body()); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { String keyId = "12345"; download(keyId); } }
标签:keyId,http,String,java,doc,import,response,下载 From: https://www.cnblogs.com/1gaoyu/p/18601973