如何从服务器下载文件并将其保存在Android的SD卡中的特定文件夹中?

时间:2020-11-27 09:00:46

I have one requirement in my Android application. I need to download and save file in specific folder of SD card programmatically. I have developed source code, which is

我的Android应用程序中有一个要求。我需要以编程方式下载并保存SD卡的特定文件夹中的文件。我开发了源代码,这是

String DownloadUrl = "http://myexample.com/android/";
     String fileName = "myclock_db.db";

    DownloadDatabase(DownloadUrl,fileName);

    // and the method is

public void DownloadDatabase(String DownloadUrl, String fileName) {
    try {
        File root = android.os.Environment.getExternalStorageDirectory();
        File dir = new File(root.getAbsolutePath() + "/myclock/databases");
        if(dir.exists() == false){
             dir.mkdirs();  
        }

        URL url = new URL("http://myexample.com/android/");
        File file = new File(dir,fileName);

        long startTime = System.currentTimeMillis();
        Log.d("DownloadManager" , "download url:" +url);
        Log.d("DownloadManager" , "download file name:" + fileName);

        URLConnection uconn = url.openConnection();
        uconn.setReadTimeout(TIMEOUT_CONNECTION);
        uconn.setConnectTimeout(TIMEOUT_SOCKET);

        InputStream is = uconn.getInputStream();
        BufferedInputStream bufferinstream = new BufferedInputStream(is);

        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while((current = bufferinstream.read()) != -1){
            baf.append((byte) current);
        }

        FileOutputStream fos = new FileOutputStream( file);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        Log.d("DownloadManager" , "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + "sec");
        int dotindex = fileName.lastIndexOf('.');
        if(dotindex>=0){
            fileName = fileName.substring(0,dotindex);

    }
    catch(IOException e) {
        Log.d("DownloadManager" , "Error:" + e);
    }

}

Now the issue is only empty file with filename myclock_db.db is saving in the path. but I need to download and save content of file in the specific folder. Tried several ways to get the file download, but I can't.

现在问题只是文件myclock_db.db保存在路径中的空文件。但我需要下载并保存特定文件夹中的文件内容。尝试了几种获取文件下载的方法,但我不能。

1 个解决方案

#1


15  

Your download URL is not a link to any file. It's a directory. Make sure its a file and exists. Also check your logcat window for error logs. One more suggestion, its always better to do a printStackTrace() in catch blocks instead of Logs. Its gives a more detailed view of the error.

您的下载URL不是指向任何文件的链接。这是一个目录。确保它是一个文件并存在。还要检查logcat窗口中的错误日志。还有一个建议,总是更好地在catch块而不是Logs中执行printStackTrace()。它提供了更详细的错误视图。

Change this line:

改变这一行:

    URL url = new URL("http://myexample.com/android/");

to:

    URL url = new URL("http://myexample.com/android/yourfilename.txt"); //some file url

Next, in catch block, add this line:

接下来,在catch块中,添加以下行:

e.printStackTrace();

Also in the directory path, it should be something like this:

同样在目录路径中,它应该是这样的:

File dir = new File(root.getAbsolutePath() + "/mnt/sdcard/myclock/databases");

instead of

File dir = new File(root.getAbsolutePath() + "/myclock/databases");

Next, make sure you have acquired permission for writing to external storage in Android manifest.

接下来,确保您已获得在Android清单中写入外部存储的权限。

#1


15  

Your download URL is not a link to any file. It's a directory. Make sure its a file and exists. Also check your logcat window for error logs. One more suggestion, its always better to do a printStackTrace() in catch blocks instead of Logs. Its gives a more detailed view of the error.

您的下载URL不是指向任何文件的链接。这是一个目录。确保它是一个文件并存在。还要检查logcat窗口中的错误日志。还有一个建议,总是更好地在catch块而不是Logs中执行printStackTrace()。它提供了更详细的错误视图。

Change this line:

改变这一行:

    URL url = new URL("http://myexample.com/android/");

to:

    URL url = new URL("http://myexample.com/android/yourfilename.txt"); //some file url

Next, in catch block, add this line:

接下来,在catch块中,添加以下行:

e.printStackTrace();

Also in the directory path, it should be something like this:

同样在目录路径中,它应该是这样的:

File dir = new File(root.getAbsolutePath() + "/mnt/sdcard/myclock/databases");

instead of

File dir = new File(root.getAbsolutePath() + "/myclock/databases");

Next, make sure you have acquired permission for writing to external storage in Android manifest.

接下来,确保您已获得在Android清单中写入外部存储的权限。