如何从服务器下载文件获取错误文件?

时间:2014-07-30 11:55:23

标签: android

你可以告诉我如何从服务器下载文件吗?我收到错误填充未找到错误?

07-30 17:10:28.849: W/System.err(14900): java.io.FileNotFoundException: /testnaveen: open failed: EROFS (Read-only file system)
07-30 17:10:28.849: W/System.err(14900):    at libcore.io.IoBridge.open(IoBridge.java:409)
07-30 17:10:28.849: W/System.err(14900):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
07-30 17:10:28.849: W/System.err(14900):    at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
07-30 17:10:28.849: W/System.err(14900):    at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
07-30 17:10:28.849: W/System.err(14900):    at com.mobilecem.atms.GlobalFunction.downloadFileFromServer(GlobalFunction.java:147)

我这样做

public static void downloadFileFromServer(String filename, String urlString) throws MalformedURLException, IOException
    {
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try
        {
            URL url = new URL(urlString);

            in = new BufferedInputStream(url.openStream());
            fout = new FileOutputStream(filename);

            byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1)
            {
                fout.write(data, 0, count);
                System.out.println(count);
            }
        }
        finally
        {
            if (in != null)
                    in.close();
            if (fout != null)
                    fout.close();
        }
        System.out.println("Done");
    }

我称之为双向,但同样的错误原因是什么?

GlobalFunction.downloadFileFromServer("test", "http://www.example.com/inputParameters.js");
GlobalFunction.downloadFileFromServer( new File(Activity.this.getFilesDir(),"www").getAbsolutePath(), "url");

2 个答案:

答案 0 :(得分:0)

如果您使用的是HONEYCOMB或更高版本,则需要使用AsyncTask来调用与网络相关的操作。

See this answer

答案 1 :(得分:0)

使用下载管理器。 下载管理器的优势:

  1. 如果连接丢失,则自动重试下载。
  2. 它照顾工人线程,即背景线程。
  3. 它会在导航栏中显示已下载状态及进度状态

    package com.example.testing;
    import android.app.Activity;
    import android.app.DownloadManager;
    import android.content.Context;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    
    public class FileDownloading extends Activity
    {
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startDownload("http://www.example.com/inputParameters.js","inputParameters.js");
    }
    public void startDownload(String url ,String fileName)
    {
        Log.d("Download Url", url);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setTitle("Your File Name");
        request.setDescription(fileName+" is downloading..");
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
        long downloadID = downloadManager.enqueue(request); 
    }
     }
    
  4. 请勿忘记在manifest.xml

    中使用互联网和存储空间权限