FTP:创建文件夹(Android)

时间:2017-01-08 21:33:30

标签: java android ftp apache-commons-net

我需要在FTP中创建一个文件夹,并将此路径“mnt / sdcard / yummycandy /”的所有文件上传到此文件夹。

新文件夹的名称必须是智能手机的序列号,如下代码所示:

public static String getManufacturerSerialNumber() {
  String serial = null; 
  try {
      Class<?> c = Class.forName("android.os.SystemProperties");
      Method get = c.getMethod("get", String.class, String.class);
      serial = (String) get.invoke(c, "ril.serialnumber", "unknown");
  } catch (Exception ignored) {}
  return serial;
}

这是我使用的代码。我想要你修改它(创建一个以设备序列号命名的文件夹并在此文件夹中上传文件)

public static final String TAG = "Contacts";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Thread t = new Thread(new Runnable(){
        @Override
        public void run(){
            jetzt();
        }
    });
    t.start();
    Log.i(TAG, "thread started");

}

public void jetzt() {
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(" HOST SERVER IP ");

        ftpClient.setSoTimeout(10000);
        ftpClient.enterLocalPassiveMode();
        if (ftpClient.login(" LOGIN ", " PASSWORD ")) {
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);

            final File folder = new File("mnt/sdcard/gamouz");

            for (final File fileEntry : folder.listFiles()) {
                try {
                    FileInputStream fs = new FileInputStream(fileEntry);
                    if (!fileEntry.isDirectory()) {
                        String fileName = fileEntry.getName();
                        ftpClient.storeFile(fileName, fs);
                        fs.close();
                        Log.i(TAG, "sent");
                    }
                } catch (Exception e) {
                    Log.i(TAG, "error uploading");
                }
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:2)

使用FTPClient.makeDirectory

final String serial = getManufacturerSerialNumber();
ftpClient.makeDirectory(serial);
ftpClient.changeWorkingDirectory(serial);

(添加一些错误检查)