首页 / JAVA / Java下载目录中的所有文件和文件夹
Java下载目录中的所有文件和文件夹
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java下载目录中的所有文件和文件夹,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4456字,纯文字阅读大概需要7分钟。
内容图文
![Java下载目录中的所有文件和文件夹](/upload/InfoBanner/zyjiaocheng/824/00bc24fb3d7f4726a6dd1c4389ef7df1.jpg)
我正在尝试从此目录下载所有文件.但是,我只能将它作为一个文件下载.我能做什么?我试图搜索这个问题,这让人感到困惑,人们开始建议使用httpclients.感谢您的帮助,这是我的代码到目前为止.有人建议我使用输入流来获取目录中的所有文件.那么那会进入阵列吗?我在http://docs.oracle.com/javase/tutorial/networking/urls/尝试了这个教程,但它没有帮助我理解.
//ProgressBar/Install
String URL_LOCATION = "http://www.futureretrogaming.tk/gamefiles/ProfessorPhys/";
String LOCAL_FILE = filelocation.getText() + "\\ProfessorPhys\\";
try {
java.net.URL url = new URL(URL_LOCATION);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/4.76");
//URLConnection connection = url.openConnection();
BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
int available = stream.available();
byte b[]= new byte[available];
stream.read(b);
File file = new File(LOCAL_FILE);
OutputStream out = new FileOutputStream(file);
out.write(b);
} catch (Exception e) {
System.err.println(e);
}
我还发现这个代码将返回要下载的文件列表.有人可以帮我合并这两个代码吗?
public class GetAllFilesInDirectory {
public static void main(String[] args) throws IOException {
File dir = new File("dir");
System.out.println("Getting all files in " + dir.getCanonicalPath() + " including those in subdirectories");
List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for (File file : files) {
System.out.println("file: " + file.getCanonicalPath());
}
}
}
解决方法:
你需要下载页面,这是目录列表,解析它,然后下载链接在页面中的inidiviudal文件…
你可以做点什么……
URL url = new URL("http:www.futureretrogaming.tk/gamefiles/ProfessorPhys");
InputStream is = null;
try {
is = url.openStream();
byte[] buffer = new byte[1024];
int bytesRead = -1;
StringBuilder page = new StringBuilder(1024);
while ((bytesRead = is.read(buffer)) != -1) {
page.append(new String(buffer, 0, bytesRead));
}
// Spend the rest of your life using String methods
// to parse the result...
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
is.close();
} catch (Exception e) {
}
}
或者,您可以下载Jsoup并使用它来完成所有艰苦工作……
try {
Document doc = Jsoup.connect("http:www.futureretrogaming.tk/gamefiles/ProfessorPhys").get();
Elements links = doc.getElementsByTag("a");
for (Element link : links) {
System.out.println(link.attr("href") + " - " + link.text());
}
} catch (IOException ex) {
ex.printStackTrace();
}
哪个输出……
?C=N;O=D - Name
?C=M;O=A - Last modified
?C=S;O=A - Size
?C=D;O=A - Description
/gamefiles/ - Parent Directory
Assembly-CSharp-Editor-firstpass-vs.csproj - Assembly-CSharp-Edit..>
Assembly-CSharp-Editor-firstpass.csproj - Assembly-CSharp-Edit..>
Assembly-CSharp-Editor-firstpass.pidb - Assembly-CSharp-Edit..>
Assembly-CSharp-firstpass-vs.csproj - Assembly-CSharp-firs..>
Assembly-CSharp-firstpass.csproj - Assembly-CSharp-firs..>
Assembly-CSharp-firstpass.pidb - Assembly-CSharp-firs..>
Assembly-CSharp-vs.csproj - Assembly-CSharp-vs.c..>
Assembly-CSharp.csproj - Assembly-CSharp.csproj
Assembly-CSharp.pidb - Assembly-CSharp.pidb
Assembly-UnityScript-Editor-firstpass-vs.unityproj - Assembly-UnityScript..>
Assembly-UnityScript-Editor-firstpass.pidb - Assembly-UnityScript..>
Assembly-UnityScript-Editor-firstpass.unityproj - Assembly-UnityScript..>
Assembly-UnityScript-firstpass-vs.unityproj - Assembly-UnityScript..>
Assembly-UnityScript-firstpass.pidb - Assembly-UnityScript..>
Assembly-UnityScript-firstpass.unityproj - Assembly-UnityScript..>
Assembly-UnityScript-vs.unityproj - Assembly-UnityScript..>
Assembly-UnityScript.pidb - Assembly-UnityScript..>
Assembly-UnityScript.unityproj - Assembly-UnityScript..>
Assets/ - Assets/
Library/ - Library/
Professor%20Phys-csharp.sln - Professor Phys-cshar..>
Professor%20Phys.exe - Professor Phys.exe
Professor%20Phys.sln - Professor Phys.sln
Professor%20Phys.userprefs - Professor Phys.userp..>
Professor%20Phys_Data/ - Professor Phys_Data/
Script.doc - Script.doc
~$Script.doc - ~$Script.doc
~WRL0392.tmp - ~WRL0392.tmp
~WRL1966.tmp - ~WRL1966.tmp
然后,您需要为每个文件构建一个新URL,并按照您已经完成的方式进行读取…
例如,Assembly-CSharp-Edit的href ..>是Assembly-CSharp-Editor-firstpass-vs.csproj,它出现在一个相对链接中,所以你需要在http://www.futureretrogaming.tk/gamefiles/ProfessorPhys前面加上这个来创建一个新的http:// URL www.futureretrogaming.tk/gamefiles/ProfessorPhys/Assembly-CSharp-Editor-firstpass-vs.csproj
您需要为要抓取的每个元素执行此操作
内容总结
以上是互联网集市为您收集整理的Java下载目录中的所有文件和文件夹全部内容,希望文章能够帮你解决Java下载目录中的所有文件和文件夹所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。