移动文件从nanohttpd的临时目录到SD卡上传 [英] move file uploaded from nanohttpd's Temporary directory to SD card

查看:1234
本文介绍了移动文件从nanohttpd的临时目录到SD卡上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的计算机到Android WebServerApp上传文件LICENSE.TXT。
NanoHTTPD使用临时目录来保存上传的文件。临时位置由决定:

I am uploading a file "LICENSE.txt" from my PC to Android WebServerApp. NanoHTTPD uses a temporary directory to save the uploaded files. The temporary location is decided by :

    tmpdir = System.getProperty("java.io.tmpdir");

和文件被上传为: /data/data/com.manmohan.mynanoserver/cache/NanoHTTPD-1736025823 在我的情况

and file gets uploaded as : /data/data/com.manmohan.mynanoserver/cache/NanoHTTPD-1736025823 in my case.

在上传我想将文件移动到我的SD卡/存储/ extSdCard /上传。

After the upload I want to move the file to my SD card "/storage/extSdCard/Uploads".

下面是我做的:

        String tempFileName = entry.getValue().toString();
        File fileToMove = new File(tempFileName); // temp file path returned by NanoHTTPD

        String p = "/storage/extSdCard/Uploads";
        String newFile = p + "/LICENSE.txt";
        File nf = new File(newFile); // I want to move file here

        if (fileToMove.canWrite()) {
            boolean success = fileToMove.renameTo(nf);
            if (success == true) {
                // LOG to console
                Log.i("FILE_MOVED_TO", newFile);
            } else {
                Log.e("FILE_MOVE_ERROR", tempFileName);
            }
        } else {
            Log.e("PERMISSION_ERROR_TEMP_FILE", tempFileName);
        }

我无法访问 /数据/。 。 目录和里面的文件,出现错误时,尝试移动的文件。

I cannot access the /data/. . . directory and files in it, and get error when try to move file.

但这个临时路径如下:

    tmpdir = "/storage/extSdCard/temp-uploads-nanohttpd";

这有什么错的 java.io.tmpdir ?如果NanoHTTPD可以写入,那么为什么我不能移动的文件?

What's wrong with java.io.tmpdir ? If NanoHTTPD can write to it, then why I am not able to move the file ?

推荐答案

由于源和目标都在不同的文件系统一个简单的重命名是不可能的。该文档的 renameTo 方法状态:

Since the source and the destination are on different file systems a simple rename is not possible. The documentation for the renameTo method states:

许多该方法的各方面的行为是固有平台相关:重命名操作可能无法从一个文件系统文件移动到另一个,它可能不是原子,并且它可能不会成功,如果与一个文件目的地抽象路径名已经存在。返回值应始终进行检查,以确保重命名操作成功。

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

要解决这个问题,将文件复制到新的地方,从旧的删除。

To solve this, copy the file to the new place and delete it from the old.

这篇关于移动文件从nanohttpd的临时目录到SD卡上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆