linux系统wget命令的java代码实现

实现如下命令实现的功能:

wget http://www.sohu.com -q -O /root/test/sohu.txt

代码如下:

package com.test.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * linux系统wget命令的java实现
 */
public class WgetUtil {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//实现类似 wget http://www.sohu.com -q -O /root/test/sohu.txt 的功能
		String url="http://www.sohu.com";
		//String location="d:\\test\\sohu.txt";
		String location="/root/test/sohu.txt";
		Boolean ret=WGet(url, location, location + ".temp");
		System.out.println("ret=="+ret);
	}
	public static boolean WGet(String url, String location,
			String location_temp) {
		boolean ret=true;
		String retData=getContent(url);
		System.out.println(url+":return:"+retData);
		if (retData==null)
		{
			ret=false;
			return ret;
		}
		retData=retData.trim();
		if (retData.length()==0)
		{
			System.out.println("系统发生错误!接口返回数据为空.");
			ret=false;
			return ret;
		}
		String errormsg="";
		if ((retData.indexOf("error")>-1) || (retData.indexOf("Exception")>-1) )
		{
			ret=false;
			return ret;
		}
		try {
			FileOutputStream fos = new FileOutputStream(location_temp);
			OutputStreamWriter writer = new OutputStreamWriter(fos);
			writer.write(retData);
			writer.close();
			fos.close();
			File temp = new File(location_temp);
			File filedir = new File(temp.getParent());
			if( !filedir.exists() ) 
			{
				filedir.mkdirs();
			}
			if (temp.exists()) {
				File dest = new File(location);
				File dest_bak = new File(location+"_bak_"+System.currentTimeMillis());
				if (dest.exists())
				{
					dest.renameTo(dest_bak);
					dest.delete();
				}
				temp.renameTo(dest);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return ret;
	}
 
	 /**
	 * 从某个接口取返回数据内容
	 * @param url
	 * @return
	 */
	public static String getContent(String url) {
		String content = null;
		int retry=3; // 重试次数
		for (int i = 0; i < retry; i++) {
			content = tryGetContent(url);
			if (content != null) {
				break;
			}
		}
		return content;
	}
	/**
	 * 从某个接口取返回数据内容
	 * @param url
	 * @return
	 */
	public static String tryGetContent(String url) {
		try {
			return fileGetContents(url);
		} catch (IOException e) {
			System.out.println("Error getURL:" + url);
		}
		return null;
	}
	
	
	/**
	 * 从某个接口取返回数据内容
	 * @param url
	 * @return
	 */
	public static String fileGetContents(String strUrl) throws IOException {
		URL url1 = null;
		BufferedReader reader = null;
		HttpURLConnection connection = null;
		InputStreamReader ins = null ;
		try {
			url1 = new URL(strUrl);
			connection = (HttpURLConnection) url1.openConnection();
			connection.setConnectTimeout(2*1000);
			connection.setReadTimeout(2*1000);
			connection.connect();
			ins =new InputStreamReader(connection.getInputStream());
			reader = new BufferedReader(ins);
			StringBuffer sb = new StringBuffer();
			String line;
			
			while ((line = reader.readLine()) != null) {
				sb.append(line+ "\n");
			}
			return sb.toString();
		} catch (IOException e) {
			System.out.println("Error getURL:" + strUrl);
		}finally
		{
			if(ins != null)
			{
				try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(reader != null)
			{
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(connection != null)
			{
				connection.disconnect();
			}
		}
		return null;
	}	
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值