`
JadeLuo
  • 浏览: 411310 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

自动发布工具类

 
阅读更多
[root@gris-test101 tmp]# cat cp_regulation.sh 
#ls -al 
#chmod +x regulation-1.1.0-SNAPSHOT.jar
#ls -al regulation-1.1.0-SNAPSHOT.jar
mkdir -p /tmp/bak
mv /data/web/webapp/WEB-INF/lib/regulation-1.1.0-SNAPSHOT.jar /tmp/bak
ls -al /data/web/webapp/WEB-INF/lib/regulation-1.1.0-SNAPSHOT.jar
mv regulation-1.1.0-SNAPSHOT.jar /data/web/webapp/WEB-INF/lib/
ls -al /data/web/webapp/WEB-INF/lib/regulation-1.1.0-SNAPSHOT.jar

[root@gris-test101 tmp]# cat DeployServer.java 

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;

//import org.apache.commons.fileupload.util.Streams;

/**
 * 接收文件服务
 * 
 * @author admin_Hzw
 * 
 */
public class DeployServer {
	private static String getIp() {
		String ip = "127.0.0.1";
		try {
			ip = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return ip;
	}

	/**
	 * 工程main方法
	 * 
	 * @param args
	 */
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		try {
			final ServerSocket server = new ServerSocket(48123);
			Thread th = new Thread(new Runnable() {
				public void run() {
					while (true) {
						try {
							System.out.println("开始监听...");
							/*
							 * 如果没有访问它会自动等待
							 */
							Socket socket = server.accept();
							System.out.println("有链接");
							receiveFile(socket);
						} catch (Exception e) {
							System.out.println("服务器异常");
							e.printStackTrace();
						}
					}
				}
			});
			th.run(); // 启动线程运行
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void run() {
	}

	/**
	 * 接收文件方法
	 * 
	 * @param socket
	 * @throws IOException
	 */
	public static void receiveFile(Socket socket) throws IOException {
		byte[] inputByte = null;
		int length = 0;
		DataInputStream dis = null;
		FileOutputStream fos = null;
		
		if("10.51.111.101".equals(getIp())){
			
		}
		String filePath = "/data/web/webapp/WEB-INF/lib/regulation-1.1.0-SNAPSHOT.jar";
    filePath = "/tmp/regulation-1.1.0-SNAPSHOT.jar";
		try {
			try {
				// String fileContent =
				// Streams.asString(socket.getInputStream());
				// System.out.println(fileContent);// 测试打印出上传文件的内容

				dis = new DataInputStream(socket.getInputStream());
				File f = new File("/tmp");
				if (!f.exists()) {
					f.mkdir();
				}
				/*
				 * 文件存储位置
				 */
				fos = new FileOutputStream(new File(filePath));
				inputByte = new byte[1024];
				System.out.println("开始接收数据...");
				while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
					fos.write(inputByte, 0, length);
					fos.flush();
				}
				System.out.println("完成接收:" + filePath);
			} finally {
				if (fos != null)
					fos.close();
				if (dis != null)
					dis.close();
				if (socket != null)
					socket.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

package com.ygsoft.community.regulation.util.ext;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * 文件发送客户端主程序
 * @author admin_Hzw
 *
 */
public class DeployClient{
	
	/**
	 * 程序main方法
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		File zipFile = new File("f:/webapp.war");
    	String descDir = "f:/descDir";
    	File f = new File(descDir);
		if (!f.exists()) {
			f.mkdir();
		}
    	ZipUtil.upzipFile(zipFile, descDir);
    	
		int length = 0;
		double sumL = 0 ;
		byte[] sendBytes = null;
		Socket socket = null;
		DataOutputStream dos = null;
		FileInputStream fis = null;
		boolean bool = false;
		try {
			File file = new File("F:/descDir/WEB-INF/lib/regulation-1.1.0-SNAPSHOT.jar"); //要传输的文件路径
			long l = file.length(); 
			socket = new Socket();  
//			socket.connect(new InetSocketAddress("127.0.0.1", 48123));
			socket.connect(new InetSocketAddress("10.51.111.101", 48123));
			dos = new DataOutputStream(socket.getOutputStream());
			fis = new FileInputStream(file);      
			sendBytes = new byte[1024];  
			while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
				sumL += length;  
				System.out.println("已传输:"+((sumL/l)*100)+"%");
				dos.write(sendBytes, 0, length);
				dos.flush();
			} 
			//虽然数据类型不同,但JAVA会自动转换成相同数据类型后在做比较
			if(sumL==l){
				bool = true;
			}
		}catch (Exception e) {
			System.out.println("客户端文件传输异常");
			bool = false;
			e.printStackTrace();  
		} finally{  
			if (dos != null)
				dos.close();
			if (fis != null)
				fis.close();   
			if (socket != null)
				socket.close();    
		}
		System.out.println(bool?"成功":"失败");
	}
}

 

package com.ygsoft.community.regulation.util.ext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;


/**
 * 压缩或解压zip:
 * 由于直接使用java.util.zip工具包下的类,会出现中文乱码问题,所以使用ant.jar中的org.apache.tools.zip下的工具类
 * @author Administrator
 */

public class ZipUtil {
    private static byte[] _byte = new byte[1024] ;
    /**
     * 压缩文件或路径
     * @param zip 压缩的目的地址
     * @param srcFiles 压缩的源文件
     */
    public static void zipFile( String zip , List<File> srcFiles ){
        try {
            if( zip.endsWith(".zip") || zip.endsWith(".ZIP") ){
                ZipOutputStream _zipOut = new ZipOutputStream(new FileOutputStream(new File(zip))) ;
                _zipOut.setEncoding("UTF-8");
                for( File _f : srcFiles ){
                    handlerFile(zip , _zipOut , _f , "");
                }
                _zipOut.close();
            }else{
                System.out.println("target file[" + zip + "] is not .zip type file");
            }
        } catch (FileNotFoundException e) {
        	e.printStackTrace();
        } catch (IOException e) {
        	e.printStackTrace();
        }
    }
    
    /**
     * 
     * @param zip 压缩的目的地址
     * @param zipOut 
     * @param srcFile  被压缩的文件信息
     * @param path  在zip中的相对路径
     * @throws IOException
     */
    private static void handlerFile(String zip , ZipOutputStream zipOut , File srcFile , String path ) throws IOException{
        System.out.println(" begin to compression file[" + srcFile.getName() + "]");
        if( !"".equals(path) && ! path.endsWith(File.separator)){
            path += File.separator ;
        }
        if( ! srcFile.getPath().equals(zip) ){
            if( srcFile.isDirectory() ){
                File[] _files = srcFile.listFiles() ;
                if( _files.length == 0 ){
                    zipOut.putNextEntry(new ZipEntry( path + srcFile.getName() + File.separator));
                    zipOut.closeEntry();
                }else{
                    for( File _f : _files ){
                        handlerFile( zip ,zipOut , _f , path + srcFile.getName() );
                    }
                }
            }else{
                InputStream _in = new FileInputStream(srcFile) ;
                zipOut.putNextEntry(new ZipEntry(path + srcFile.getName()));
                int len = 0 ; 
                while( (len = _in.read(_byte)) > 0  ){
                    zipOut.write(_byte, 0, len);
                }
                _in.close();
                zipOut.closeEntry();
            }
        }
    }

    /**
     * 解压缩ZIP文件,将ZIP文件里的内容解压到targetDIR目录下
     * @param zipName 待解压缩的ZIP文件名
     * @param targetBaseDirName  目标目录
     */
    public static List<File> upzipFile(String zipPath, String descDir) {
        return upzipFile( new File(zipPath) , descDir ) ;
    }
    
    /**
     * 对.zip文件进行解压缩
     * @param zipFile  解压缩文件
     * @param descDir  压缩的目标地址,如:D:\\测试 或 /mnt/d/测试
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static List<File> upzipFile(File zipFile, String descDir) {
        List<File> _list = new ArrayList<File>() ;
        try {
            ZipFile _zipFile = new ZipFile(zipFile , "GBK") ;
            for( Enumeration entries = _zipFile.getEntries() ; entries.hasMoreElements() ; ){
                ZipEntry entry = (ZipEntry)entries.nextElement() ;
                File _file = new File(descDir + File.separator + entry.getName()) ;
                if( entry.isDirectory() ){
                    _file.mkdirs() ;
                }else{
                    File _parent = _file.getParentFile() ;
                    if( !_parent.exists() ){
                        _parent.mkdirs() ;
                    }
                    InputStream _in = _zipFile.getInputStream(entry);
                    OutputStream _out = new FileOutputStream(_file) ;
                    int len = 0 ;
                    while( (len = _in.read(_byte)) > 0){
                        _out.write(_byte, 0, len);
                    }
                    _in.close(); 
                    _out.flush();
                    _out.close();
                    _list.add(_file) ;
                }
            }
        } catch (IOException e) {
        	e.printStackTrace();
        }
        return _list ;
    }
    
    /**
     * 对临时生成的文件夹和文件夹下的文件进行删除
     */
    public static void deletefile(String delpath) {
        try {
            File file = new File(delpath);
            if (!file.isDirectory()) {
                file.delete();
            } else if (file.isDirectory()) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File delfile = new File(delpath + File.separator + filelist[i]);
                    if (!delfile.isDirectory()) {
                        delfile.delete();
                    } else if (delfile.isDirectory()) {
                        deletefile(delpath + File.separator + filelist[i]);
                    }
                }
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
    	File zipFile = new File("f:/webapp.war");
    	String descDir = "f:/descDir";
    	File f = new File(descDir);
		if (!f.exists()) {
			f.mkdir();
		}
    	ZipUtil.upzipFile(zipFile, descDir);
    }
    
}

 

分享到:
评论

相关推荐

    java自己修改的支持public属性、父类属性的json自动编译的工具类

    java自己修改的支持public属性、父类属性的json自动编译的工具类

    微头条全自动发布1.4微头条发布器-其它工具类资源.zip

    微头条全自动发布1.4微头条发布器-其它工具类资源.zip

    java微信交互开发工具类

    java版微信交互开发工具类,包含图灵机器人自动回复。微信交互处理类及网页授权功能。这是之前发布的了,把分调低了重新发布。

    自动投票小工具源代码

    web 控件操作例程,使用的技术包括: 浏览器视图的使用 网页form及input按钮的读写以及自动提交form 定时器的使用 图形数字识别 ...可以自己修改不同的投票对象为他进行自动投票 ...最后编译环境是vs2008

    SEO外链自动发布外链工具网站源码

    去搜索引擎搜索那些关于seo外链工具,像久爱资源网站长工具,还有很多小站点提供的各式各样的在线外链发布工具都有很好的排名,而且这类工具的流量也是比较大的,可见这类工具的还是有很多人需求的。 外链更新介绍 ...

    自己编写的Post提交工具

    之前寻找post工具,没有找到特别符合要求...工具需要.net环境,共编译3个版本,分别为3.5,4.0,4.5。 工具支持选择请求编码,选择响应编码,支持选择请求内容类型,支持上传文件。若服务器返回错误,也会显示错误代码。

    java自动打包工具

    介绍下这个打包工具 把本次你改动的java工程下的文件对应的class文件自动打包,然后热部署发布即可 如果你需要频繁改动java代码,热发布几个文件到服务器上,你会怎么做呢? 没有工具的人会手动拷贝目录 然后到...

    反编译工具

    Android Killer 是一款可视化的安卓应用逆向工具,集Apk反编译、Apk打包、Apk签名,编码互转,ADB通信(应用安装-卸载-运行-设备文件管理)等特色功能于一 身,支持logcat日志输出,语法高亮,基于关键字(支持单行...

    java反编译工具.class反编译工具

    绝对好的反编译工具,打开后选择所需反编译的.class文件,即可瞬间反编译出java类文件,并且会自动查找出同包的其他.class文件,方便查看反编译效果,让你在编程大道上再添助力,不下载用用,你会后悔的,我多方查找...

    反编译工具.NET Reflector 6.5.0.135

     Reflector的出现使.NET程序员眼前豁然开朗,因为这个免费工具可以将.NET程序集中的中间语言反编译成C#或者Visual Basic代码。除了能将IL转换为C#或Visual Basic以外,Reflector还能够提供程序集中类及其成员的概要...

    java class文件的反编译工具

    对class 文件进行反编译 排查线上的class文件的代码是否一致,可以将文件反编译后 ,对比本地代码。 支持拖拉 自动加载该类的引用类

    .Net编码自动生成工具 最新版

    采用 Model + DAL + BLL + Web 的设计,主要实现在 C# 中对应数据库中表的基类代码的自动生成,包括生成属性、添加、修改、删除、查询、存在性、 Model 类构造等基础代码片断,使程序员可以节省大量机械录入的时间和...

    .net dll反编译工具

    Reflector for .NET Remotesoft .NET Explorer .dll 文件反编译的工具软件: Reflector for .NET Remotesoft .NET Explorer Remotesoft .NET ...在search 框中输入以字母开头的类,就可以自动显示相关的类.具体自己去试吧

    补丁、打包编译后的文件,自动生成class、jsp、js等文件目录(附教程)

    利用工具类自动生成编译后的文件目录,对于上线部署很好用

    比Ansible更吊的自动化运维工具,自动化统一安装部署unifyDeploy0.4版本发布

    自动化部署与统一安装升级 - 类ansible工具 unifyDeploy0.3版本发布 (更新时间2014-12-24) http://www.cnblogs.com/Javame/p/3835575.html 新增功能: 逻辑与业务分离,完美实现逻辑与业务分离,业务实现统一...

    mybatis反编译工具

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实体类、DAO接口和Mapper映射文件。

    工具箱(java开发中常用工具类,web工程中常用的Filter等).zip

    API管理工具则方便开发者创建、测试、发布和维护API接口。 持续集成与持续部署(CI/CD): Jenkins、Travis CI、GitHub Actions等工具负责自动化构建、测试和部署流程,提高交付效率和可靠性。 数据库管理与...

    ILSpy反编译工具

    此款反编译工具 是我用过比较好的一款,能自动提取类,里面的解析功能也很好,我用过它反编译过分页控件。

    Windows窗口自动化操作类forVB6_V2.0 clsWindow源码

    假设记事本标题为“测试要求.txt - 记事本”,通过SPY等工具查看得知记事本的文本框类名为:Edit,那么我们编写程序如下: Dim window As New clsWindow If window.GetWindowByTitle("测试要求.txt - 记事本").hWnd ...

    AUTOIT生成自动安装的工具

    3.自动读取安装程序图标,编译时也可以选择其他图标 4.可自定义编译EXE版本信息 5.支持组件多控件选择(例如标准树形treeviw、列表listview控件)、文本edit控件修改文本(例如安装路径、序列号输入) 6.新增隐藏...

Global site tag (gtag.js) - Google Analytics