網頁生成圖片快照
源碼地址:https://github.com/juforg/websnapasimg
使用的核心開源項目:
DJNativeSwing 1.0.3
DJNativeSwing-SWT 1.0.3
org.eclipse.swt.win32.win32.x86_64 4.3
調用
請求地址
測試環境:http://localhost:7090/htmlsnapshot/snaphtml
調用方式 GET
參數名 | 說明 |
---|---|
htmlUrl | 需要將response的html進行截圖的請求地址,需要URLEncoder.encode(htmlUrl,"UTF-8") |
imgType | 生成圖片的類型 如:PNG,GIF,JPG,BMP,RAF |
保存返回的圖片流
HttpClient client =newHttpClient();
HttpMethod get =newGetMethod("http://localhost:8081/htmlsnapshot/snaphtml?htmlUrl=http%3A%2F%2Fsoa2.yundasys.com%3A8080%2Fydmobile%2Fbpsfinance%2Fzhengzhaosy%2F15346handler57090mbtmp.shtml&imgType=png");
int status =-1;
try{
status = client.executeMethod(get);
if(200== status){
File file =newFile(path);//保存圖片至本地的路徑
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
fos =newFileOutputStream(file);
in = get.getResponseBodyAsStream();//獲取流
out =newBufferedOutputStream(fos);
int i;
while((i=in.read())!=-1){
out.write(i);
}
out.flush();
out.close();
in.close();
System.out.println("生成文件:"+path);
}
}catch(Exception e){
;
}finally{//釋放資源
get.releaseConnection();
try{
if(null!=out){
out.close();
}
if(null!=in){
in.close();
}
}catch(IOException e){
e.printStackTrace();
}
}