如何使用java将网页保存为mht格式(2)
来源:优易学  2011-1-5 9:23:17   【优易学:中国教育考试门户网】   资料下载   IT书店

  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();
  }
  }
  }

上一页  [1] [2] [3] 

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训