java ftp传输文件导致文件损坏

我是新来的这种工作,所以请帮助我。 我发送一个xlsx文件(一个excel文件)到服务器,在我的代码没有错误,并且在ftp服务器中,我使用xampp中的filezilla。 我search谷歌,说它必须存储在一个zip文件,但zip文件也损坏。 这是我的代码

FTPClient upload= new FTPClient(); File firstLocalFile = new File("C:/Users/user/desktop/sample.xlsx"); InputStream inputStream = new FileInputStream(firstLocalFile); try { upload.connect("localhost"); upload.login("user", "pass"); upload.enterLocalPassiveMode(); upload.storeFile("sample.zip", inputStream); } finally { try { upload.logout(); upload.disconnect(); } catch (Exception e) { } } 

任何解决我的问题?

你需要设置文件types使用 –

 upload.setFileType(FTPClient.BINARY_FILE_TYPE); 

此外,添加尝试并抓住upload.storeFile ,以确保其存储文件。

尝试以下

 public void fetchFile() throws Exception { final FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(this.serverDetails.getHostName(), this.serverDetails.getPort()); if (!ftpClient.login(this.serverDetails.getUserName(), this.serverDetails.getPassword())) { throw new IOException("cannot login to server (server reply: " + ftpClient.getReplyCode()); } if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { throw new IOException("Exception in connecting to FTP Server"); } final FTPFile ftpFile = ftpClient.mlistFile(this.serverDetails.getPath()); if (ftpFile.isFile()) { final InputStream is = ftpClient.retrieveFileStream(this.serverDetails.getPath()); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); copyFromRemote(is, ftpFile.getSize()); } else { throw new IOException("Remote file Doesnot Exist"); } } catch (final Exception e) { throw e; } finally { ftpClient.logout(); ftpClient.disconnect(); } } 
Interesting Posts