0

这是我遇到的一个奇怪的错误,在尝试下载文件时,我需要打开输入流,然后将读取的数据写入输出流......输入流不起作用......调试时没有错误。 .但它似乎只是挂在那里,键盘上的 F6 似乎不起作用....必须终止调试会话...我做错了什么???

 try{
        URL url = new URL(pdfurl);
        URLConnection conexion = url.openConnection();
        conexion.connect();

        int lenghtOfFile = conexion.getContentLength();
        Log.d("k", "Lenght of file: " + lenghtOfFile);
        //InputStream s=file
        InputStream s=url.openStream();
        InputStream input = new BufferedInputStream(s);
        OutputStream output = new FileOutputStream(file);

        byte data[] = new byte[1024];

        long total = 0;

         while ((count = input.read(data)) != -1) {
          total += count;
          publishProgress((int)((total*100)/lenghtOfFile));
          output.write(data, 0, count);
         }

         output.flush();
         output.close();
         input.close();
        } 
         catch (Exception e) 
        {
         download_flag = true;
         String s=e.getMessage().toString();
         Log.d("k","exception occured"+s);
        }
4

1 回答 1

0

你可以尝试一个 HttpGet 请求:

HttpGet httpGet = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
// Execute HTTP Get Request
HttpResponse response = httpclient.execute(httpGet);
content = response.getEntity().getContent();
于 2012-09-06T14:06:47.160 回答