0

I have the following code snippet that tries to make an HTTP call to my servlet:

    try {
        // Construct data
        String data = URLEncoder.encode("rpt_type", "UTF-8") + "=" + URLEncoder.encode(reportType, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_project", "UTF-8") + "=" + URLEncoder.encode(reportProject, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_mrv_creator", "UTF-8") + "=" + URLEncoder.encode(reportMrvCreator, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_gi_recipient", "UTF-8") + "=" + URLEncoder.encode(reportGiRecipient, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_plant", "UTF-8") + "=" + URLEncoder.encode(reportPlant, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_sloc", "UTF-8") + "=" + URLEncoder.encode(reportStorageLoc, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_gi_no", "UTF-8") + "=" + URLEncoder.encode(reportGiNo, "UTF-8");
        data += "&" + URLEncoder.encode("date_sap_gi_fr", "UTF-8") + "=" + URLEncoder.encode(reportDateGiFrom, "UTF-8");
        data += "&" + URLEncoder.encode("date_sap_gi_to", "UTF-8") + "=" + URLEncoder.encode(reportDateGiTo, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_partno", "UTF-8") + "=" + URLEncoder.encode(reportPartNo, "UTF-8");
        data += "&" + URLEncoder.encode("rpt_so_no", "UTF-8") + "=" + URLEncoder.encode(reportSvcOrderNo, "UTF-8");
        data += "&" + URLEncoder.encode("date_scan_fr", "UTF-8") + "=" + URLEncoder.encode(reportDateScanFrom, "UTF-8");
        data += "&" + URLEncoder.encode("date_scan_to", "UTF-8") + "=" + URLEncoder.encode(reportDateScanTo, "UTF-8");
        System.out.println("[data]\n" + data);

        // Send data
        String urlString = "http://localhost:8080/aerobook/GIStatusReportDownload?" + data;
        System.out.println("[url] " + urlString);
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        //conn.setDoOutput(true);
        //OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        //wr.write(data);
        //wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
        //wr.close();
        rd.close();
    } catch (Exception e) {
    }

My debug output:

[data]
rpt_type=d&rpt_project=aaa&rpt_mrv_creator=bbb&rpt_gi_recipient=ccc&rpt_plant=ddd&rpt_sloc=eee&rpt_gi_no=fff&date_sap_gi_fr=02%2F05%2F2012&date_sap_gi_to=03%2F05%2F2012&rpt_partno=ggg&rpt_so_no=hhh&date_scan_fr=26%2F05%2F2012&date_scan_to=31%2F05%2F2012
[url] http://localhost:8080/aerobook/GIStatusReportDownload?rpt_type=d&rpt_project=aaa&rpt_mrv_creator=bbb&rpt_gi_recipient=ccc&rpt_plant=ddd&rpt_sloc=eee&rpt_gi_no=fff&date_sap_gi_fr=02%2F05%2F2012&date_sap_gi_to=03%2F05%2F2012&rpt_partno=ggg&rpt_so_no=hhh&date_scan_fr=26%2F05%2F2012&date_scan_to=31%2F05%2F2012

On my servlet (in a separate file from the code above), I generate an Excel file for download:

    res.setContentType(sContentType);
    res.setHeader("Content-Disposition", "attachment;filename=\"" + sExcelFileName + "\"");
    OutputStream oOutStrm = res.getOutputStream();
    wbBook.write(oOutStrm);
    oOutStrm.close();

My issue here is that from the URL generated by my code (as shown in the debug output above), I can access my servlet and I manage to get the Save-As dialog.

I'd like to get the contents of the file generated for use within my code. Is there any way I can get the attachment from my code, in byte stream or any other format?


Edit #3: Cleaned up the top

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
ohseekay
  • 795
  • 3
  • 20
  • 37
  • did u check your url in browser? – Subhrajyoti Majumder May 03 '12 at 12:09
  • Yes I did. In my code segment above, `System.out.println(data);` produced the string `key1=value1&key2=value2`. What I did was, I appended "?" to my URL, followed by this string. The result I got was `http://localhost/MyProject/MyServlet?key1=value1&key2=value2`. I entered this into the browser address bar, I was able to download my excel file. I want to get the contents of the excel file on my code, but so far it's not working. – ohseekay May 03 '12 at 12:14
  • ok. check this sample http://www.roseindia.net/java/example/java/io/file-url-download.shtml – Subhrajyoti Majumder May 03 '12 at 12:19
  • Provide code snippet of `wbBook.write()` method – Hardik Mishra May 03 '12 at 12:21
  • @Quio I looked up the link and tried the code inside. It didn't work =/ – ohseekay May 03 '12 at 12:36
  • @ohseekay - *`Is there any way I can get the attachment from my code, in byte stream or any other format?`* Check my answer below. – Ravinder Reddy May 03 '12 at 17:22

4 Answers4

1

When you enter the URI into the browser, you are doing a GET request. Your client Java code however produces a POST request, and sends the parameters in the body, not the URI.

You may want to look at an HTTP trace and compare.

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
  • This sounds reasonable! I'm not sure how to do an HTTP trace, but how do I go about doing a GET request in my Java code? – ohseekay May 03 '12 at 12:32
  • Re: edit #2, I think my code is doing a GET request now but no it's still not working – ohseekay May 03 '12 at 13:00
0

Check wbBook.write(oOutStrm); whether anything has been written into outputStream, also you need call oOutStrm.flash() before close it.

user1335794
  • 1,082
  • 6
  • 12
  • I'm very sure that there's data written into it. As I mentioned in my question, when I access my servlet via the URL, I get a Save-As dialog and I'm able to save the file. However, I'm not able to replicate this on my code. – ohseekay May 03 '12 at 12:06
  • you send this `URL url = new URL("http://localhost/MyProject/MyServlet");` from your code, without any parameters, that is not same as you typed in browser. – user1335794 May 03 '12 at 12:29
  • The three lines after `conn.setDoOutput(true);` are supposed to send the data to my servlet, aren't they? Or am I missing something here? – ohseekay May 03 '12 at 12:38
  • It does not like you thought. Try to send out the same URL that you typed in browser. Or try to add some code in `doPost` method of your servlet, you may send out a post request. – user1335794 May 03 '12 at 12:49
  • Made some changes to match what would have worked on the browser, but still nothing – ohseekay May 03 '12 at 13:00
  • try to comment `conn.setDoOutput(true);`, and added some debug info in to you servlet, you should make sure your request has been handled by the same method in your servlet. – user1335794 May 03 '12 at 13:05
  • Commented out that line, newer version of code with debug lines (they were there all along) put up – ohseekay May 03 '12 at 13:29
  • oh, I mean add some debug info into your servlet, you make sure the request has sent to doGet method. – user1335794 May 03 '12 at 13:32
0

I doubt that the problem lies at OutputStream oOutStrm = res.getOutputStream();.

I bet res is HttpServletResponse and it returns a ServletOutputStream suitable for writing binary data in the response. The servlet container does not encode the binary data

Check API

So, You might not be getting anything except fileName.

In Servlet Try

FileOutputStream stream = new FileOutputStream("c:/excel/Book1.xls");
workBook.write(stream);
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
  • Unfortunately I'm not able to make changes to the servlet, due to certain reasons :( – ohseekay May 03 '12 at 12:33
  • Check edited answer. You have to write something to `OutputStream` else you will get nothing as output. – Hardik Mishra May 03 '12 at 12:40
  • I don't understand what you mean. Right now `wbBook.write(oOutStrm);` works, the content type is set to `application/ms-excel`. The servlet code is inherited by a lot of children classes so it's very hard for me to justify changing the code there. – ohseekay May 03 '12 at 12:53
  • How one can come to know that you have inherited other code ? I have answered based on your post and you had not mentioned for the same also. Anyway hope your problem is solved – Hardik Mishra May 04 '12 at 15:28
0

I want to get the contents of the excel file on my code, but so far it's not working.

I find no errors in the code.
I believe you want to convert content from input stream to an HSSFWorkbook object.
Following code snippet will help you on it.

java.net.URLConnection conn = url.openConnection();  
java.io.InputStream is = conn.getInputStream();  
org.apache.poi.hssf.usermodel.HSSFWorkbook workBook = new org.apache.poi.hssf.usermodel.HSSFWorkbook( is );  
System.out.println( "Number of Sheets: " + workBook.getNumberOfSheets() );  
org.apache.poi.hssf.usermodel.HSSFSheet sheet = workBook.getSheetAt( 0 );  
System.out.println( "Sheet Name: " + sheet.getSheetName() );  

// rest of your code to handle the captured workBook
// cheers
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82