I'm working on Android app.
how can i create a new directory in a remote host in java?
I tried this but it doesn't work:
String newFolder = "http://www.mysite.it/public/newfolder";
File outFile = new File(newFolder);
if(!outFile.exists()){
boolean b = outFile.mkdirs();
}
Thank you so much!
It can not be done this way. You need to send a command to your server and create the directory from your server.
Then you can send an acknowledgement back to your client
First of all: The folder is remote.
When using File you are accessing the local file system.
To connection to a remote host, you will need a URLConnection, HTTPUrlConnection or sockets.
So the file constructed using your string won't represent a valid file.
Creating folders on a remote host require the use of protocols such as ssh or ftp.
As an example:
URL url = new URL("ftp://mirror.csclub.uwaterloo.ca/index.html");
URLConnection urlConnection = url.openConnection();
would open a ftp - connection to host mirror.csclub.uwaterloo.ca requesting a file named index.html.
command - line ftp clients can send commands for creating folders to the remote host, e.g.
ftp anonoumos#somehost.com
# mkdir folderName
so you would have to send this command to your ftp host. For ssh it's basically the same procedure.
Both requires a basic client, though.
There are existing solutions around in the Java World, but I don't know of any for Android.
So while uploading a file to a host via ftp is pretty straight forward, (take a look at this example http://www.codejava.net/java-se/networking/ftp/upload-files-to-ftp-server-using-urlconnection-class), sending commands via ftp seems to be a lot more complicated task.
org.apache.commons.net.ftp.FTPClient e.g. supports sending commands via it's doCommand - method, but I don't know if you get it to work on android.
Related
I am making a FTP client using apache.commons.vfs and I want to add the copy and paste functionality for the files in the remote server.
I used the copyFrom method to copy the files:
destFile.copyFrom(srcFile, Selectors.SELECT_ALL)
The problem is that in this way the data goes from remote server to the FTP client and from there back to the remote server, making the copy speed very slow compared to copying them by the cp command.
Is there a way in apache.commons.vfs to do the copy using the host commands instead of network transfers?
I was wondering if there is a way to copy files on an SFTP server to another directory on the same SFTP server. I want to do this without getting the file in a client and then setting it in the other folder. Of course this would work fine but I guess that this would produce more overhead, so I would like to avoid this if at all possible. I'm currently working with Spring integration which is based on JCraft JSch.
So far I haven't been able to find any way to do this without an intermediary.
Another approach would be to open an SSH channel and just use the cp command but well that's not too pretty either in my opinion.
Thanks in advance!
A core SFTP protocol does not support duplicating a remote file.
There's a draft of copy-file extension to the protocol, but that's supported by only few SFTP servers (ProFTPD mod_sftp and Bitvise SFTP server for example).
In the most widespread OpenSSH SFTP server it is supported only by very recent version 9.0.
And it's also not supported by the JSch library.
See also my answer to How can I copy/duplicate a file to another directory using SFTP?
So actually using the cp shell command over an "exec" channel (ChannelExec) is unfortunately the best available approach (assuming you connect to a *nix server and you have a shell access).
If you do not have a shell access, then your only option is indeed to download the file to a local temporary folder and upload it back to the new location (or use streams, to avoid a temporary file). See also:
How do I transfer a file from one directory to another using Java SFTP Library JSch?
How do I copy files stored in a remote SFTP server to another folder in the same remote server using Java?
I am not authorized to use ssh/sftp( using private/public key). So ftp is my only choice.
The following piece of code works just fine for me, fetching the file from Unix box, but my motto is to log in to a UNIX box from windows, using java,then from my home directory go to a different directory and use grep, then copy that output back to my windows java program. I was looking for how to execute some Unix command in the box. as we do it in shell/python/ant...
new URL("ftp://user:password#url/sourcefile;type=i");
URLConnection con = url.openConnection();
BufferedInputStream in =
new BufferedInputStream(con.getInputStream());
FileOutputStream out =
new FileOutputStream("Targetfile");
If you have username and password then you can go for Jsch library.
Have a look at this or directly run it !!
http://www.jcraft.com/jsch/examples/Shell.java.html
Similarly you can all shell commands from this.
FTP is a file transfer protocol. It's not a general-purpose remote access protocol. It doesn't have built-in support for a client to run arbitrary commands on the FTP server.
FTP does have a command called SITE which permits running custom commands on the server. To use it, the FTP server's administrator would have to set up a custom command that meets your needs. Then you'd have to use a real FTP client library to invoke the site command on the remote server--calling openConnection() on an FTP URL won't let you invoke site commands.
I am using the Apache commons net FTPClient to distribute files as part of batch jobs. The client is encapsulated, but roughly I issue the following method calls (i have omitted error handling, stream handling etc. for brevity):
FTPClient client = new FTPClient();
client.connect("127.0.0.1", 22);
client.login("user", "password");
client.setFileType(FTP.BINARY_FILE_TYPE);
client.changeWorkingDirectory("path/");
client.storeFile("file1.txt", new ByteArrayInputStream("hello world".getBytes()));
client.disconnect();
When I issue the commands above, The following commands are sent to the FTP server:
USER zoor
PASS ********
TYPE I
CWD path/
PORT 127,0,0,1,229,72
STOR file1.txt
And this works fine. Now, however, I am in a situation where i need to send files to a legacy system which does not support CWD or STOR but instead relies on CD and PUT, respectively..
Now my question is, how do I make the FTPClient use these commands?
I could use the sendCommand method to send the CD command, but I am not sure how to do this properly (and correctly) with the PUT command (given that I must send the file data also).
NOTE: I realize that there are better ways than FTP to distribute files like this, but unfortunately, my hands are bound. The recipients are using FTP and are not willing to change.
UPDATE: The legacy system is an AS/400 machine.
It occurred to me that CD and PUT are not actually FTP commands and as such the question makes little sense.. The solution (because we need it to work ASAP) is that we transfer the files to our own FTP server, and call a script via SSH on that server that transfers the files to the AS/400 machine.
This should work, I understand you worked around the issue, but for sake of completeness, do you know if AS/400 needs any site commands to store the file.
What is the error your getting back?
I regularly send files to MVS / Z/OS and there are a few site commands that need to be sent first for storage and setup.
Site commands would look something like:
TRAIL
CYLINDERS PRIMARY=350 SECONDARY=300
RECFM=FB LRECL=1516 BLKSIZE=31836
CONDDISP=delete
Here's a little code that I use, ftp is essentially the FTPClient that you use in your example.
public boolean sendSiteCommand(String siteCmd){
if (null == siteCmd){
System.err.println("Warning, Null Parameter sent to sendSiteCommand, aborting...");
return false;
}
try{
return ftp.sendSiteCommand(siteCmd);
}catch (Exception e){
System.out.println("Error Occurred Sending Site Command to Remote System, aborting...");
e.printStackTrace(System.err);
return false;
}
}
If you truly need to upload a file as another command, you can always extend FTPClient and call the internal methods with a different command. They are built that you can give any string as the command to the already existing stores.
Example:
public static class OldFtpClient extends FTPClient {
public OutputStream putFileStream(String remote) throws IOException {
return _storeFileStream("PUT", remote);
}
}
Source of class can be found at https://github.com/apache/commons-net/blob/master/src/main/java/org/apache/commons/net/ftp/FTPClient.java .
I want to establish a connection with my UNIX file system using java program.. So that I can make some File I/O operations and normally I can connect using Putty.
How can I do the same using java program
I have the Host name, username,password and Port number
Help appreciated :)
You need several things:
A server that takes commands (create directory, list directory, write data to a file, read data from a file) over the network. This server should listen to port1 on localhost
You need to configure putty to forward port2 on your local computer to port1 on the server.
A local client which allows you to connect to port2 on your local computer. Putty will tunnel any data send to port2 to port1 on the remote server and vice versa.
Or you get WinSCP which uses the SSH protocol (just like Putty) and maybe already does what you want.
There's a pure Java implementation of SSH/SCP available: http://www.cleondris.ch/opensource/ssh2/
You can use its SCPClient or SFTPv3Client classes to work on the remote file system.
Documentation is available at http://www.cleondris.ch/opensource/ssh2/javadoc.
If you want to do it from Java, you can use Apache Commons VFS. It provides a common approach to dealing with files on all of the supported file systems. SFTP is one of the supported types which is most likely what you would need if you have been connecting with PuTTY.
You need SSH client. There are various pure java SSH clients. Google "java ssh client" and try any one of them. I used Jsch http://www.jcraft.com/jsch/ and it worked fine for me.