Android的:如何从服务器下载文件并将其保存在SD卡中的特定文件夹 [英] Android:How to download the File from the server and save it in specific folder in sdcard

查看:111
本文介绍了Android的:如何从服务器下载文件并将其保存在SD卡中的特定文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Andr​​oid应用程序一个要求。我需要下载和保存文件的SD卡中的特定文件夹编程。我已经开发的源$ C ​​$ C ..

和我的code是

 字符串DownloadUrl =htt​​p://myexample.com/android/;
     字符串文件名=myclock_db.db;

    DownloadDatabase(DownloadUrl,文件名);

    //并且该方法是

公共无效DownloadDatabase(字符串DownloadUrl,字符串文件名){
    尝试 {
        文件根= android.os.Environment.getExternalStorageDirectory();
        文件DIR =新的文件(root.getAbsolutePath()+/ myclock /数据库);
        如果(dir.exists()== FALSE){
             dir.mkdirs();
        }

        网址URL =新的URL(http://myexample.com/android/);
        档案文件=新的文件(目录,文件名);

        长的startTime = System.currentTimeMillis的();
        Log.d(下载管理器,下载网址:+网址);
        Log.d(下载管理器,下载文件的名称:+文件名);

        URLConnection的康涅狄格大学= url.openConnection();
        uconn.setReadTimeout(TIMEOUT_C​​ONNECTION);
        uconn.setConnectTimeout(TIMEOUT_SOCKET);

        InputStream的是= uconn.getInputStream();
        的BufferedInputStream bufferinstream =新的BufferedInputStream(是);

        ByteArrayBuffer BAF =新ByteArrayBuffer(5000);
        INT电流= 0;
        而((电流= bufferinstream.read())!=  -  1){
            baf.append((字节)电流);
        }

        FileOutputStream中FOS =新的FileOutputStream(文件);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        Log.d(下载管理器,下载现成的+((System.currentTimeMillis的() -  startTime时)/ 1000)+SEC);
        INT dotindex = fileName.lastIndexOf('。');
        如果(dotindex> = 0){
            文件名= fileName.substring(0,dotindex);

    }
    赶上(IOException异常E){
        Log.d(下载管理,错误:+ E);
    }

}
 

现在的问题是与文件名myclock_db.db只有空文件在保存路径。但我需要下载和保存文件的内容,在特定的文件夹。试了几种方法来获取文件的下载,但我不能。谁能帮我解决了问题...

在此先感谢...

解决方案

您的下载网址是不是链接到的任何文件。这是一个目录。确保它的一个文件,并存在。另外,请检查您的logcat窗口错误日志。还有一个建议,它始终是更好地在catch块,而不是原木做的printStackTrace()。其提供了错误的更详细的图。

更改此行:

 网​​址URL =新的URL(http://myexample.com/android/);
 

 网​​址URL =新的URL(http://myexample.com/android/yourfilename.txt); //某些文件网址
 

接下来,在catch块,加入这一行:

  e.printStackTrace();
 

另外,在目录路径,它应该是这样的:

 文件DIR =新的文件(root.getAbsolutePath()+到/ mnt / SD卡/ myclock /数据库);
 

而不是

 文件DIR =新的文件(root.getAbsolutePath()+/ myclock /数据库);
 

接下来,确保您已获得许可,以写入外部存储在Android清单。

I have one requirement in my Android application. I need to download and save file in specific folder of sdcard Programmatically. I have developed source code..

and my code is

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. Can anyone help me to solve the issue...

Thanks in advance...

解决方案

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.

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:

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的:如何从服务器下载文件并将其保存在SD卡中的特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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