Android下载文件获取FileNotFoundException

时间:2012-12-14 22:12:08

标签: java android

    else if (v.getId() == R.id.update) {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);

        try {
            URL url = new URL("http://darkliteempire.gaming.multiplay.co.uk/testdownload.txt");

            //create the new connection

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            //set up some things on the connection

            urlConnection.setRequestMethod("GET");

            urlConnection.setDoOutput(true);

            //and connect!

            urlConnection.connect();

            //set the path where we want to save the file

            //in this case, going to save it on the root directory of the

            //sd card.

            File SDCardRoot = new File("/sdcard/"+"download/");

            //create a new file, specifying the path, and the filename

            //which we want to save the file as.

            File file = new File(SDCardRoot,"test.txt");

            //this will be used to write the downloaded data into the file we created

            FileOutputStream fileOutput = new FileOutputStream(file);

            //this will be used in reading the data from the internet

            InputStream inputStream = urlConnection.getInputStream();

            //this is the total size of the file

            int totalSize = urlConnection.getContentLength();

            //variable to store total downloaded bytes

            int downloadedSize = 0;

            //create a buffer...

            byte[] buffer = new byte[1024];

            int bufferLength = 0; //used to store a temporary size of the buffer

            //now, read through the input buffer and write the contents to the file

            while ( (bufferLength = inputStream.read(buffer)) > 0 ) 

            {

            //add the data in the buffer to the file in the file output stream (the file on the sd card

            fileOutput.write(buffer, 0, bufferLength);

            //add up the size so we know how much is downloaded

            downloadedSize += bufferLength;

            int progress=(int)(downloadedSize*100/totalSize);

            //this is where you would do something to report the prgress, like this maybe

            //updateProgress(downloadedSize, totalSize);

            }

            //close the output stream when done

            fileOutput.close();
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

此代码不断出现FileNotFoundException,但文件在那里,如果您按照网址进行操作,则可以看到它。

是否必须以特定方式托管?或者我得到它的方式有问题

编辑:这是logcat消息

12-14 22:29:19.890: W/System.err(24557): java.io.FileNotFoundException:     http://darkliteempire.gaming.multiplay.co.uk/testdownload.txt
12-14 22:29:19.890: W/System.err(24557):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
12-14 22:29:19.890: W/System.err(24557):    at com.MasterZangetsu.kentrocksoc.MainActivity.onClick(MainActivity.java:99)
12-14 22:29:19.890: W/System.err(24557):    at android.view.View.performClick(View.java:3591)
12-14 22:29:19.890: W/System.err(24557):    at android.view.View$PerformClick.run(View.java:14263)
12-14 22:29:19.895: W/System.err(24557):    at android.os.Handler.handleCallback(Handler.java:605)
12-14 22:29:19.895: W/System.err(24557):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-14 22:29:19.895: W/System.err(24557):    at android.os.Looper.loop(Looper.java:137)
12-14 22:29:19.895: W/System.err(24557):    at android.app.ActivityThread.main(ActivityThread.java:4507)
12-14 22:29:19.895: W/System.err(24557):    at java.lang.reflect.Method.invokeNative(Native Method)
12-14 22:29:19.895: W/System.err(24557):    at java.lang.reflect.Method.invoke(Method.java:511)
12-14 22:29:19.895: W/System.err(24557):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
12-14 22:29:19.895: W/System.err(24557):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
12-14 22:29:19.895: W/System.err(24557):    at dalvik.system.NativeStart.main(Native Method)
12-14 22:29:20.170: W/WifiStateTracker(2010): getNetworkInfo : NetworkInfo: type: WIFI[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: false

1 个答案:

答案 0 :(得分:10)

在您的代码中注释下面一行。 urlConnection.setDoOutput(真);