1. 程式人生 > >上傳檔案的jar包 + 簡單的可執行 jar 檔案包製作

上傳檔案的jar包 + 簡單的可執行 jar 檔案包製作

最近公司ftp 上傳有點問題,老是會出錯,為了保證上傳的準確性(有時候釋出版本需要先上傳ftp),特意做了一個上傳的jar demo。

基本邏輯:  上傳檔案  並記錄檔名, 之後下載剛才上傳的檔案, 對比上傳和下載的MD5值, 之後刪除本地下載的臨時檔案。


import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; import java.security.MessageDigest; public class MyClass { private String Host = "xxx"; private int Port = 18021; private String username = "xxx"; private String password
= "xxxx"; private String rootDir = "xxx/"; private FTPClient ftp; public static String dirSrc = ""; public static String dirSrcParent = ""; public static String downloadPatch = "/download"; public static String dirDes = ""; private static String fileNames[]; private boolean
connect(){ int reply; ftp = new FTPClient(); try { ftp.connect(Host, Port); ftp.login(username, password); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if(!FTPReply.isPositiveCompletion(reply)){ ftp.disconnect(); return false; }else{ ftp.changeWorkingDirectory(rootDir); return true; } } catch (IOException e) { e.printStackTrace(); } return false; } private void closeFtp(){ if(ftp != null && ftp.isConnected()){ try { ftp.logout(); ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } private void upload() throws IOException { File file = new File(dirSrc); if(file.isDirectory()) { if(dirDes != null && dirDes != ""){//要上傳到某個子資料夾 ftp.makeDirectory(dirDes); ftp.changeWorkingDirectory(dirDes); } else { System.out.println("傳到app 目錄下了"); } fileNames = file.list(); for(int i = 0; i < fileNames.length; i++){ File file1 = new File(file.getPath() + "\\" + fileNames[i]); if(file1.isDirectory()){ System.out.println("不能包含子目錄!"); return; } else{ FileInputStream input = new FileInputStream(file1); ftp.storeFile(file1.getName(), input); input.close(); } } } else { System.out.println("你傳錯路徑了!"); } } private void download() throws IOException { ftp.changeWorkingDirectory(dirDes); if(fileNames != null && fileNames.length == 0){ for(int i = 0; i < fileNames.length; i++){ FTPFile file = ftp.mlistFile(fileNames[i]); if(file != null && file.isFile()){ FileOutputStream outputStream = new FileOutputStream(dirSrc + downloadPatch + file.getName()); ftp.retrieveFile(file.getName(), outputStream); outputStream.flush(); outputStream.close(); } System.out.println(fileNames[i] + "沒有下載成功!"); } } else { System.out.println("沒有要下載的檔案!"); } } public static String getFileMD5(File file) { if (!file.isFile()) { return null; } MessageDigest digest = null; FileInputStream in = null; byte buffer[] = new byte[8192]; int len; try { digest = MessageDigest.getInstance("MD5"); in = new FileInputStream(file); while ((len = in.read(buffer)) != -1) { digest.update(buffer, 0, len); } BigInteger bigInt = new BigInteger(1, digest.digest()); return bigInt.toString(16); } catch (Exception e) { e.printStackTrace(); return null; } finally { try { in.close(); } catch (Exception e) { e.printStackTrace(); } } } private static boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { boolean success = deleteDir(new File(dir, children[i])); if (!success) { return false; } } } // 目錄此時為空,可以刪除 return dir.delete(); } public static void main(String[] args){ if(args.length >=1 && args[0] != null && args[0] != "") {//讀取第一級目錄 dirSrc = args[0]; if(args.length >=2 && args[1] != null && args[1] != "") { dirDes = args[1]; } } if(dirSrc == null || dirSrc == ""){ System.out.println("請傳入要上傳檔案的絕對路徑 請傳入目標檔案的相對路徑(如果為空就直接放在app/下面了)"); return; } MyClass myClass = new MyClass(); if(myClass.connect()){ try { myClass.upload(); System.out.println("上傳完畢!!!!!"); } catch (IOException e) { e.printStackTrace(); System.out.println("上傳失敗!"); } } else { System.out.println("連線不上伺服器啊!"); } myClass.closeFtp(); if(myClass.connect()){ try { myClass.download(); System.out.println("下載完畢!!!!!"); } catch (IOException e) { e.printStackTrace(); System.out.println("下載失敗!"); } } else { System.out.println("連線不上伺服器啊!"); } //對比兩個檔案的MD5 File sourceFile; File downloadFile; for(int i = 0; i < fileNames.length; i++){ sourceFile = new File(dirSrc + fileNames[i]); downloadFile = new File(dirSrc + downloadPatch + fileNames[i]); if(sourceFile == null || downloadFile == null){ System.out.println("檔案損壞了!"); return; } if(getFileMD5(sourceFile) == getFileMD5(downloadFile)){ System.out.println(fileNames[i] + "上傳成功了!"); } else { System.out.println(fileNames[i] + "上傳有問題!"); return; } } System.out.println(fileNames.length + "上傳成功了"); File downloadFiles = new File(dirSrc + downloadPatch); deleteDir(downloadFiles); System.out.println( "臨時目錄已經刪除!"); } }

接下來打包成可執行jar

1 將原始碼檔案放到一個目錄下,將依賴的commons-net-3.4.jar 包放到相同目錄下

2 用cmd 進入該目錄後  執行 javac -encoding UTF-8 -classpath commons-net-3.4.jar MyClass.java

3 執行 jar -cvf ftp.jar MyClass.class  ,打包成一個ftp.jar

4. 解壓縮 ftp.jar 並且修改裡面的MANIFEST.MF 檔案:

Manifest-Version: 1.0
Main-Class: MyClass
Class-Path: commons-net-3.4.jar
Created-By: 1.8.0_74 (Oracle Corporation)

5 將剛修改好的檔案 拷貝到與原始碼同一個目錄,在cmd裡面執行 jar cvfm abc.jar MANIFEST.MF MyClass.class

ok  打包成功了。

6.  使用java -jar abc.jar  執行jar檔案