加入收藏 收藏网站 设为首页 设为首页
招生考试网
学历类| 阳光高考 研 究 生 自学考试 成人高考 专 升 本 中考会考 外语类| 四 六 级 职称英语 商务英语 公共英语 日语能力
资格类| 公 务 员 报 关 员 银行从业 司法考试 导 游 证 教师资格 财会类| 会 计 证 经 济 师 会计职称 注册会计 税 务 师
工程类| 一级建造 二级建造 造 价 师 造 价 员 咨 询 师 监 理 师 医学类| 卫生资格 执业医师 执业药师 执业护士 国际护士
计算机| 等级考试 软件水平 应用能力 其它类| 书画等级 美国高考 驾 驶 员 书法等级 少儿英语 报 检 员 单 证 员 出国留学
 招生考试网 - 计算机等级考试 - 考试辅导 - 正文

 
20个非常有用的Java程序片段(2)
来源:fjzsksw.com 2009-12-16 编辑:yangmeiling 【字体:小 大】
20个非常有用的Java程序片段(2)

18. 发送邮件

  Java代码

  import javax.mail.*;

  import javax.mail.internet.*;

  import java.util.*;

  public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException

  {

  boolean debug = false;

  //Set the host smtp address

  Properties props = new Properties();

  props.put("mail.smtp.host", "smtp.example.com");

  // create some properties and get the default Session

  Session session = Session.getDefaultInstance(props, null);

  session.setDebug(debug);

  // create a message

  Message msg = new MimeMessage(session);

  // set the from and to address

  InternetAddress addressFrom = new InternetAddress(from);

  msg.setFrom(addressFrom);

  InternetAddress[] addressTo = new InternetAddress[recipients.length];

  for (int i = 0; i < recipients.length; i++)

  {

  addressTo[i] = new InternetAddress(recipients[i]);

  }

  msg.setRecipients(Message.RecipientType.TO, addressTo);

  // Optional : You can also set your custom headers in the Email if you Want

  msg.addHeader("MyHeaderName", "myHeaderValue");

  // Setting the Subject and Content Type

  msg.setSubject(subject);

  msg.setContent(message, "text/plain");

  Transport.send(msg);

  }

  19. 发送代数据的HTTP 请求

  Java代码

  import java.io.BufferedReader;

  import java.io.InputStreamReader;

  import java.net.URL;

  public class Main {

  public static void main(String[] args)  {

  try {

  URL my_url = new URL("http://coolshell.cn/");

  BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));

  String strTemp = "";

  while(null != (strTemp = br.readLine())){

  System.out.println(strTemp);

  }

  } catch (Exception ex) {

  ex.printStackTrace();

  }

  }

  }

  20. 改变数组的大小

  Java代码

  查看源代码打印帮助

  /**

  * Reallocates an array with a new size, and copies the contents

  * of the old array to the new array.

  * @param oldArray  the old array, to be reallocated.

  * @param newSize   the new array size.

  * @return          A new array with the same contents.

  */

  private static Object resizeArray (Object oldArray, int newSize) {

  int oldSize = java.lang.reflect.Array.getLength(oldArray);

  Class elementType = oldArray.getClass().getComponentType();

  Object newArray = java.lang.reflect.Array.newInstance(

  elementType,newSize);

  int preserveLength = Math.min(oldSize,newSize);

  if (preserveLength > 0)

  System.arraycopy (oldArray,0,newArray,0,preserveLength);

  return newArray;

  }

  // Test routine for resizeArray().

  public static void main (String[] args) {

  int[] a = {1,2,3};

  a = (int[])resizeArray(a,5);

  a[3] = 4;

  a[4] = 5;

  for (int i=0; i<a.length; i++)

  System.out.println (a[i]);

  }

 

相关链接:



 
网站版权与免责声明
①由于各方面情况的不断调整与变化,本网所提供的相关信息请以权威部门公布的正式信息为准.
②本网转载的文/图等稿件出于非商业性目的,如转载稿涉及版权等问题,请在两周内来电联系.
最新文章
热门文章

报名考试
全国 | 黑龙江 | 吉林 | 辽宁 | 内蒙古
青海 | 宁夏 | 甘肃 | 新疆 | 陕西
西藏 | 北京 | 天津 | 河北 | 山东
江苏 | 安徽 | 河南 | 上海 | 浙江
福建 | 广东 | 山西 | 湖南 | 湖北
江西 | 广西 | 海南 | 云南 | 贵州
四川 | 重庆
分省高校计算机考试
黑龙江 | 吉林 | 辽宁 | 内蒙古 | 河北
北京 | 天津 | 新疆 | 甘肃 | 宁夏
青海 | 陕西 | 山西 | 河南 | 山东
江苏 | 安徽 | 浙江 | 福建 | 广东
海南 | 广西 | 江西 | 湖北 | 湖南
四川 | 上海 | 重庆 | 贵州 | 云南
西藏
成绩查询
报考指南
试题答案
模拟试题
考试辅导
计算机一级 | 计算机二级 | 计算机三级 | 计算机四级
经验交流
高校计算机