用过http api 发送邮件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.支持发送邮件和远端文件下载两个功能Demo,环境MyEclipse 6.0.1+jdk1.6 2.import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; public class SearchDomain { public static void main(String[] args) throws ClientProtocolException, IOException { //实例化一个HttpClient HttpClient httpClient = new DefaultHttpClient(); //设定目标站点 web的默认端口80可以不写的 当然如果是其它端口就要标明 HttpHost httpHost = new HttpHost("127.0.0.1",80); //设置需要下载的文件 HttpGet httpGet = new HttpGet("/fax/temp/284/201205171022522920.doc"); //这里也可以直接使用httpGet的绝对地址,当然如果不是具体地址不要忘记/结尾 //HttpGet httpGet = new HttpGet("http://www.0431.la/"); //HttpResponse response = httpClient.execute(httpGet); HttpResponse response = httpClient.execute(httpHost, httpGet); if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){ //请求成功 //取得请求内容 HttpEntity entity = response.getEntity(); //显示内容 if (entity != null) { //这里可以得到文件的类型 如image/jpg /zip /tiff 等等 但是发现并不是十分有效,有时明明后缀是.rar但是取到的是null,这点特别说明 System.out.println(entity.getContentType()); //可以判断是否是文件数据流 System.out.println(entity.isStreaming()); //设置本地保存的文件 File storeFile = new File("e:/111.doc"); FileOutputStream output = new FileOutputStream(storeFile); //得到网络资源并写入文件 InputStream input = entity.getContent(); byte b[] = new byte[1024]; int j = 0; while( (j = input.read(b))!=-1){ output.write(b,0,j); } output.flush(); output.close(); } if (entity != null) { entity.consumeContent(); } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值