首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 等级考试 > 复习指导 >

使用java将网页保存为mht格式(2)(2)

2009-01-05 
mht格式网页

  *方法说明:得到mht文件的标题
  *输入参数:mhtFilename mht文件名
  *返回类型:mht文件的标题
  */
  public static String getTitle(String mhtFilename) {
  try {
  //TODO readEmlFile
  InputStream fis = new FileInputStream(mhtFilename);
  Session mailSession = Session.getDefaultInstance(System.getProperties(), null);
  MimeMessage msg = new MimeMessage(mailSession, fis);
  Object content = msg.getContent();
  if (content instanceof Multipart) {
  MimeMultipart mp = (MimeMultipart)content;
  MimeBodyPart bp1 = (MimeBodyPart)mp.getBodyPart(0);
  String strEncodng = getEncoding(bp1);
  String strText = getHtmlText(bp1, strEncodng);
  if (strText == null)
  return null;
  strText = strText.toLowerCase();
  int pos1 = strText.indexOf("<title>");
  int pos2 = strText.indexOf("</title>");
  if (pos1 != -1 && pos2!= -1 && pos2 > pos1) {
  return strText.substring(pos1 + 7, pos2).trim();
  }
  }
  return null;
  } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return null;
  }
  }
  /**
  *方法说明:得到html文本
  *输入参数:bp MimeBodyPart类型的网页内容; strEncoding 内容编码
  *返回类型:html文本
  */

    private static String getHtmlText(MimeBodyPart bp, String strEncoding) {
  InputStream textStream = null;
  BufferedInputStream buff = null;
  BufferedReader br = null;
  Reader r = null;
  try {
  textStream = bp.getInputStream();
  buff = new BufferedInputStream(textStream);
  r = new InputStreamReader(buff, strEncoding);
  br = new BufferedReader(r);
  StringBuffer strHtml = new StringBuffer("");
  String strLine = null;
  while ((strLine = br.readLine()) != null) {
  strHtml.append(strLine + "rn");
  }
  br.close();
  r.close();
  textStream.close();
  return strHtml.toString();
  } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } finally{
  try{
  if (br != null)
  br.close();
  if (buff != null)
  buff.close();
  if (textStream != null)
  textStream.close();
  }catch(Exception e){
  System.out.println("解析mht失败");
  }
  }
  return null;
  }
  /**
  *方法说明:保存html文件
  *输入参数:strText html内容; strHtml html文件名
  *返回类型:
  */
  private static void saveHtml(String strText, String strHtml) {
  try {
  FileWriter fw = new FileWriter(strHtml);
  fw.write(strText);
  fw.close();
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  System.out.println("解析mht失败");
  }
  }
  private InternetAddress[] getInetAddresses(String emails) throws Exception {
  ArrayList list = new ArrayList();
  StringTokenizer tok = new StringTokenizer(emails, ",");
  while (tok.hasMoreTokens()) {
  list.add(tok.nextToken());
  }
  int count = list.size();
  InternetAddress[] addresses = new InternetAddress[count];
  for (int i = 0; i < count; i++) {
  addresses[i] = new InternetAddress(list.get(i).toString());
  }
  return addresses;
  }
  class AttachmentDataSource implements DataSource {
  private MimetypesFileTypeMap map = new MimetypesFileTypeMap();
  private String strUrl;
  private String strType;
  private byte[] dataSize = null;
  /**
  * This is some content type maps.
  */
  private Map normalMap = new HashMap();
  {
  // Initiate normal mime type map
  // Images
  normalMap.put("image", "image/jpeg");
  normalMap.put("text", "text/plain");
  }
  public AttachmentDataSource(String strUrl, String strType) {
  this.strType = strType;
  this.strUrl = strUrl;
  strUrl = strUrl.trim();
  strUrl = strUrl.replaceAll(" ", "%20");
  dataSize = JQuery.downBinaryFile(strUrl, null);
  }
  /**
  * Returns the content type.
  */
  public String getContentType() {
  return getMimeType(getName());
  }
  public String getName() {
  char separator = File.separatorChar;
  if( strUrl.lastIndexOf(separator) >= 0 )
  return strUrl.substring(strUrl.lastIndexOf(separator) + 1);
  return strUrl;
  }
  private String getMimeType(String fileName) {
  String type = (String)normalMap.get(strType);
  if (type == null) {
  try {
  type = map.getContentType(fileName);
  } catch (Exception e) {
  // TODO: handle exception
  }
  System.out.println(type);
  // Fix the null exception
  if (type == null) {
  type = "application/octet-stream";
  }
  }
  return type;
  }
  public InputStream getInputStream() throws IOException {
  // TODO Auto-generated method stub
  if (dataSize == null)
  dataSize = new byte[0];
  return new ByteArrayInputStream(dataSize);
  }
  public OutputStream getOutputStream() throws IOException {
  // TODO Auto-generated method stub
  return new java.io.ByteArrayOutputStream();
  }
  }
  }

 

3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/

热点排行