【问题标题】:how to download file from server getting error file not found?如何从服务器下载文件找不到错误文件?
【发布时间】:2014-07-30 11:55:22
【问题描述】:

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

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");

【问题讨论】:

  • 确保您的文件不是只读的。已在您的清单中提供了 WRITE_EXTERNAL_STORAGE 权限?
  • 已写入清单文件..请从服务器下载文件
  • 确保你的文件名是正确的,并且上传到服务器上。
  • 如果你点击喜欢,它就会上传到服务器上,因为有文件。但是文件名是什么?我什么都提

标签: android


【解决方案1】:

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

  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); 
    }
     }
    

别忘了在manifest.xml中使用互联网和存储权限

【讨论】:

    【解决方案2】:

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

    See this answer

    【讨论】:

    • 你必须重写它才能使用 AsyncTask
    猜你喜欢
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多