/**
* 合同URL转换-空白合同(服务器加了白名单)
* @return
* @throws IOException
*/
@RequestMapping(value = "/yunji/agreement/get_template", method = RequestMethod.GET)
public void get_template(String s, HttpServletResponse response) {
String path = formsOpenapiConfig.getZd_post_url()+"api/v1/yunji/agreement/get_template?s="+s;
try {
byte[] html = yunJiService.createHtml(path);
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new String(html));
} catch (IOException e) {
e.printStackTrace();
}
response.setStatus(HttpServletResponse.SC_OK);
}
/**
* 合同URL转换-PDF(服务器加了白名单)
* @return
* @throws IOException
*/
@RequestMapping(value = "/link", method = RequestMethod.GET)
public void link(String s, HttpServletResponse response) {
String path = formsOpenapiConfig.getZd_post_url()+"api/v1/link?s="+s;
try {
byte[] html = yunJiService.createHtml(path);
response.setContentType("application/pdf;charset=UTF-8");
response.setCharacterEncoding("utf-8");
response.getOutputStream().write(html);
} catch (IOException e) {
e.printStackTrace();
}
response.setStatus(HttpServletResponse.SC_OK);
}
public byte[] createHtml(String path) throws IOException {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置超时间为3秒
conn.setConnectTimeout(3000);
// 防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// 得到输入流
InputStream is = conn.getInputStream();
byte[] body = readInputStream(is);
// 关闭流
is.close();
return body;
}
/**
* 从输入流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
private byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}