我的问题是:想从网上下载pdf文件并保存到sdcard中,但现在问题是,我所保存的pdf文件好像不是正规格式的pdf文件,以至于手机上装了几个pdf阅读器都无法打开,电脑adobe软件也无法打开,但是手机用PoQoP-Document软件可以打开
     我判断是我的pdf文件生成的有问题,可能非正规pdf格式,但我得到数据流后该如何生成呢?下面是我的代码,还请高手看看,非常感谢!
                File dirFile=new File(path_info);
                if(!dirFile.exists()){
                        dirFile.mkdir();
                        Log.v("whether make", ""+dirFile.exists());
                }
                String pdf_name=urlPdf.substring(urlPdf.lastIndexOf("/")+1);
                
                File pdf_File=new File(dirFile, pdf_name);
                if(!pdf_File.exists()){
                        try {
                                pdf_File.createNewFile();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
                try {
                        FileOutputStream fos=new FileOutputStream(pdf_File);
                        try {
                                URL myUrl=new URL(urlPdf);
                                try {
                                        URLConnection conn=myUrl.openConnection();
                                        HttpURLConnection httpConn=(HttpURLConnection)conn;
                                        httpConn.connect();
                                        
                                        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                                                int contentLength = httpConn.getContentLength();
                                                InputStream is=httpConn.getInputStream();
                                                if (is == null) throw new RuntimeException("stream is null");                                                                                                                                                
                                                BufferedInputStream reader=null;
                                                BufferedOutputStream out=null;
                                                reader=new BufferedInputStream(is, 1024);
                                                out=new BufferedOutputStream(fos , 1024);
                                                byte[] bytes=new byte[512];
                                                int length=0;
                                                while((length=reader.read(bytes))!=-1){
                                                        out.write(bytes, 0, length);
                                                }
                                                fos.flush();
                                                is.close();
                                                fos.close();
                                                
                                                openPDF(path_info+"/"+pdf_name);
                                                progressD.dismiss();                                        }
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        } catch (MalformedURLException e) {
                                e.printStackTrace();
                        }
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                }
        

解决方案 »

  1.   

     if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
      int contentLength = httpConn.getContentLength();
      InputStream is=httpConn.getInputStream();
      if (is == null) throw new RuntimeException("stream is null");    
      byte[] bytes=new byte[1024];
      int length=0;
      ByteArrayOutputStream baos=new ByteArrayOutputStream();
       while((len=is.read(data))!=-1)
    baos.write(data, 0, len);
      fos.write(baos.toByteArray());

      fos.flush();
      is.close();
      fos.close();
        
      openPDF(path_info+"/"+pdf_name);
      progressD.dismiss();  }
      } catch (IOException e) {
      e.printStackTrace();
      }
      } catch (MalformedURLException e) {
      e.printStackTrace();
      }
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      }
    试试 这个!!!
      

  2.   

       对流整体读写看成不成
    if (is == null) throw new RuntimeException("stream is null");   int len = is.available();
       BufferedInputStream reader=null;
       BufferedOutputStream out=null;
       reader=new BufferedInputStream(is, len);
       out=new BufferedOutputStream(fos , len);
       byte[] bytes=new byte[len];
       int length=0;
       while((length=reader.read(bytes))!=-1){
       out.write(bytes, 0, length);
       }
       fos.flush();
      

  3.   

    你设断点跟一下你的代码。一般我都这样: byte[] buffer = new byte[1024];
    int lenght = 0;
    while ((lenght = inputStream.read(buffer)) > 0){
    fos.write(buffer, 0, lenght);
    }
      

  4.   

    inputStream 为 InputStream
    fos 为 FileOutputStream
      

  5.   

    去掉这句话试下  fos.flush();
    此方法不保证能将这些字节实际写入到物理设备(如磁盘驱动器)