Zip文件在上传到服务器时损坏

我的java程序将zip文件从我的系统上传到FTP服务器。 uploadfile()是一个包含上传代码的函数。

uploadfile( “192.168.0.210”, “muruganp”, “vm4snk”, “/首页/管理/ GATE521 / LN_RB_Semivalid2junk /输出/” +日期+ “_ RB1.zip”, “/文件服务器/ filesbackup / EMAC /” +日期+“_ RB1 。压缩”);

 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模式传输。 它应该按照本问题实际通过二进制模式传输。 如何达到同样的目标? 请指教。

最好的猜测是FTP上传使用的是ascii模式,它会像zip一样破坏二进制文件。 validation这一点,如果是,请将其更改为二进制模式。

在上传之前,使用FTPClient的setFileType方法将其设置为FTP.BINARY_FILE_TYPE

我只是使用setFileType(FTP.BINARY_FILE_TYPE)来解决它。 这些信息真有帮助! 非常感谢。

Interesting Posts