您的位置:首页 > 其它

上传文件的jar包 + 简单的可执行 jar 文件包制作

2016-04-15 11:43 567 查看
最近公司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文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: