2012-12-14 54 views
1
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異常,但該文件是存在的,如果你遵循的網址就可以看到它。Android的下載文件得到一個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

很可能是目標目錄有問題,請檢查文件系統上是否存在'/ sdcard/download'路徑 – hoaz

+0

你確定它是網址嗎? UrlConnection似乎不會引發FileNotFound異常,也許這是您訪問sdcard上的文件的方式?你能發佈發生異常的行嗎? – SirRichie

+0

請發佈您的所有logcat錯誤。 – Sam

回答

8

評論在您的代碼下面的一塊線路。 urlConnection.setDoOutput(true);

+0

請說明評論的原因。 –

+0

感謝它很好! – Bhupinder

+0

感謝它對我的作品。 –

相關問題