Skip to Main Content

Download URL content using jsp

Reddy007Feb 19 2018 — edited Feb 19 2018

Hi Every one,

i need to call one web service which downloads a PDF document, i am trying with jsp but final output is corrupted meaning unreadable format with question marks like ..

sb String - %PDF-1.4 %���� 2 0 obj <> stream ���� JFIF   ����C                     $        #," '.0,)7(  44418=9'.<2432��C        2! !22222222222222222222222222222222222222222222222222��   � �         ��                  ���             }       !1A  Qa "q 2��� #B�� R��$3br�      %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������                     ���              w      !1  AQ aq "2�  B���� #3R� br�  $4�%�    &'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz�������������������������������������������������������������������������� ���       ?��(� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (� � (��&%  (  Ҁ`:P � ��� �� �@ (  � �� ��� �@ �    Nh ��i� � �� � q  qh�.  �#� �, q@�q@�  q@ �P  �* �Q@ � � � P  m `�.!

Below is my code which i am trying

<%@ page import = "java.io.BufferedReader" %>

<%@ page import = "java.io.IOException;" %>

<%@ page import = "java.io.InputStreamReader" %>

<%@ page import = "java.io.OutputStream" %>

<%@ page import = "java.io.*" %>

<%@ page import = "java.net.HttpURLConnection" %>

<%@ page import = "java.net.MalformedURLException" %>

<%@ page import = "java.net.URL" %>

<%@ page import = "com.sun.org.apache.xerces.internal.impl.dv.util.Base64" %>

<html>

 

   <body>

      <center>

         <table width = "100%" border = "1" align = "center">

           

            <%

               String USER_AGENT = "Mozilla/5.0";

               String targeturl = "http://XXX";\\Commented out my url

              

                String uniqueId = "12345678_236683804-121";

                String doc_type="litpack";

                String rtv_app="Portal";

                URL myurl = new URL(targeturl + "?" + "unique_id=" + uniqueId + "&doc_type=" + doc_type + "&rtv_app=" + rtv_app);

                HttpURLConnection con = (HttpURLConnection)myurl.openConnection();

                con.setDoOutput(true);

                con.setDoInput(true);

                con.setRequestProperty("Accept", "application/pdf");

                con.setRequestProperty("Method", "GET");

               

                String username = "admin";

                String password = "password";

                String userpassword = username + ":" + password;

                String encodedAuthorization = Base64.encode(userpassword.getBytes());

                con.setRequestProperty("Authorization", "Basic "+encodedAuthorization);

               

                OutputStream os = con.getOutputStream();

               

                

                StringBuilder sb = new StringBuilder(); 

               

                int HttpResult =con.getResponseCode();

                if(HttpResult ==HttpURLConnection.HTTP_OK)

                {

                BufferedReader br = new BufferedReader(new   InputStreamReader(con.getInputStream(),"utf-8"));

                String line = null;

                while ((line = br.readLine()) != null) { 

                sb.append(line + "\n"); 

                }

                 br.close();

                 out.println("sb String - "+sb.toString());

                }else{

                    out.println(con.getResponseCode());

                    out.println(con.getResponseMessage()); 

                }

                String text = sb.toString();

            %>

         </table>

      </center>

  

   </body>

</html>

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Mar 19 2018
Added on Feb 19 2018
1 comment
398 views