java - Android 下载文件获取 FileNotFoundException

标签 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,但文件就在那里,如果您按照 url 可以看到它。

是否必须以特定方式托管?还是我获取它的方式有问题

编辑:这是 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

最佳答案

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

关于java - Android 下载文件获取 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13886987/

相关文章:

java - Android Facebook 登录 - 由 null 引起的错误

Android "localhost"套接字在没有数据连接时失败?

android - Flutter 平台 channel 的 channel 名称应该是什么格式?

c# - 如何以编程方式设置 layout_weight?

java - 无法处理文件 - Android 工作室(应用程序 :mergeDebugResources)

java - SimpleDateFormat.parse - ParseException

java - 为什么启动 StreamingContext 失败并显示 “IllegalArgumentException: requirement failed: No output operations registered, so nothing to execute” ?

java - 问题!实体数据未使用数据访问对象 (dao) 和 JPA 获取?

android - 具有拉伸(stretch)宽高比的自定义 Android 相机预览

java - 从 ArrayList 中检索样本