java - 如何通过 url 链接“递归”读取所有文件和文件夹
问题描述
我正在尝试从 URL 下载所有文件。但是,通过chrome开发者工具分析代码,我可以看到我需要的文件在一个路径中,例如:/var/www/html/folders_and_files_that_i_need
.
有人有什么想法可以帮助我吗?提前致谢。
我的代码实际上只能下载文件设置绝对路径。
public static void downloadFile(String fileURL, String saveDir) throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10, disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
System.out.println("buffering");
}
outputStream.close();
inputStream.close();
System.out.println("Arquivo " + fileName + " baixado");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
httpConn.disconnect();
}
解决方案
在 REST API 中,您必须拥有单个文件的资源 Java 下载目录中的所有文件和文件夹
可以帮助你。
推荐阅读
- python - ScalarQueryParameter 返回类型错误:“列表对象不可调用”
- docker - Docker 是在桌面上运行的安全威胁吗?
- android - Gradle 偶尔会失败,并显示“任务 ':app:generateJsonModelRelease' 执行失败:格式说明符 '%s'”
- java - Android - Webview 获取页面加载时间
- codenameone - 如何在 CN1 中创建此用户界面?
- php - 假设 https 用于两者,PHP 会话是否比 ajax 调用更安全?
- sql-server - 如何为ssis的oledb目标中的列分配一个常量值
- java - 如何在 Asynctask 中创建 ImageView?
- django - 使用模态ID删除图像
- javascript - 如何使用 Immutable.js 在嵌套 Map 中添加新的键/值对