I have looked through the documentation and I can't seem to find a function which does this. So, I assume that I should code it myself. Looking further into the documentation, I found a list() function which lists all the files in a working directory. How would I download all the files while preserving the directory structure?
After you connect to the server:
FTPClient client = new FTPClient();
client.connect(host);
client.login(user, pass);
You change to the desired folder
client.changeDirectory(ftpFolder);
and then you request the list of files:
FTPFile[] list = client.list();
Iterate the file array of results and download the file.
using:
FTPFile[] list = client.list();
for (int i = 0; i < list.length; i++)
{
//client.download("localFile", new java.io.File("remotefile);
client.download(list[i].getName(), new java.io.File(list[i].getName());
}
Related
I want to start by saying that I am very new to java so I am having issues finding resources. I am trying to find a library that will allow for me to access files in a Moto G4 phone. I have found plenty of examples of how to do this but they never say what library they are using. The code I am interested in is the following: (Note that this is an example and not what I will be doing with these files. I just want to get this script to run)
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}
This was pulled from another post on how to find files on an Android phone. Any help would be greatly appreciated. It is important to note that this is just so that I can gain access to these files so that I can move them to my hard drive.
I am writing a program where I am backing up a bunch of folders and files based on a file that contains the paths. The issue arrives when I am opening files, I keep finding "My Music" within Documents. This file does not exist from what I can see. I checked through cmd and looked at other places but it keeps cropping up. I have tried file.exists, get that it does and the when I try to list what's inside it I get a nullPointer from it not existing.
`
public static void enterFolder(String path, String runningPath) throws IOException {
//opens folder
File f = new File(path);
//check to see if folder exists I check in debug
boolean food = f.exists();
//get an array of the stuff inside
File[] list = f.listFiles();
DataOutputStream output;
DataInputStream input;
input = new DataInputStream(connection.getInputStream());
output = new DataOutputStream(connection.getOutputStream());
//loops through what is inside breaks here with nullPointer because list uninitialized
for (int i = 0; i < list.length; i++) { `
path is C:\Users\Tallennar\Documents\My Music
and runningPath is C:\Users\Tallennar\Documents\My Music
So after after a lot of research, the My Music folder is a Directory Junction Point by Windows. This is so that XP programs are compatible with the newer versions of Windows. My solution was to delete these files because I personally do not have any programs from XP. How I deleted them was I went into the options in the view tab of File Explorer. I then unchecked the Hide System files in the view tab of the options. I then I highlighted the files and deleted them and set the options back to normal.
Link to the XP junction information:
Phantom Folder Info
we can extract all the files from a zoip filder using extractAll method given in zip4j, but what if i need to extract only one kind of files,say only text files or only files which have a certain sub-string in the name of the file?? is there a way to do this using zip4j
i thought this question might be relating to my problem
Read Content from Files which are inside Zip file
but that's not exactly what i want.
can anyone explain in detail about using this ZipEntry things, if it helps my problem getting solved?
Try the below code
ZipFile zipFile = new ZipFile("myzip.zip");
// Get the list of file headers from the zip file
List fileHeaderList = zipFile.getFileHeaders();
// Loop through the file headers
for (int i = 0; i < fileHeaderList.size(); i++) {
FileHeader fileHeader = (FileHeader)fileHeaderList.get(i);
String fileName = fileHeader.getFileName();
if(fileName.contains(".java")){
zipFile.extractFile(fileHeader, "c:\\scrap\\");
}
}
I am trying to create a method that displays a list of files in a given directory. This works fine for normal directories (on disk) but when I enter a url my list of files is null.
public void getListOfFiles(String folderLocation){
File folder = new File(folderLocation);
File[] listFiles = folder.listFiles();
for(int i = 0; i < 10; i++){
System.out.println(listFiles[i]);
}
}
I think my problem is because the File 'folder' is removing one of the '/' in my folderLocation (http://...)
I have tried using URL and URI but have had no luck! Can anyone help?
First of all, File won't work for this as it's not networking-aware.
Secondly, in general there's no mechanism to list files over plain HTTP. If the HTTP server gives you some kind of a listing page when you present it with the URL, you'll have to download the page using, for example, URLConnection and parse it yourself.
To list files over FTP, you could use FTPClient from Apache Commons Net.
I want to know the directory structure of a particular directory on the webserver. I can login through putty (by providing userid and password) but i want to do the code in java in my local system and print the directory structure for the same. I did LS -R in putty to get the same but it is very huge so i want this same to print in java console.
Can anyone please suggest in how to do?
Edit:
Let me explain below is the code for getting the directory structure
static void showDir(int indent, File file) throws IOException {
for (int i = 0; i < indent; i++) {
System.out.print('-');
System.out.println(file.getName());
}
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++){
showDir(indent + 4, files[i]);
}
}
}
but this prints for my local system. I want this to be work for remote server. I have the IP of server.
Expanding on Chris Kuehl's comment about SSH-ing with Java, you may want to look into one of these:
JSch
Ganymed SSH-2
I would assume (but I haven't looked into it) that with one of these one could feasibly write code that SSH's into a machine, then executes commands on it, capturing the output.
It may be as simple as loading one of those libraries, writing code that connects via SSH to the machine, executes and captures the output of: cd your_dir, ls -R, then prints it.