Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

mathguy

You jumped to the code too soon. First describe in detail the problem you are having - the real-life, business issue you are trying to solve.
What "dates" are you referring to? (Do you mean "data", not "dates"?)
What do you mean by "remove items"? "Remove" is not a technical term in SQL, so you must explain what you mean. Same with "item".
And don't say "read the code, you will understand". For one thing, the code DOESN'T do what it is supposed to do (otherwise you wouldn't ask a question here in the first place). So it is not possible to guess what it was supposed to do, when it doesn't even do that. Second, the code may try the wrong approach anyway. And third, it is your job to tell us exactly what the problem is - not to ask us to make the effort to guess the problem from the code.

User_AC3QE
<a href="https://community.oracle.com/tech/developers/discussion/comment/16784775#Comment_16784775" alt="undefined type="undefined">undefined</a> (0 Bytes)Thanks, I adjusted the question text
Frank Kulash

Hi, @user-ac3qe
Whenever you have a question, please post a little sample data in a usable form (such as CREATE TABLE and INSERT statements, relevant columns only), so the people who want to help you car re-create the problem and test their ideas. Also post the exact results you want from the given data, and an explanation (in general terms) of you get those results from that data.
Simplify the problem, if possible. For example, if your query involves five tables, but you already know how to join them, and the problem only concerns excluding some rows from the result set, then perhaps you can post CREATE TABLE and INSERT statements for a single table that looks like that result set. Just make sure the simplified version shows what the real problem is.
Always say which version of Oracle you're using (e.g. 12.2.0.1.0).
See: How to Ask Questions in Developer Community Spaces - oracle-tech

User_AC3QE

it would be like filtering the select, I can't generate create table similar at the moment
oracle 12c

Frank Kulash

Hi,
I can't generate create table similar at the moment
No problem; I'm not in a hurry. I'll try to help you whenever you can generate the CREATE TABLE and INSERT statements, and post the other things needed.
When you do post them, post them in a new reply; don't just edit your original message. That way it will be easier for people to follow the thread, and harder for them to miss any changes.

mathguy
Answer

OK, I think I understand.
Assuming your existing query works without error, and produces the output you posted: You want to show rows for pairs of (DT_REFERENCIA, CD_SETOR), but with the following conditions: If DS_MOTIVO is anything other than 'NOT ISOLATED', include the row in the output. However, if DS_MOTIVO is 'NOT ISOLATED', then don't include the row unless there are no other rows for the same (DT_REFERENCIA, CD_SETOR) with DS_MOTIVO different from 'NOT ISOLATED'.
If this is correct: Find the main SELECT clause in your existing query, and add the following column (new - it doesn't exist in your code yet):


..., count(case when ds_motivo != 'NOT ISOLATED' then 1 end) 
       over (partition by dt_referencia, cd_setor) as ct_ds_motivo

Then make your entire query - including this extra column - into a subquery in the WITH clause. I assume you understand how to do that - if you don't, write back.
In the final query, select again all the columns you need, FROM this last subquery in the WITH clause, and add

where ct_ds_motivo = 0 or ds_motivo != 'NOT ISOLATED'

First try to make this query work. When it does, look again at your data, and/or think about it and discuss it with your users. Can DS_MOTIVO ever be NULL? If it can, how should that be handled? What I suggested above handles it in one particular way, which may or may not be what you need. (Of course, if the column is NOT NULL, then this is moot.)

Marked as Answer by User_AC3QE · Mar 17 2021
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 19 2018
Added on Feb 19 2018
1 comment
629 views