通过FTP上传文件之前检查文件是否存在

时间:2018-11-07 14:21:30

标签: android ftp

实际上,我有一个类,用于将一些XML文件上载到FTPServer,并且在发送文件后,它由另一个软件进行处理,并且在将XML插入数据库后将删除该文件。

问题是如果我还没有删除最后一个文件或者是否有问题导致服务器上的软件无法删除该文件,则我将另一个文件发送到FTP。

我该如何处理这种情况? 如果该文件还存在,我将添加一个自定义AlertDialog,然后仅关闭连接而不上传该文件。

这是我的FTPUploader.class,我对我该怎么做很感兴趣。

public class FTPUploader extends AsyncTask<Void,Void,String> {

    private String siteFTP;
    private String usrFTP;
    private String pswFTP;
    private String path;
    private String namePath;
    private String namefile;
    private  InventarioActivity inventarioActivity;

    FTPUploader(String siteFTP,String usrFTP, String pswFTP, String path,String namePath,String namefile, InventarioActivity inventarioActivity){
        super();
        this.siteFTP = siteFTP;
        this.usrFTP = usrFTP;
        this.pswFTP = pswFTP;
        this.path = path;
        this.namePath = namePath;
        this.namefile = namefile;
        this.inventarioActivity = inventarioActivity;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        inventarioActivity.customAlertLoading();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        InventarioActivity.dialogLoading.dismiss();
        switch (s) {
            case "OK":
                inventarioActivity.alertDeleteFile();
                break;
            case "EC":
                inventarioActivity.alertError("EC");
                break;
            case "ET":
                inventarioActivity.alertError("ET");
                break;
        }
    }

    @Override
    protected String doInBackground(Void... voids) {
        FTPClient ftpClient = new FTPClient();

        try
        {

            ftpClient.connect(siteFTP);

            if (ftpClient.login(usrFTP, pswFTP))
            {
              ftpClient.enterLocalPassiveMode();



                ftpClient.cwd(path);

                @SuppressLint("SdCardPath") String data = namePath;

                FileInputStream in = new FileInputStream(new File(data));


                try {
                    ftpClient.storeFile(namefile, in);
                }catch (IOException e){
                    e.printStackTrace();
                    return "ET";
                }
                in.close();
                return "OK";


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

        }

        try {
            ftpClient.logout();
            ftpClient.disconnect();
        }catch (IOException e){
            e.printStackTrace();
        }

        return null;
    }
}

0 个答案:

没有答案