linux系统wget命令的java代码实现
来源:互联网 发布:跨境电商erp软件 编辑:程序博客网 时间:2024/05/24 05:34
实现如下命令实现的功能:
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;}}
- linux系统wget命令的java代码实现
- linux的wget命令
- linux的wget命令
- linux的 wget 命令
- linux的wget命令
- linux的wget命令
- Linux的wget命令
- Linux系统中下载文件的wget命令操作实例
- Linux系统中下载文件的wget命令操作实例
- linux系统命令wget命令详解
- linux的下载命令wget
- Linux下的wget命令
- 常用的linux命令-wget
- java代码实现ping命令(支持linux系统)
- linux命令:wget命令
- linux下用代码实现系统命令
- 常用的linux命令---wget,find
- Linux的下载命令wget详解
- 影响性能的几个方面
- sed与awk常用功能
- 向大家推荐20本最好的Linux免费书籍[英文版]
- 《COM技术内幕》读书笔记——第4章 引用计数
- 直接创建一个DataTable,并为之添加数据(自定义DataTable)
- linux系统wget命令的java代码实现
- 维护网络安全有妙招
- WsaAsyncSelect服务器端代码
- 如何隐藏Android4.0及以上版本的ActionBar、NotificationBar、StatusBar、SystemBar、TitleBar
- Linux内核学习的一些国外网站
- 抽象的操作系统(九) --- 硬件系统
- Spring安全权限管理(Spring Security)
- 使用UDEV在Oracle Linux 6上安装Oracle 11g RAC(11.2.0.3) (二)
- linux文件描述符open file descriptors与open files的区别