首页 > 技术文章 > 网页生成图片快照

juforg 2015-12-07 22:12 原文

网页生成图片快照

源码地址: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. }





推荐阅读