Zip 文件上传到服务器时损坏
我的 java 程序将 zip 文件从我的系统上传到 FTP 服务器。 uploadfile()
是一个包含上传代码的函数。
uploadfile("192.168.0.210","muruganp","vm4snk","/home/Admin/GATE521/LN_RB_Semivalid2junk/Output/"+date+"_RB1.zip","/fileserver/filesbackup/Emac/"+日期+“_RB1.zip”);
public static boolean uploadfile(String server, String username,
String Password, String source_file_path, String dest_dir) {
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(server);
ftp.login(username, Password);
System.out.println("Connected to " + server + ".");
System.out.print(ftp.getReplyString());
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
return false;
}
System.out.println("FTP server connected.");
InputStream input = new FileInputStream(source_file_path);
ftp.storeFile(dest_dir, input);
System.out.println(ftp.getReplyString());
input.close();
ftp.logout();
} catch (Exception e) {
System.out.println("err");
e.printStackTrace();
return false;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (Exception ioe) {}
}
}
return true;
}
我系统中的 zip 文件是完美的。但是在服务器位置上传相同的内容后,下载相同的内容并解压就会出现问题。错误提示“文件已损坏”。我应该怎么做才能解决这个问题。请就此提出建议。
我怀疑问题可能是通过 ASCII 模式传输。它实际上应该按照这个问题通过二进制模式传输。如何达到同样的效果?请指教。
My java program uploads a zip file from my system to FTP server. uploadfile()
is a function that contains the uploading code.
uploadfile("192.168.0.210","muruganp","vm4snk","/home/Admin/GATE521/LN_RB_Semivalid2junk/Output/"+date+"_RB1.zip","/fileserver/filesbackup/Emac/"+date+"_RB1.zip");
public static boolean uploadfile(String server, String username,
String Password, String source_file_path, String dest_dir) {
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(server);
ftp.login(username, Password);
System.out.println("Connected to " + server + ".");
System.out.print(ftp.getReplyString());
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
return false;
}
System.out.println("FTP server connected.");
InputStream input = new FileInputStream(source_file_path);
ftp.storeFile(dest_dir, input);
System.out.println(ftp.getReplyString());
input.close();
ftp.logout();
} catch (Exception e) {
System.out.println("err");
e.printStackTrace();
return false;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (Exception ioe) {}
}
}
return true;
}
The zip file that I do have in my system is perfect. But after uploading the same in the server location,downloading the same, and extracting the problem occurs. "The file is corrupt" says the error. What should I do to resolve this issue. Kindly advise on this.
I suspect the problem would be something like, transferring through ASCII mode. It should actually be transferred through binary mode as per this QUESTION. How to attain the same? Please advise.
最好的猜测是 FTP 上传使用的是 ascii 模式,这会损坏 zip 等二进制文件。验证这一点,如果是这样,请将其更改为二进制模式。
Best guess is that the FTP upload is using ascii mode which will corrupt a binary file like a zip. Verify this and if so change it to binary mode instead.