protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getParameter("lsh") == null || req.getParameter("name") == null || req.getParameter("aaddr") == null) { throw new RuntimeException("Necessary Arg Missing"); } String lsh= req.getParameter("lsh"); String name= req.getParameter("name"); String addr= req.getParameter("addr"); String digest = "6f9ec32c2a08a5527f6932f9c9df90dfc1a0be85";//192.168.17.232:4300 String filename = lsh + "_" + name+ "_" + addr; String surfix = ".pdf"; String contentType = "application/octet-stream"; OutputStream out = null; try { String sql = "SELECT * FROM " + table + " WHERE name = " + name+ " AND addr = " + addr; JSONObject reqJson = new JSONObject(); reqJson.put("stmt", sql); JSONObject respJson = executeRequest(reqJson); if (respJson.getIntValue("code") == -1) { throw new RuntimeException("DonwloadInovice Error: Get digest from " + table + " occured error, please try later"); } JSONArray respArr = respJson.getJSONArray("msg"); JSONObject data = respArr.getJSONObject(0); digest = data.getString("pdfdigest");// To-do,這裏須要確認digest在數據庫中存儲到哪一個字段了 InputStream is = downloadPdf(digest); resp.setHeader("Content-Type", contentType); resp.setHeader("Content-Transfer-Encoding", "binary"); resp.setHeader("Cache-Control","must-revalidate"); resp.setCharacterEncoding("UTF-8"); resp.setHeader("Content-Disposition", "attachment;filename=" + filename + surfix); int read = -1; byte[] bytes = new byte[1024]; out = resp.getOutputStream(); while((read = is.read(bytes)) != -1) { out.write(bytes, 0, read); } }catch (Exception e) { e.printStackTrace(); contentType = "text/html"; resp.setHeader("Content-Type", contentType); String err = "downloadInvoice occured err, err message:" + e.getMessage(); out.write(err.getBytes()); } finally { if (null != out) { out.close(); } } } private InputStream downloadPdf(String digest){ try { HttpGet get = new HttpGet(blobUri(digest)); CloseableHttpResponse response = httpClient.execute(get); InputStream is = response.getEntity().getContent(); return is; }catch (Exception e){ e.printStackTrace(); } return null; } private String blobResourceUri(String index, String digest) { return String.format(Locale.ENGLISH, "%s/%s", index, digest); } private String blobUri(String digest) { return String.format(Locale.ENGLISH, "http://%s:%s/_blobs/%s", url, port, blobResourceUri(pdfblob, digest)); } private JSONObject executeRequest(JSONObject jsonObject) { JSONObject respBO = new JSONObject(); String pdfurl = "http://" + url + ":" + port + "/_sql?pretty"; System.out.println(pdfurl); System.out.println(jsonObject); try { HttpPost httpPost = new HttpPost(pdfurl); HTTPResp resp = client.post(pdfurl, jsonObject); if (resp == null || resp.getRespCode(-1) != 200) { int errorCode = resp.getRespJson().getJSONObject("error").getIntValue("code"); String errorMsg = resp.getRespJson().getJSONObject("error").getString("message"); logger.error(" ----- execute failed :" + resp.getRespMsg()); respBO.put("code", -1); respBO.put("msg", resp.getRespMsg()); } else { logger.info(" ----- execute success"); JSONObject respJson = resp.getRespJson(); JSONArray respArray = reformatData(respJson); respBO.put("code", 0); respBO.put("msg", respArray); } }catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage(), e); respBO.put("code", -1); respBO.put("msg", e.getMessage()); } return respBO; } private JSONArray reformatData(JSONObject arg) { JSONArray rows = arg.getJSONArray("rows"); JSONArray cols = arg.getJSONArray("cols"); JSONArray array = new JSONArray(); for (int i = 0; i < rows.size(); i++) { JSONArray arr = rows.getJSONArray(i); JSONObject one = new JSONObject(); for (int j = 0; j < arr.size(); j ++) { Object value = arr.get(j); String key = cols.getString(j); if (value == null) value = "null";//如果沒有值,則賦值爲null one.put(key, value); } array.add(one); } return array; }
若是下載的文件有問題,那多是上傳的時候就有問題,能夠經過在瀏覽器輸入訪問地址,看可否獲取到相應的文件來檢查,官網樣例:html
sh$ curl -isS '127.0.0.1:4200/_blobs/myblobs/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e' HTTP/1.1 404 Not Found Content-Length: 0
官網地址:https://crate.io/docs/reference/blob.htmlsql
當你輸入的digest和url時就能在瀏覽器上看到你的blob文件,若是看不到,那說明存儲到cratedb的文件是有問題的數據庫
提示:必定要確認存到cratedb的blob文件是可用的,不然白忙活了json