在Android中使用HttpPost下载Pdf显示不正确的PDF格式

时间:2015-03-10 20:24:01

标签: android pdf download

我试图使用HttpPost下载文件。但使用下面的代码下载后,它显示无效的pdf格式。请帮我纠正这个问题?

private void downLoadFile(String Filename) throws ClientProtocolException, IOException{

         HttpClient client = new DefaultHttpClient();
         HttpPost post = new HttpPost(URL);


         List<NameValuePair> pairs = new ArrayList<NameValuePair>();

         Log.i(TAG, "Filename: " + Filename);

         pairs.add(new BasicNameValuePair("Filename:", Filename));
         post.setEntity(new UrlEncodedFormEntity(pairs));
         HttpResponse response = client.execute(post);
         InputStream is = response.getEntity().getContent();

         FileOutputStream fos = new FileOutputStream(new File( Environment.getExternalStorageDirectory().getAbsolutePath() +"/omniware/retail/"+Filename));
             int read = 0;
             byte[] buffer = new byte[1048576];
             while((read = is.read(buffer)) > 0){
                    fos.write(buffer, 0, read);
             }
             fos.flush();
             fos.close();
             is.close();

             showPdf(Filename);
             Log.i(TAG, "Response Code: " + Integer.toString(response.getStatusLine().getStatusCode()));


     }

     public void showPdf(String Filename){
            File file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/omniware/retail/"+Filename);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(file2);
            intent.setDataAndType(uri, "application/pdf");
            startActivity(intent);
        }

0 个答案:

没有答案