0

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!

L.Grillo
  • 960
  • 3
  • 12
  • 26

2 Answers2

3

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

The D Merged
  • 680
  • 9
  • 17
  • Yes if you use PHP server side you have to do the server part in PHP. A problem you will get is that anyone also can make a file through your PHP api if you do not program it securely. – The D Merged Jan 08 '14 at 22:15
2

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.

Peter
  • 1,769
  • 1
  • 14
  • 18