|
@@ -0,0 +1,55 @@
|
|
|
+package com.gis;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+
|
|
|
+public class DownloadFile {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String fileURL = "https://ossxiaoan.4dage.com//720yun_fd_manage/image/20250224_152717258.jpg?d=174038203732";
|
|
|
+
|
|
|
+ try {
|
|
|
+ URL url = new URL(fileURL);
|
|
|
+ // 解析URL得到路径部分,去除查询参数
|
|
|
+ String filePath = url.getPath();
|
|
|
+ // 获取查询参数部分,如果不需要可以忽略这行
|
|
|
+ String query = url.getQuery();
|
|
|
+
|
|
|
+ // 假设基础保存目录在C盘
|
|
|
+ String baseDir = "c:\\";
|
|
|
+ // 构建完整的保存路径
|
|
|
+ String saveFilePath = baseDir + filePath;
|
|
|
+
|
|
|
+ HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
|
|
|
+ int responseCode = httpConn.getResponseCode();
|
|
|
+
|
|
|
+ // 检查HTTP响应码是否为200表示请求成功
|
|
|
+ if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
+ InputStream inputStream = httpConn.getInputStream();
|
|
|
+ // 确保目录存在
|
|
|
+ File file = new File(saveFilePath);
|
|
|
+ File dir = file.getParentFile();
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs(); // 创建所有必要的父目录
|
|
|
+ }
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(file);
|
|
|
+
|
|
|
+ int bytesRead;
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+
|
|
|
+ outputStream.close();
|
|
|
+ inputStream.close();
|
|
|
+
|
|
|
+ System.out.println("文件下载完成: " + saveFilePath);
|
|
|
+ } else {
|
|
|
+ System.out.println("无法下载文件,服务器响应码:" + responseCode);
|
|
|
+ }
|
|
|
+ httpConn.disconnect();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|