java result of a is ignored,Result of .renameTo() is ignored

问题

I am trying to upload a file via FTP, but before it is uploaded it has to be renamed to the input of 2 editText's. To do this i use the following code:

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 {

@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;

}

}

But when i try to uplaod it Logcat gives me this:

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)

i narrowed the problem to to thwe function f.renameTo(new_fileName), it says the result of calling this method is ignored. But why is it ignored? And is there a way around this?

Thank you in advance :)

回答1:

the error is pretty straight forward is it's all documented:

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

Many failures are possible. Some of the more likely failures include:

Write permission is required on the directories containing both the source and destination paths.

Search permission is required for all parents of both paths.

Both paths be on the same mount point. On Android, applications are most likely to hit this restriction when attempting to copy between internal storage and an SD card.

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

Thrown when a file specified by a program cannot be found

I'm betting that if you check if the file exist with the code:

if(f.exists()){

you'll find out that foto_path is not an existing file, or maybe you should check that foto_path is also inside Environment.getExternalStorageDirectory().

If it's not in the same mount point you have to copy the file over (with the new name) instead of just renaming.

回答2:

RenameTo renames the actual file, it doesn't change the File object. If you called .exists() you'd find the new file exists and the old one doesn't.

This is because the File class represents abstract paths rather than actual files on a file system. The idea is that File.renameTo gives a new name to a file system entry at the given path; it does not change the path itself.

来源:https://stackoverflow.com/questions/32482737/result-of-renameto-is-ignored

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值