B
bmcdougald
I have written a servlet that makes an HTTP connection to our report
repository system and returns data to the calling browser in either
text or binary format. The binary formats returned are either Adobe
PDF's or Excel spreadsheets. This is working well and presents data
correctly to a user's browser and/or initiates an HTTP download session
in the browser, which, again, stores the data correctly as text or
binary.
Now, I want to write a batch driven backend java program (not servlet)
that will call the the report repository via the same method above, but
store the data as a file (text or PDF) in a directory somewhere on the
server. I can get the text data portion of the program to write to a
flat file correctly, but the PDF causes the Adobe Reader to fail when
the file is opened. When I inspect the .pdf file to see the contents,
it is all text, not binary as you would expect.
I used the same method for reading Bytes in this program function that
I used in my servlet. Only thing different is that the
DataOutputStream object pipes to a file instead of the
HttpServletResponse object in the servlet, and I'm not setting any
response headers with mime types and such.
What am I missing?
Here is my function code:
static void callUrl( String sType, String sSession,
String sRptName, String sRid,
String sIndexes, String sIPAddr, String sFolder){
byte[] buffer = new byte[8192]; //8k page
boolean binaryFlag = false;
BufferedInputStream in;
BufferedReader ir;
String sUrl;
String inString;
String sFilename = "", sExt = "";
sUrl="http://" + sIPAddr +
"/webaccess/bmc-ctd-wa-cgi.exe?0=report&sid=" + sSession +
"&rid=" + sRid + "&index=" + sIndexes +
"&mode=External&errorflowelem=onerrorxml%2Etxt";
try{
URL url = new URL(sUrl);
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setAllowUserInteraction(false);
/*
* Is Report a PDF - set binary flag true
*/
if(sType.equals("P")){
binaryFlag = true;
sFilename=sRptName +".pdf";
sExt="PDF";
sUrl += "&18=Txt_P_2_Pdf_D";
}
/*
* Is Report a TXT file - set binary flag flase
*/
if(sType.equals("T")){
binaryFlag = false;
sExt="TXT";
sFilename=sRptName +".TXT";
}
sFilename = "x:/users/GS/"+sFolder+"/"+sExt+"/"+sFilename;
File aFile = new File(sFilename);
aFile.createNewFile();
/* Build data stream pipe to output file */
DataOutputStream myStream = new DataOutputStream(
new FileOutputStream(aFile));
if (binaryFlag == true){
/*
* write binary data to output file *** NOT WORKING ***
*/
in = new BufferedInputStream(url.openStream());
while (true) {
int nBytes = in.read(buffer);
if (nBytes < 0) break; // EOF ?
myStream.write(buffer,0,nBytes); // write binary
data to file
}
in.close();
}else{
/*
* write plain text data to output file *** WORKING ***
*/
ir = new BufferedReader(
new InputStreamReader(url.openStream()));
while ( (inString = ir.readLine()) != null ) {
myStream.writeChars(inString);
}
ir.close();
}
myStream.flush();
myStream.close();
}catch (MalformedURLException malformed)
{
System.out.println("Malformed URL");
malformed.printStackTrace();
} // end catch malformed
catch (IOException ioe)
{
System.out.println("BAD IO");
ioe.printStackTrace();
} // end catch IO
catch (Exception e)
{
System.out.println("Exception");
e.printStackTrace();
} //
}
repository system and returns data to the calling browser in either
text or binary format. The binary formats returned are either Adobe
PDF's or Excel spreadsheets. This is working well and presents data
correctly to a user's browser and/or initiates an HTTP download session
in the browser, which, again, stores the data correctly as text or
binary.
Now, I want to write a batch driven backend java program (not servlet)
that will call the the report repository via the same method above, but
store the data as a file (text or PDF) in a directory somewhere on the
server. I can get the text data portion of the program to write to a
flat file correctly, but the PDF causes the Adobe Reader to fail when
the file is opened. When I inspect the .pdf file to see the contents,
it is all text, not binary as you would expect.
I used the same method for reading Bytes in this program function that
I used in my servlet. Only thing different is that the
DataOutputStream object pipes to a file instead of the
HttpServletResponse object in the servlet, and I'm not setting any
response headers with mime types and such.
What am I missing?
Here is my function code:
static void callUrl( String sType, String sSession,
String sRptName, String sRid,
String sIndexes, String sIPAddr, String sFolder){
byte[] buffer = new byte[8192]; //8k page
boolean binaryFlag = false;
BufferedInputStream in;
BufferedReader ir;
String sUrl;
String inString;
String sFilename = "", sExt = "";
sUrl="http://" + sIPAddr +
"/webaccess/bmc-ctd-wa-cgi.exe?0=report&sid=" + sSession +
"&rid=" + sRid + "&index=" + sIndexes +
"&mode=External&errorflowelem=onerrorxml%2Etxt";
try{
URL url = new URL(sUrl);
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setAllowUserInteraction(false);
/*
* Is Report a PDF - set binary flag true
*/
if(sType.equals("P")){
binaryFlag = true;
sFilename=sRptName +".pdf";
sExt="PDF";
sUrl += "&18=Txt_P_2_Pdf_D";
}
/*
* Is Report a TXT file - set binary flag flase
*/
if(sType.equals("T")){
binaryFlag = false;
sExt="TXT";
sFilename=sRptName +".TXT";
}
sFilename = "x:/users/GS/"+sFolder+"/"+sExt+"/"+sFilename;
File aFile = new File(sFilename);
aFile.createNewFile();
/* Build data stream pipe to output file */
DataOutputStream myStream = new DataOutputStream(
new FileOutputStream(aFile));
if (binaryFlag == true){
/*
* write binary data to output file *** NOT WORKING ***
*/
in = new BufferedInputStream(url.openStream());
while (true) {
int nBytes = in.read(buffer);
if (nBytes < 0) break; // EOF ?
myStream.write(buffer,0,nBytes); // write binary
data to file
}
in.close();
}else{
/*
* write plain text data to output file *** WORKING ***
*/
ir = new BufferedReader(
new InputStreamReader(url.openStream()));
while ( (inString = ir.readLine()) != null ) {
myStream.writeChars(inString);
}
ir.close();
}
myStream.flush();
myStream.close();
}catch (MalformedURLException malformed)
{
System.out.println("Malformed URL");
malformed.printStackTrace();
} // end catch malformed
catch (IOException ioe)
{
System.out.println("BAD IO");
ioe.printStackTrace();
} // end catch IO
catch (Exception e)
{
System.out.println("Exception");
e.printStackTrace();
} //
}