[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; /** * Receive file service * * @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; } /** * Engineering main method * * @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("Start listening..."); /* * If there is no access it will wait automatically */ Socket socket = server.accept(); System.out.println("There is a link"); receiveFile(socket); } catch (Exception e) { System.out.println("Server exception"); e.printStackTrace (); } } } }); th.run(); // start the thread to run } catch (Exception e) { e.printStackTrace (); } } public void run() { } /** * How to receive files * * @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);// Test to print out the content of the uploaded file dis = new DataInputStream(socket.getInputStream()); File f = new File("/tmp"); if (!f.exists()) { f.mkdir(); } /* * File storage location */ fos = new FileOutputStream(new File(filePath)); inputByte = new byte[1024]; System.out.println("Start receiving data..."); while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) { fos.write(inputByte, 0, length); fos.flush(); } System.out.println("Finish receiving: " + 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; /** * File sending client main program * @author admin_Hzw * */ public class DeployClient{ /** * Program main method * @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"); //The path of the file to be transferred 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("Transmitted: "+((sumL/l)*100)+"%"); dos.write(sendBytes, 0, length); dos.flush(); } //Although the data types are different, JAVA will automatically convert to the same data type and then compare if(sumL==l){ bool = true; } }catch (Exception e) { System.out.println("Client file transfer exception"); bool = false; e.printStackTrace (); } finally{ if (dos != null) dos.close(); if (fis != null) fis.close(); if (socket != null) socket.close(); } System.out.println(bool?"Success":"Failure"); } }
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; /** * Compress or decompress zip: * Due to the direct use of the classes under the java.util.zip toolkit, there will be a problem of Chinese garbled characters, so use the tool classes under org.apache.tools.zip in ant.jar * @author Administrator */ public class ZipUtil { private static byte[] _byte = new byte[1024] ; /** * zip file or path * @param zip destination address * @param srcFiles Compressed source files */ 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 destination address * @param zipOut * @param srcFile compressed file information * @param path relative path in 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(); } } } /** * Unzip the ZIP file and extract the contents of the ZIP file to the targetDIR directory * @param zipName the name of the ZIP file to be decompressed * @param targetBaseDirName target directory */ public static List<File> upzipFile(String zipPath, String descDir) { return upzipFile( new File(zipPath) , descDir ) ; } /** * Unzip the .zip file * @param zipFile unzip file * @param descDir compressed destination address, such as: D:\\test or /mnt/d/test * @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 ; } /** * Delete temporarily generated folders and files under folders */ 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); } }