从URL下载文件时抛出FileNotFoundException

时间:2014-02-23 00:22:50

标签: java android xml sqlite

所以我想从永久URL下载文件,然后将其保存在本地,以便以后可以访问它以写入SQLite数据库。我有一个AsynchTask类来处理这个功能,但我一直遇到FileNotFoundException错误(参见Logcat)。

下载CSV文件类

private class CSVFileDownloader extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
        try {   
            DefaultHttpClient httpClient = new DefaultHttpClient();
            httpClient.getCredentialsProvider().setCredentials(
                new AuthScope(null, -1),
                new UsernamePasswordCredentials("username", "password"));

            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet("http://www.cardigan.cc/app/locations.csv");  

                //GET FILE FROM URL AND WRITE TO LOCATIONS.CSV LOCALLY
                HttpResponse response = httpClient.execute(httpGet);
                HttpEntity entity = response.getEntity();
                if(entity != null){
                    BufferedInputStream bis = new BufferedInputStream(entity.getContent());
                    filename = "locations.csv";
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filename)));
                    int inByte;
                    while((inByte = bis.read()) != -1) bos.write(inByte);
                    bis.close();
                    bos.close();
                }

                else if(entity == null) {
                    System.out.println("NO FILE DOWNLOADED FROM SERVER");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

            return null ;               
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            //LOAD CSV FILE
            try {
                file = new FileReader(filename); //CREATE READ FOR LOCATIONS.CSV FILE A.K.A filename
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }

        }
    }

结果 LogCat 打印输出:

02-23 00:12:16.550: W/System.err(28119): java.lang.NullPointerException: lock == null
02-23 00:12:16.610: W/System.err(28119):    at java.io.Reader.<init>(Reader.java:64)
02-23 00:12:16.610: W/System.err(28119):    at java.io.BufferedReader.<init>(BufferedReader.java:92)
02-23 00:12:16.610: W/System.err(28119):    at java.io.BufferedReader.<init>(BufferedReader.java:80)
02-23 00:12:16.610: W/System.err(28119):    at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper.onCreate(MySQLiteHelper.java:90)
02-23 00:12:16.610: W/System.err(28119):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-23 00:12:16.610: W/System.err(28119):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
02-23 00:12:16.610: W/System.err(28119):    at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper.getAllMapMarkers(MySQLiteHelper.java:199)
02-23 00:12:16.610: W/System.err(28119):    at uk.ac.aber.dwd.CeredigionTourism.MapActivity.onCreate(MapActivity.java:40)
02-23 00:12:16.610: W/System.err(28119):    at android.app.Activity.performCreate(Activity.java:5240)
02-23 00:12:16.610: W/System.err(28119):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-23 00:12:16.610: W/System.err(28119):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-23 00:12:16.610: W/System.err(28119):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-23 00:12:16.610: W/System.err(28119):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-23 00:12:16.610: W/System.err(28119):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-23 00:12:16.610: W/System.err(28119):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 00:12:16.610: W/System.err(28119):    at android.os.Looper.loop(Looper.java:137)
02-23 00:12:16.610: W/System.err(28119):    at android.app.ActivityThread.main(ActivityThread.java:5048)
02-23 00:12:16.610: W/System.err(28119):    at java.lang.reflect.Method.invokeNative(Native Method)
02-23 00:12:16.610: W/System.err(28119):    at java.lang.reflect.Method.invoke(Method.java:511)
02-23 00:12:16.610: W/System.err(28119):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
02-23 00:12:16.610: W/System.err(28119):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:675)
02-23 00:12:16.610: W/System.err(28119):    at dalvik.system.NativeStart.main(Native Method)
02-23 00:12:17.180: W/System.err(28119): java.io.FileNotFoundException: /res/locations.csv: open failed: EROFS (Read-only file system)
02-23 00:12:17.770: W/System.err(28119):    at libcore.io.IoBridge.open(IoBridge.java:416)
02-23 00:12:17.770: W/System.err(28119):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
02-23 00:12:17.770: W/System.err(28119):    at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
02-23 00:12:17.770: W/System.err(28119):    at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper$CSVFileDownloader.doInBackground(MySQLiteHelper.java:296)
02-23 00:12:17.770: W/System.err(28119):    at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper$CSVFileDownloader.doInBackground(MySQLiteHelper.java:1)
02-23 00:12:17.770: W/System.err(28119):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-23 00:12:17.770: W/System.err(28119):    at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
02-23 00:12:17.770: W/System.err(28119):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-23 00:12:17.770: W/System.err(28119):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-23 00:12:17.770: W/System.err(28119):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-23 00:12:17.770: W/System.err(28119):    at java.lang.Thread.run(Thread.java:856)
02-23 00:12:17.780: W/System.err(28119): Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system)
02-23 00:12:17.780: W/System.err(28119):    at libcore.io.Posix.open(Native Method)
02-23 00:12:17.780: W/System.err(28119):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
02-23 00:12:17.780: W/System.err(28119):    at libcore.io.IoBridge.open(IoBridge.java:400)
02-23 00:12:17.780: W/System.err(28119):    ... 10 more
02-23 00:12:18.730: W/System.err(28119): java.io.FileNotFoundException: /res/locations.csv: open failed: ENOENT (No such file or directory)
02-23 00:12:18.780: W/System.err(28119):    at libcore.io.IoBridge.open(IoBridge.java:416)
02-23 00:12:18.780: W/System.err(28119):    at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-23 00:12:18.780: W/System.err(28119):    at java.io.FileInputStream.<init>(FileInputStream.java:105)
02-23 00:12:18.780: W/System.err(28119):    at java.io.FileReader.<init>(FileReader.java:66)
02-23 00:12:18.780: W/System.err(28119):    at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper$CSVFileDownloader.onPostExecute(MySQLiteHelper.java:319)
02-23 00:12:18.780: W/System.err(28119):    at uk.ac.aber.dwd.util.CeredigionTourism.MySQLiteHelper$CSVFileDownloader.onPostExecute(MySQLiteHelper.java:1)
02-23 00:12:18.780: W/System.err(28119):    at android.os.AsyncTask.finish(AsyncTask.java:631)
02-23 00:12:18.780: W/System.err(28119):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-23 00:12:18.780: W/System.err(28119):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-23 00:12:18.780: W/System.err(28119):    at android.os.Handler.dispatchMessage(Handler.java:99) 
02-23 00:12:18.780: W/System.err(28119):    at android.os.Looper.loop(Looper.java:137)
02-23 00:12:18.780: W/System.err(28119):    at android.app.ActivityThread.main(ActivityThread.java:5048)
02-23 00:12:18.780: W/System.err(28119):    at java.lang.reflect.Method.invokeNative(Native Method)
02-23 00:12:18.780: W/System.err(28119):    at java.lang.reflect.Method.invoke(Method.java:511)
02-23 00:12:18.780: W/System.err(28119):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
02-23 00:12:18.780: W/System.err(28119):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:675)
02-23 00:12:18.780: W/System.err(28119):    at dalvik.system.NativeStart.main(Native Method)
02-23 00:12:18.780: W/System.err(28119): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
02-23 00:12:18.780: W/System.err(28119):    at libcore.io.Posix.open(Native Method)
02-23 00:12:18.780: W/System.err(28119):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
02-23 00:12:18.790: W/System.err(28119):    at libcore.io.IoBridge.open(IoBridge.java:400)
02-23 00:12:18.790: W/System.err(28119):    ... 16 more

有人能指出我出错的方向吗?不确定文件是否正在下载,并且肯定不知道为什么它不能作为资源找到?!

1 个答案:

答案 0 :(得分:0)

java.io.FileNotFoundException:/res/locations.csv

它试图在您应用的资源文件夹中找到文件locations.csv。如果要将文件存储在设备上,则需要指定SD卡的路径。

FileOutputStream outputStream;
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);


// Now you can wrap this outputstream with BufferedOutputStream 
//and copy contents of the file that you are getting from the website
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
int inByte;
while((inByte = bis.read()) != -1) bos.write(inByte);
bis.close();
bos.close();