在Android中使用SCP / SFTP或更安全的方式上传文件

时间:2014-08-04 12:19:31

标签: android ftp sftp scp

如何在Android中使用SCP协议上传文件。

我不知道。请给我示例代码或明确的指导。

我使用FTP制作了以下代码。它的工作正常。但客户说FTP是一种不太安全的协议。他们告诉我使用SCP而不是FTP。这些都是代码。请让我知道如何以安全的方式做到这一点(SCP或任何相关方法请解释)。

private String sendImageToServer(final String folderName, final File image) {
class FtpAsyncTask extends AsyncTask<Void, Void, String>{

    @Override
    protected String doInBackground(Void... params) {
        try{

            FTPClient connection = new FTPClient();
            connection.connect("192.244.110.86");
            boolean imageUploaded = false;

            if (connection.login("root","1pass6")) {

                connection.setFileType(FTP.BINARY_FILE_TYPE);
                connection.enterLocalPassiveMode();

                byte[] imageInBytes = new byte[(int) image.length()];
                try {

                    FileInputStream fileInputStream = new FileInputStream(
                            image);
                    fileInputStream.read(imageInBytes);

                    ByteArrayInputStream in = new ByteArrayInputStream(
                            imageInBytes);

                    boolean folderCreted = connection.makeDirectory(folderName);

                    if (folderCreted) {
                        System.out.println("folder created ");
                    } else {
                        System.out.println("folder not created ");

                    }

                    imageUploaded = connection.storeFile(folderName+"/"+image.getName()+".jpg", in);

                    System.out.println("Image sent "+imageUploaded);
                    in.close();
                    connection.logout();
                    connection.disconnect();
                    if (imageUploaded) {
                        System.out.println("imageUploaded success");
                        return "success";
                    } else {
                        System.out.println("imageUploaded fail");
                        return "fail";
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result = "fail";

                }

            } else {
                System.out.println("UNABLE TO LOG IN TO THE FTP");
            }


        }catch(Exception e){
            e.printStackTrace();
            return "fail";
        }
        return "fail";
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        System.out.println("onPostExecute "+result);
        if (result.equals(UPLOAD_SUCCESS)) {
            image.delete();

            System.out.println("UPLOAD_SUCCESS");
        } else if (result.equals(UPLOAD_FAIL)) {
            System.out.println("UPLOAD_FAIL");                  

        }
    }

}

FtpAsyncTask ftpAsyncTask = new FtpAsyncTask();
ftpAsyncTask.execute();
return result;
}

1 个答案:

答案 0 :(得分:0)

我在谷歌搜索过。请尝试以下代码。现在,我没有服务器可供我测试。

                Intent intent = new Intent();
            intent.setAction(Intent.ACTION_PICK);
            // FTP URL (Starts with ftp://, sftp://, ftps:// or scp:// followed by hostname and port).
            Uri ftpUri = Uri.parse("scp://yourserver.com");
            intent.setDataAndType(ftpUri, "vnd.android.cursor.dir/lysesoft.andftp.uri");
            // Upload
            intent.putExtra("command_type", "upload");
            // FTP credentials (optional)
            intent.putExtra("ftp_username", "yoursshlogin");
            intent.putExtra("ftp_password", "yoursshpassword");
            //intent.putExtra("ftp_keyfile", "/sdcard/rsakey.txt");
            //intent.putExtra("ftp_keypass", "optionalkeypassword");
            // FTP settings (optional)
            //intent.putExtra("ftp_encoding", "UTF-8");
            // Activity title
            intent.putExtra("progress_title", "Uploading files ...");
            intent.putExtra("local_file1", "/sdcard/subfolder1/file1.zip");
            // Optional initial remote folder (it must exist before upload)
            intent.putExtra("remote_folder", "/home/youruser/");
            startActivityForResult(intent, UPLOAD_FILES_REQUEST);

参考 - http://www.lysesoft.com/products/andftp/AndFTPClient1.0.zip

如果可能的话,你可以测试一下,让我知道它是否有效吗?