输入流错误

时间:2012-09-06 13:55:33

标签: android

这是我面临的一个奇怪错误,在尝试下载文件时,我需要打开输入流,然后将数据读入输出流......输入流不起作用......没有错误虽然调试..但它似乎挂在那里和键盘上的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);
        }

1 个答案:

答案 0 :(得分:0)

您可以尝试HttpGet请求:

HttpGet httpGet = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
// Execute HTTP Get Request
HttpResponse response = httpclient.execute(httpGet);
content = response.getEntity().getContent();