網頁生成圖片快照


網頁生成圖片快照

源碼地址: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

保存返回的圖片流

  1. HttpClient client =newHttpClient();
  2. HttpMethod get =newGetMethod("http://localhost:8081/htmlsnapshot/snaphtml?htmlUrl=http%3A%2F%2Fsoa2.yundasys.com%3A8080%2Fydmobile%2Fbpsfinance%2Fzhengzhaosy%2F15346handler57090mbtmp.shtml&imgType=png");
  3. int status =-1;
  4. try{
  5. status = client.executeMethod(get);
  6. if(200== status){
  7. File file =newFile(path);//保存圖片至本地的路徑
  8. if(!file.getParentFile().exists()){
  9. file.getParentFile().mkdirs();
  10. }
  11. fos =newFileOutputStream(file);
  12. in = get.getResponseBodyAsStream();//獲取流
  13. out =newBufferedOutputStream(fos);
  14. int i;
  15. while((i=in.read())!=-1){
  16. out.write(i);
  17. }
  18. out.flush();
  19. out.close();
  20. in.close();
  21. System.out.println("生成文件:"+path);
  22. }
  23. }catch(Exception e){
  24. ;
  25. }finally{//釋放資源
  26. get.releaseConnection();
  27. try{
  28. if(null!=out){
  29. out.close();
  30. }
  31. if(null!=in){
  32. in.close();
  33. }
  34. }catch(IOException e){
  35. e.printStackTrace();
  36. }
  37. }






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM