Created
March 7, 2018 10:26
-
-
Save akari0624/95a409d75a675d71f63fc06249504a91 to your computer and use it in GitHub Desktop.
get byteArrayAsStream from netWork , set into BufferedInputStream and fiil the buffer then out.flush again
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SWT裡不知道哪裡來 invoke了這裡,但這就像填空題, 你只要確保這個method out.flush出去的是正確的 byte array 後面的步驟就應該也是對的 | |
public boolean createpdf(String filename) { | |
PostMethod post = null; | |
boolean result; | |
OutputStream os = null; | |
try { | |
/* | |
* 省略 | |
* | |
*/ | |
String urlStr = ""; | |
JsonObject jsonObj = new JsonObject(); | |
if (landID.compareTo(filename.substring(2, 4)) == 0) { | |
/* | |
* | |
* 省略 | |
* | |
*/ | |
} else { | |
/* | |
* | |
* 省略 | |
* | |
*/ | |
post = HttpClientService.getByteArrayAndTransToInputStream(urlStr, | |
jsonObj.toString()); // 這裡會blocking住 | |
// ,直到對方servlet回應為止 | |
if (post != null) { | |
System.out.println("對方 回應200"); | |
Map<String, String> responseHeaderMap = new HashMap<String, String>(); | |
Header[] theHeaders = post.getResponseHeaders(); | |
for (Header h : theHeaders) { | |
responseHeaderMap.put(h.getName(), h.getValue()); | |
System.out.println("the header:"+h.getName()+" , "+ h.getValue()); | |
} // 為了取得所有回應標頭.... | |
String pageCount = "0"; | |
// 2017.02.16 回應標頭的page_no2 有可能是空字串 這也是錯誤的狀況 | |
// 回應標頭裡的page_no1是 實際計算金額的頁數 , page_no2是 PDF頁數 | |
if (responseHeaderMap.get("page_no2") != null && !responseHeaderMap.get("page_no2").equals("")) { | |
System.out.println("回傳的 page_no2回應標頭 正確"); | |
pageCount = responseHeaderMap.get("page_no2"); | |
pdfdao.setPageNumber(Integer.parseInt(pageCount)); //如果回應標頭page_no2是空字串的話 這行會拋出 NumberFormatException:For input string: "" , | |
// 在catch裡 result就變false | |
pdfdao.setRealPageNumber(Integer.parseInt(responseHeaderMap | |
.get("page_no1"))); | |
BufferedInputStream br = new BufferedInputStream( | |
post.getResponseBodyAsStream()); // 取得response | |
// body(byte | |
// array) | |
System.out.println("取得 對方 丟回來的 PDF Byte Array"); | |
int length; | |
byte[] pdfByteArray = new byte[4096]; | |
while ((length = br.read(pdfByteArray)) != -1) { | |
os.write(pdfByteArray, 0, length); | |
} | |
os.flush(); | |
System.out.println("檔案 產製成功!!!"); | |
result = true; | |
} else { // 取不到response header的話 就表示對方的response是錯的 | |
pdfdao.setPageNumber(0); | |
pdfdao.setRealPageNumber(0); | |
System.out | |
.println("Some Error Message"); | |
result = false; | |
} | |
} else { | |
System.out.println("執行HttpClient時失敗 請檢查 \"afterDoCrossOffice_And_3Table_Inserted_ThenShouldRetrievePdf urlStr>>>>>>>>>>>>>>\" 網址是否正確"); | |
result = false; | |
} | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
System.out.println("createPDF exception :" | |
+ e.getMessage()); | |
pdfdao.setPageNumber(0); | |
pdfdao.setRealPageNumber(0); | |
result = false; | |
} finally { | |
post.releaseConnection(); | |
System.out.println("HttpClient PostMethod物件 順利關閉"); | |
if(os != null){ | |
try { | |
os.close(); | |
} catch (IOException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment