将html文件中的图片导出到某一文件夹或者生成xml文件

    1. package net.risesoft.riseinfo.integration.parse;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.Reader;
    5. import java.io.StringReader;
    6. import java.util.ArrayList;
    7. import java.util.HashMap;
    8. import java.util.Iterator;
    9. import java.util.List;
    10. import java.util.Map;
    11. import java.util.Set;
    12. import java.util.StringTokenizer;
    13. import com.hothouseobjects.tags.Inspector;
    14. import com.hothouseobjects.tags.Tag;
    15. import com.hothouseobjects.tags.TagTiller;
    16. /*
    17.  * 从html中将img标签的src属性解析出来,并对解析的图片地址进行处理,
    18.  * 主要是为了解决组织部中组工网不在OA上,但信息发布是用fckedit做的,
    19.  * 他的图片不能直接和数据一起通过webservices传到组工网上的问题
    20.  * $author sking huang $date 2008-11-11
    21.  */
    22. public class ParseHtml {
    23.     
    24.     private String htmlSrc=null;
    25.     
    26.     
    27.     private Map tagList =new HashMap();
    28.     
    29.     
    30.     
    31.     private static org.apache.log4j.Logger log = net.risesoft.commons.log.LogFactory
    32.     .getLog(ParseHtml.class);
    33.     public ParseHtml(String htmlSrc){
    34.         this.htmlSrc = htmlSrc;
    35.         
    36.         
    37.     }
    38.     
    39.     
    40.     
    41.     //向标签中列表中增加img标签
    42.     private void initTagToList(){
    43.         //增加取得<a href=...的html标签
    44. //      tagList.put("a", new String[]{"href"});
    45.         
    46. //      增加取得<img src=...的html标签
    47.         tagList.put("img"new String[]{"src"});
    48.     }
    49.     
    50.     public void addTagList(String key,String[] value){
    51.         tagList.put(key, value);
    52.         
    53.     }
    54.     
    55.     public void remove(String key){
    56.         tagList.remove(key);
    57.     }
    58.     public List parse(){
    59.         List imageName=new ArrayList();
    60.         
    61.         log.debug("********开始解析html标签***********");
    62.         //增加标签列表
    63.         initTagToList();
    64.         try{
    65.             
    66.             
    67.         Reader read = new StringReader(htmlSrc);
    68.         
    69.         TagTiller tagtiller = new TagTiller(read);
    70.         
    71.         tagtiller.runTiller();
    72.         
    73.         Tag thePage = tagtiller.getTilledTags();
    74.         
    75.         Set     tagSet   =   tagList.entrySet(); 
    76.         Iterator   iter   =   tagSet.iterator(); 
    77.         //从标签列表中取出要解析的标签,并将解析完的标签加入标签列表
    78.         while(iter.hasNext()) 
    79.         { 
    80.               Map.Entry   entry   =   (Map.Entry)iter.next(); 
    81.               String   key   =  (String)entry.getKey();
    82.               String[]   value   = (String[])  entry.getValue();
    83.               
    84.               if(key == null || "".equals(key)){
    85.                   continue;
    86.               }
    87.               if(value == null || value.length==0){
    88.                   continue;
    89.               }
    90.               List theHref = Inspector.collectByType(thePage,key);
    91.               
    92.               int i = theHref.size();
    93.               
    94.               while (i>0) {
    95.                 for(int ii=0;ii<value.length;ii++){
    96.                     String filterStr=filterStr(((Tag)theHref.get(i-1)).getAttributeValue(value[ii]));
    97.                     if(filterStr!=null){
    98.                         imageName.add(filterStr);
    99.                     }
    100.               }
    101.                 i -=1;
    102.               }
    103.               
    104.         } 
    105.         
    106.         
    107.         
    108.         log.debug("********html标签解析完毕***********");
    109.         
    110.         }catch(Exception e){
    111.             log.error("在解析html的过程中出现问题", e);
    112.         }
    113.         
    114.         return imageName;
    115.     }
    116.     
    117.     //对字符串进行过滤
    118.     private String filterStr(String addr){
    119.         
    120.         if(addr==nullreturn addr;
    121.         
    122.          StringTokenizer parser =new StringTokenizer(addr,"/"///");
    123.          
    124.          String rtn="";
    125.          //取最后一个,因为最后一个为图片的名字
    126.          while(parser.hasMoreTokens()) { 
    127.              rtn=parser.nextToken();
    128.          }
    129.         return rtn;
    130.         
    131.     }
    132.     public static void main(String[] args) {
    133.         
    134.         
    135.         
    136.         try {
    137.             File file = new File("d://ttt.htm");
    138.             int len = (int)file.length();
    139.             byte[] b;
    140.             b = new byte[len];
    141.             FileInputStream fis = new FileInputStream(file);
    142.             fis.read(b);
    143.             fis.close();
    144.          
    145.             ParseHtml pp=new ParseHtml(new String(b));
    146.             List list =pp.parse();
    147.             for(int i=0;list.size()>0;i++){
    148.                 System.out.println(list.get(i));
    149.             }
    150.          
    151.         }
    152.         catch (Exception ex) {
    153.             ex.printStackTrace();
    154.         }
    155.     }
    156. }
    package net.risesoft.riseinfo.integration.parse;
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public abstract class ExportImg {
  4.     private List imgList =new ArrayList();
  5.     private ParseHtml parseHtml =null;
  6.     
  7.     private String imgSrc = null// 图片存放文件夹
  8.     private String imgDest = null;// 图片将要被转移到文件夹,如果不需要转移图片可以调用两个构造函数的方法
  9.     
  10.     public ExportImg(String srcHtml){
  11.         this(srcHtml,null,null);
  12.     }
  13.     
  14.     public ExportImg(String srcHtml,String imgSrc){
  15.         this(srcHtml,imgSrc,null);
  16.     }
  17.     
  18.     public ExportImg(String srcHtml,String imgSrc,String imgDest){
  19.         this.imgSrc=imgSrc;
  20.         this.imgDest=imgDest;
  21.         parseHtml =new ParseHtml(srcHtml);
  22.     }
  23.     
  24.     public List getImgList() {
  25.         return imgList;
  26.     }
  27.     public void setImgList(List imgList) {
  28.         this.imgList = imgList;
  29.     }
  30.     
  31.     public  String export(){
  32.         imgList = parseHtml.parse();
  33.         return operate();
  34.     }
  35.     public abstract String operate();
  36.     public String getImgDest() {
  37.         return imgDest;
  38.     }
  39.     public void setImgDest(String imgDest) {
  40.         this.imgDest = imgDest;
  41.     }
  42.     public String getImgSrc() {
  43.         return imgSrc;
  44.     }
  45.     public void setImgSrc(String imgSrc) {
  46.         this.imgSrc = imgSrc;
  47.     }
  48. }
    1. package net.risesoft.riseinfo.integration.parse;
    2. import java.io.File;
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.io.IOException;
    6. import java.util.ArrayList;
    7. import java.util.List;
    8. /*
    9.  *将fckedit中的图片取出并导入到指定目录
    10.  *@author sking huang
    11.  *@2008-11-11 
    12.  */
    13. public class ExportImgToFile extends ExportImg {
    14.     public ExportImgToFile(String srcHtml, String imgSrc, String imgDest) {
    15.         super(srcHtml, imgSrc, imgDest);
    16.     }
    17.     private static org.apache.log4j.Logger log = net.risesoft.commons.log.LogFactory
    18.             .getLog(ExportImgToFile.class);
    19.     // 在将文件从指定数据源拷贝到指定目录之前请先给图片列表赋值,图片列表中只存图片名称
    20.     public String operate() {
    21.         for (int i = 0; i < super.getImgList().size(); i++) {
    22.             String imageName = (String) super.getImgList().get(i);
    23.             try {
    24.                 File file = new File(getImgSrc() + File.separator + imageName);
    25.                 //如果文件存在且是文件
    26.                 if (file.exists() && file.isFile()) {
    27.                     FileInputStream input = new FileInputStream(file);
    28.                     FileOutputStream output = new FileOutputStream(getImgDest()
    29.                             + File.separator + imageName);
    30.                    byte[] b= new byte[1024];
    31.                                      int size=0;
    32.                     while ((size = input.read(b)) != -1) {
    33.                         output.write(b,0,size);
    34.                                            }
    35.                     input.close();
    36.                     output.close();
    37.                 }
    38.             } catch (IOException e) {
    39.                 log.error("文件导出过程中出现问题", e);
    40.             }
    41.         }
    42.         return null;
    43.     }
    44.     public static void main(String[] args) {
    45.         // 生成图片
    46.         try {
    47.             File file = new File("d://ttt.htm");
    48.             int len = (int) file.length();
    49.             byte[] b;
    50.             b = new byte[len];
    51.             FileInputStream fis = new FileInputStream(file);
    52.             fis.read(b);
    53.             fis.close();
    54.             ExportImg eif = new ExportImgToFile(new String(b), "D://ttt.files",
    55.                     "D://img//dest");
    56.             eif.export();
    57.         } catch (Exception ex) {
    58.             ex.printStackTrace();
    59.         }
    60.     }
    61. }
      1. package net.risesoft.riseinfo.integration.parse;
      2. import java.io.ByteArrayInputStream;
      3. import java.io.File;
      4. import java.io.FileInputStream;
      5. import java.io.FileOutputStream;
      6. import java.io.InputStream;
      7. import java.io.OutputStream;
      8. import javax.xml.parsers.DocumentBuilder;
      9. import javax.xml.parsers.DocumentBuilderFactory;
      10. import org.w3c.dom.Document;
      11. import org.w3c.dom.Node;
      12. import org.w3c.dom.NodeList;
      13. import net.risesoft.integration.adapter.AdapterUtil;
      14. public class ExportImgToXml extends ExportImg {
      15.     private static org.apache.log4j.Logger log = net.risesoft.commons.log.LogFactory
      16.     .getLog(ExportImgToXml.class);
      17.     
      18.     public ExportImgToXml(String srcHtml, String imgSrc) {
      19.         super(srcHtml, imgSrc);
      20.     }
      21.     public String operate() {
      22.         
      23.         StringBuffer sb=new StringBuffer();
      24.         sb.append("<?xml version=/"1.0/" encoding=/"GB2312/"?>");
      25.         sb.append("<DATA>");
      26.         for (int i = 0; i < super.getImgList().size(); i++) {
      27.             String imageName = (String) super.getImgList().get(i);
      28.             try {
      29.                 File file = new File(getImgSrc() + File.separator + imageName);
      30.                 //如果文件存在且是文件
      31.                 if (file.exists() && file.isFile()) {
      32.                     sb.append("<IMGLIST>");
      33.                     sb.append("<IMGNAME>" + file.getName() + "</IMGNAME>");
      34.                     
      35.                     FileInputStream input = new FileInputStream(file);
      36.                     
      37.                     byte[] b=new byte[(int)file.length()];
      38.                     input.read(b);
      39.                     
      40.                     sb.append("<IMGVALUE>" + AdapterUtil.base64Encode(b) + 
      41.                             "</IMGVALUE>");
      42.                     sb.append("</IMGLIST>");
      43.                     input.close();
      44.                 }
      45.             } catch (Exception e) {
      46.                 log.error("文件生成xml过程中出现问题", e);
      47.             }
      48.         }
      49.         sb.append("</DATA>");
      50.         return sb.toString();
      51.     
      52.     }
      53.     //此方法为组工网一端接收xml的例子,只做参考用
      54.     public void parseXml(InputStream is){
      55.         try {
      56.             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      57.             factory.setNamespaceAware(true);
      58.             factory.setValidating(true);
      59.             DocumentBuilder builder = factory.newDocumentBuilder();
      60.             Document doc = builder.parse(is);
      61.             
      62.             
      63.     
      64.                 NodeList attnodeList = doc
      65.                         .getElementsByTagName("IMGLIST");
      66.                 int attlength = attnodeList.getLength();
      67.                 for (int attIndex = 0; attIndex < attlength; attIndex++) { // 实际上,Attachment有0到n个
      68.                     
      69.                     Node attnode = attnodeList.item(attIndex);
      70.                     NodeList attlist = attnode.getChildNodes();
      71.                     String fileName=null;
      72.                     byte[] fileContent=null;
      73.                     for (int j = 0; j < attlist.getLength(); j++) {
      74.                         Node col = attlist.item(j);
      75.                         if (col.getNodeName() == null) {
      76.                             continue;
      77.                         }
      78.                         Node firstChild = col.getFirstChild();
      79.                         if (firstChild == null) {
      80.                             continue;
      81.                         }
      82.                         String value = firstChild.getNodeValue();
      83.                         if (value == null && value.length() == 0) {
      84.                             continue;
      85.                         }
      86.                         String field = col.getNodeName();
      87.                         
      88.                         if (field.equals("IMGVALUE")) {
      89.                             fileContent=AdapterUtil.base64Decode(value);
      90.                             
      91.                         } else if (field.equals("IMGNAME")) {
      92.                             fileName= value;
      93.                         } 
      94.                     }
      95.                     if(fileName!=null && fileContent!=null){
      96.                         
      97.                         File file =new File("D://img//dest//"+fileName);
      98.                         if(!file.exists())
      99.                             file.createNewFile();
      100.                         OutputStream fos =new FileOutputStream(file);
      101.                         fos.write(fileContent);
      102.                         fos.close();
      103.                     }
      104.                 
      105.                 }
      106.                 log.info("**************数据写入成功********************");
      107.             
      108.             
      109.         } catch (Exception ex) {
      110.             
      111.             log.error("附件写入出错了!", ex);
      112.             
      113.         }  
      114.     
      115.     }
      116.     public static void main(String[] args) {
      117.         // 生成图片
      118.         try {
      119.             File file = new File("d://ttt.htm");
      120.             int len = (int) file.length();
      121.             byte[] b;
      122.             b = new byte[len];
      123.             FileInputStream fis = new FileInputStream(file);
      124.             fis.read(b);
      125.             fis.close();
      126.             ExportImgToXml eif = new ExportImgToXml(new String(b), "D://ttt.files");
      127.             
      128.             String img=eif.export();
      129.             //System.out.println(img);
      130.             InputStream is =new ByteArrayInputStream(img.getBytes());
      131.             eif.parseXml(is);
      132.         } catch (Exception ex) {
      133.             ex.printStackTrace();
      134.         }
      135.     }
      136. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值