2

此代码在从 apache 服务器下载时有效,没问题。但是在测试使用安装在win7上的iis 7.5 currenlty时。它似乎不适用于 getInputStream(); 尽管在调试并将其粘贴到浏览器中时会显示文件的完整地址,但会开始下载 apk

看了本站和网上都加了 MIME TYPE .apk application/vnd.android.package-archive

try {

                        URL url = new URL(prefs.getString("server_address", null) + "/updates/filename.apk");
                        HttpURLConnection c = (HttpURLConnection) url.openConnection();
                        c.setRequestMethod("GET");
                        c.setDoOutput(true);
                        c.connect();

                        String PATH = Environment.getExternalStorageDirectory() + "/download/";
                        File file = new File(PATH);
                        file.mkdirs();
                        File outputFile = new File(file, "filename.apk");
                        FileOutputStream fos = new FileOutputStream(outputFile);

                        InputStream is = c.getInputStream();

                        byte[] buffer = new byte[1024];
                        int len1 = 0;
                        while ((len1 = is.read(buffer)) != -1) {
                            fos.write(buffer, 0, len1);
                        }
                        fos.close();
                        is.close();



                        } catch (IOException e) {
                            Toast.makeText(context, "error!", Toast.LENGTH_LONG).show();
                        }
4

1 回答 1

2

只需摆脱这段代码:

      c.setRequestMethod("GET");
      c.setDoOutput(true);
      c.connect();

首先是没有必要的,其次, c.setDoOutput(true);将请求方法更改为 POST ...这就是为什么您不允许使用 405 方法...

于 2012-10-11T11:22:08.390 回答