我正在尝试从此目录下载所有文件。但是,我只能将 url 作为一个文件下载。我能做些什么?我尝试搜索这个问题,结果令人困惑,人们开始建议改用 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());
}
}
}