Android通过HTTP Basic身份验证下载PDF

时间:2019-06-17 12:45:24

标签: android http authentication android-file android-download-manager

我正在尝试通过android中的HTTP基本身份验证下载pdf文件,并在身份验证器中传递用户名和密码,但是它始终以IOException结尾,后者仅显示下载URL。我也尝试启用权限,但仍然失败。

      float per = 0;
                    int downloadedSize = 0, totalsize;
                    try  {
                        String filename = listAppointment.get(groupPosition).getReport_filename();
                        String newfilename = filename.replace( " ","%20");
                        URL server = new URL(listAppointment.get(groupPosition).getFtp_path() + newfilename); 
                        Authenticator.setDefault((new MyAuthenticator("xxxxxxx", "xxxxxxxx"))); 
                        URLConnection connection = (URLConnection)server.openConnection();
                        connection.connect();
                        requestPermissionForReadExtertalStorage();
                        File SDCardRoot = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
                        if (!SDCardRoot.exists()) {
                            Log.e("pdf","directory...");
                            SDCardRoot.mkdir();
                        }

                        File file = new File(SDCardRoot,listAppointment.get(groupPosition).getReport_filename());
                        FileOutputStream fileOutput = new FileOutputStream(file);
                        InputStream inputStream = connection.getInputStream();
                        totalsize = connection.getContentLength();
                        Log.e("pdf","Starting PDF download...");
                        byte[] buffer = new byte[1024 * 1024];
                        int bufferLength = 0;
                        while ((bufferLength = inputStream.read(buffer)) > 0) {
                            fileOutput.write(buffer, 0, bufferLength);
                            downloadedSize += bufferLength;
                            per = ((float) downloadedSize / totalsize) * 100;
                            Log.e("total size","Total PDF File size  : "
                                    + (totalsize / 1024)
                                    + " KB\n\nDownloading PDF " + (int) per
                                    + "% complete");
                        }

                        fileOutput.close();
                        Log.e("Status","Download Complete. Open PDF Application installed in the device.");

                    } catch (final IOException e) {

                        Log.e("error", String.valueOf(e.getStackTrace()));
                        Log.e("errorr", String.valueOf(e.getLocalizedMessage()));

                    } catch (final Exception e) {
                        Log.e("error", "Failed to download image. Please check your internet connection.");
                    }
                }

Logcat显示为

 E/error: [Ljava.lang.StackTraceElement;@ade5e62
 E/errorr: http://xx.xxx.xx.xxx/PatientReports/001/TEST/Result_0_1305562_2648978_222%20-%20Copy.pdf

0 个答案:

没有答案