if I have a ftp server at ftp://ftpdomainname.com and I want to upload file to it, how do I check for its permissions using Commons Net ?
I try:
ftpClient = new FTPClient();
ftpClient.connect(url.getHost());
ftpClient.login(username, pass);
FTPFile[] directories = mFtpClient.listDirectories("/");
for(FTPFile dir : directories) {
permissions = dir.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION);
}
but it's not getting the permissions for the root server (folder) but for its subfolders.
Can i specify url like ftp://ftpdomainname.com/ and get whether I have write permissions? Or url like ftp://ftpdomainname.com/subfolder1/subfolder2/ ?
Thanks
Using Commons Net 3.1 FTPClient class, you could try obtaining current FTPFile by issuing the following command
FTPFile current = mFTPClient.listFiles(".")[0];
// Then check permissions as your upper code does.
As, after all, on most hosts, the current file can be accessed through ".".
EDIT some clarifications according to comment.
Suppose you just connected to your FTP server (before to try to upload file) :
ftpClient = new FTPClient();
ftpClient.connect(url.getHost());
ftpClient.login(username, pass);
Once it is done, immediatly try to get access to current folder (which should be your default user folder, in other words the folder where you want to upload your file) :
FTPFile current = mFTPClient.listFiles(".")[0];
In this folder, read permissions, and you'll have permissions for your root folder :
current.hasPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION);
Want to write something ? Then check the WRITE_PERMISSION.
Related
I want to add my file to a specific directory ,not root directory in my ftp server.
what can I do ? here is my java code :
FTPClient client = new FTPClient();
String filename = "out.txt";
// Read the file from resources folder.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = classLoader.getResourceAsStream(filename);
try {
client.connect("18.14.18.17");
boolean login = client.login("root", "root");
if (login) {
System.out.println("Login success...");
// Store file to server
client.storeFile(filename, is);
client.logout();
}
this code adds the file to my root directory in my ftp server.
I can use method client.changeWorkingDirectory("server directory").
I'm trying to upload some files on a shared folder where I'm granted on with full control, but I see that connection is not working for some authentication reasons. This is the piece of code I've used for some writing tests:
String destination = "serverX/shareFolder/";
String domain = "myDomain";
String smbFile = "smb://"+domain+"/user1:pwd1#"+destination;
SmbFile sFile = new SmbFile(smbFile);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write("Test".getBytes());
sfos.close();
this is the error I've received:
jcifs.smb.SmbAuthException: The referenced account is currently locked out and may not be logged on to.
Is looks like the code is good, the problem seems to be the account you are using. Have you tried other account? or maybe you can "unlock" the account someway at the server.
I need to connect to an FTP server and browse all files without using any libraries like apache.commons because I don't have the option to get these libraries at the moment.
I tried using a simple URL connection:
URL url = new URL("username:password#ip/folder/");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ( (line = reader.readLine()) != null ) {
System.out.println(line);
}
reader.close();
When I don't include the /folder/ it works, but it prints me many things that I can't even see in the home location of the FTP server, I don't know if it gives me the files or other data.
But when I do include /folder/ i get error CWD /folder/:550 failed to change directory
and I cannot change the directoy's permissions, its read-only and thats what I need.
What is wrong with it? Is it possible to do with just java's default libraries?
First, you need to use an FTP URL:
URL url = new URL("ftp:username:password#ip/folder/");
assuming username and password are substituted with their correct values.
Second, if you have FTP access to folder it will deliver you a directory listing in some format. If you don't, you need to study the exception message you get. If you omit /folder it will give you a listing of whatever the FTP server's default root directory is for that username. The code 550 means either an access problem or that the directory doesn't exist.
I am trying to download the files from ftp server using java FTPClient and FTPFile classes(commons-net.jar).How do i conclude if the file is partial or full(i.e., file is completely uploaded or not) using those classes?
Probably you could use the storeFile method form the Apache FTPClient:
public boolean storeFile(String remote, InputStream local)
throws IOException
Stores a file on the server using the given name and taking input from
the given InputStream. This method does NOT close the given
InputStream. If the current file type is ASCII, line separators in the
file are transparently converted to the NETASCII format (i.e., you
should not attempt to create a special InputStream to do this).
Parameters:
remote - The name to give the remote file. local - The
local - InputStream from which to read the file.
Returns: True if successfully completed, false if not.
Please note the following link Apache FTPClient for more information.
A simple implementation that you could try, would be something like this:
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// APPROACH #1: uploads first file using an InputStream
File firstLocalFile = new File("D:/Test/Projects.zip");
String firstRemoteFile = "Projects.zip";
InputStream inputStream = new FileInputStream(firstLocalFile);
System.out.println("Start uploading first file");
boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
inputStream.close();
if (done) {
System.out.println("The first file is uploaded successfully.");
}
...
}
Regards.
How would I create a file in another windows server? The server has a username, and password, ip address and specific directory.
SAMBA! Braziiillll, Braziiiiiiillll!
Something like this:
String userPass = "username:password";
String filePath = "smb://ip_address/shared_folder/file_name";
NtlmPasswordAuthentication authentication = new NtlmPasswordAuthentication(userPass);
SmbFile smbFile = new SmbFile(filePath, authentication);
SmbFileOutputStream smbFileOutputStream = new SmbFileOutputStream(smbFile);
PrintStream printStream = new PrintStream(smbFileOutputStream);
//You should be good from this point on...
NOTE: The destination folder needs to be shared first!
As #orm already points out, this is answered already here FTP upload via sockets
Basically, you could reuse an existing library like Apache Commons Net to do it. For the specifics of using the FTP client have a look at the documentation for the class FTPClient class.