|
|
@@ -8,10 +8,11 @@ import org.apache.commons.io.FilenameUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.*;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -48,7 +49,7 @@ public class ServiceFileUtil {
|
|
|
directoryPathAndName = createFilePath + "/" + extractFilename(directoryName);
|
|
|
Path file = Paths.get(directoryPathAndName);
|
|
|
if (Files.exists(file)) {
|
|
|
- logger.warn("处理文件失败:文件不存在: {}", directoryPathAndName);
|
|
|
+ logger.warn("处理文件失败:文件存在: {}", directoryPathAndName);
|
|
|
} else {
|
|
|
//直接创建目录
|
|
|
Path extractPath = Paths.get(directoryPathAndName);
|
|
|
@@ -72,14 +73,14 @@ public class ServiceFileUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
- //给定一个名称创建一个目录,返回路径
|
|
|
+ //创建子目录
|
|
|
public static String createDirectory(String directoryPath, String directoryName) {
|
|
|
String directoryPathAndName = null;
|
|
|
try {
|
|
|
directoryPathAndName = directoryPath + "/" + directoryName;
|
|
|
Path file = Paths.get(directoryPathAndName);
|
|
|
if (Files.exists(file)) {
|
|
|
- logger.warn("处理文件失败:文件不存在: {}", directoryPathAndName);
|
|
|
+ logger.warn("处理文件失败:文件已经存在: {}", directoryPathAndName);
|
|
|
} else {
|
|
|
//直接创建目录
|
|
|
Path extractPath = Paths.get(directoryPathAndName);
|
|
|
@@ -125,7 +126,8 @@ public class ServiceFileUtil {
|
|
|
Path dirPath = Paths.get(directoryPath);
|
|
|
if (!Files.exists(dirPath)) {
|
|
|
logger.warn("目录不存在: {}", directoryPath);
|
|
|
- throw new ServiceException("目录不存在: " + directoryPath);
|
|
|
+ //throw new ServiceException("目录不存在: " + directoryPath);
|
|
|
+ return;
|
|
|
}
|
|
|
Files.walk(dirPath)
|
|
|
.sorted(Comparator.reverseOrder())
|
|
|
@@ -305,6 +307,47 @@ public class ServiceFileUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 下载远程网络文件
|
|
|
+ * @param fileURL
|
|
|
+ * @param destinationPath
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static void downloadFile(String fileURL,String fileName, String destinationPath) throws IOException {
|
|
|
+ String encodeFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
|
|
|
+ String newUrl = fileURL.replaceAll("&fileName=[^&]*$", "");
|
|
|
+ // 拼接合法URL
|
|
|
+ String fullUrl = newUrl + "&fileName=" + encodeFileName;
|
|
|
+ URL url = new URL(fullUrl);
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ connection.setConnectTimeout(5000); // 设置连接超时时间
|
|
|
+ connection.setReadTimeout(5000); // 设置读取超时时间
|
|
|
+ int contentLength = connection.getContentLength(); // 获取文件大小
|
|
|
+
|
|
|
+ System.out.println("Downloading file of size: " + contentLength + " bytes");
|
|
|
+
|
|
|
+ //目录路径许哟啊拼接文件名称
|
|
|
+ destinationPath += "/"+ fileName;
|
|
|
+
|
|
|
+ try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
|
|
|
+ FileOutputStream out = new FileOutputStream(destinationPath)) {
|
|
|
+
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int bytesRead;
|
|
|
+
|
|
|
+ while ((bytesRead = in.read(buffer)) != -1) {
|
|
|
+ out.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println("Download completed.");
|
|
|
+ } finally {
|
|
|
+ connection.disconnect();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
@@ -322,7 +365,14 @@ public class ServiceFileUtil {
|
|
|
public static void main(String[] args) {
|
|
|
System.out.println("==========start=============");
|
|
|
try {
|
|
|
- test();
|
|
|
+
|
|
|
+ String baseUrl = "http://10.224.130.138:7071/aplanmis-rest/province/downDzzzFileByFileId?fileId=605028ecf919500a380acc5a&fileName=阳光三路工程规划许可证申请表.pdf";
|
|
|
+ String fileName = "阳光三路工程规划许可证申请表.pdf";
|
|
|
+
|
|
|
+ String savePath = "C:\\home\\siwei\\uploadPath\\2026\\06\\13\\建设用地规划许可_20260613182743A035_20260613182743A036\\建设工程规划许可证申请表";
|
|
|
+ downloadFile(baseUrl, fileName, savePath);
|
|
|
+
|
|
|
+ downloadFile(baseUrl,fileName,savePath);
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|