1

我正在尝试通过 FTP 上传文件,但在上传之前必须将其重命名为 2 个 editText 的输入。为此,我使用以下代码:

public FTPClient client = new FTPClient();

    public void upload_klik (View view) {
        EditText week_text = (EditText) findViewById(R.id.week_edit);
        EditText pagina_text = (EditText) findViewById(R.id.pagina_edit);
        String w_val = week_text.getText().toString();
        String p_val = pagina_text.getText().toString();
        upload_task up = new upload_task();
        up.execute(w_val, p_val);
    }

    protected class upload_task extends AsyncTask<String, Object, String> {

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

            String w = params[0];
            String p = params[1];

            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();
            String ret = "Done!";
            if(!bundle.isEmpty()) {
                String afdeling_url = bundle.getString("afdeling_url", "DKW/");
                String afdeling_preFix = bundle.getString("afdeling_preFix", "dkw");
                String locatie_url = bundle.getString("locatie_url", "/delf_wend/");

                String new_fileName = afdeling_preFix +"_" + "w" + w + "_" + "p" + p + ".jpg";

                System.out.println(new_fileName);

                File f = new File(foto_path);
                File sdcard = Environment.getExternalStorageDirectory();
                File to = new File(sdcard, new_fileName);
                f.renameTo(to);

                if(f != null) {

                    try{
                        client.setPassive(true);
                        client.setAutoNoopTimeout(30000);
                        client.connect(FTP_HOST, 21);
                        client.login(FTP_USER, FTP_PASS);
                        client.setType(FTPClient.TYPE_BINARY);
                        System.out.println(locatie_url + afdeling_url);
                        client.changeDirectory(locatie_url + afdeling_url);
                        client.upload(to);

                        restart();

                    }
                    catch (Exception e){
                        e.printStackTrace();
                        try {
                            client.disconnect(true);
                        }
                        catch (Exception e2) {
                            e2.printStackTrace();
                        }
                    }
                }

            }
            return ret;
        }
    }

但是当我尝试上传它时,Logcat 给了我这个:

09-09 16:33:17.794  23674-25966/nl.knapper_development.jrw_uploader I/System.out﹕ slag_w11_p222.jpg
09-09 16:33:17.994  23674-25966/nl.knapper_development.jrw_uploader I/System.out﹕ /delf_wend/SLAG/
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ java.io.FileNotFoundException: /slag_w11_p222.jpg
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2577)
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ at it.sauronsoftware.ftp4j.FTPClient.upload(FTPClient.java:2457)
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ at nl.knapper_development.jrw_uploader.upload$upload_task.doInBackground(upload.java:154)
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ at nl.knapper_development.jrw_uploader.upload$upload_task.doInBackground(upload.java:119)
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-09 16:33:18.024  23674-25966/nl.knapper_development.jrw_uploader W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

我将问题缩小到函数f.renameTo(new_fileName),它说调用这个方法的结果被忽略了。但是为什么会被忽略呢?有没有办法解决这个问题?

先感谢您 :)

4

2 回答 2

1

该错误非常简单,它已全部记录在案:

https://developer.android.com/reference/java/io/File.html#renameTo(java.io.File)

许多失败是可能的。一些更可能的失败包括:

  • 同时包含源路径和目标路径的目录需要写权限。
  • 两条路径的所有父级都需要搜索权限。
  • 两条路径都在同一个挂载点上。在 Android 上,应用程序在尝试在内部存储和 SD 卡之间进行复制时最有可能遇到此限制。

https://developer.android.com/reference/java/io/FileNotFoundException.html

当找不到程序指定的文件时抛出

我敢打赌,如果您检查文件是否与代码一起存在:

if(f.exists()){

你会发现它foto_path不是一个现有的文件,或者你应该检查它foto_path也在里面Environment.getExternalStorageDirectory()

如果它不在同一个挂载点,则必须将文件复制过来(使用新名称),而不仅仅是重命名。

于 2015-09-09T14:57:40.450 回答
0

RenameTo 重命名实际文件,它不会更改 File 对象。如果您调用 .exists() 您会发现新文件存在而旧文件不存在。

这是因为 File 类表示抽象路径而不是文件系统上的实际文件。这个想法是 File.renameTo 为给定路径的文件系统条目提供一个新名称;它不会改变路径本身。

于 2015-09-09T15:16:31.037 回答