Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems uploading file to FTP

807603Sep 25 2006 — edited Jan 13 2008
I've got a zip file which work fine on my local file system. After I upload it to a FTP server, the file gets corrupted. It seems something gets appended to the file because the zip file is bigger after I've uploaded it.

When I try to unzip it after I've uploaded it says: error: invalid compressed data to inflate --> 77 extra bytes at beginning or within zipfile

I'm using the Jacarta Jacarta Commons Net FTP package to deal with the FTP server.

I can't figure this one out. Why does my code append data to the file when uploading it to the FTP?

Help!

Here's my code:
/**
 * Uploads a file via ftp.
 * 
 * @param file the file to store on the ftp server
 * @param fileName what to call the file one the ftp server
 * 
 * @throws NeSystemException if an error occurs
 * 
 * @see http://www.rfc-archive.org/getrfc.php?rfc=640 for ftp reply codes
 */
private void ftpUpload(File file, String fileName) throws NeSystemException {

	String ftpServer = "something";
	String ftpUsername = "something";
	String ftpPassword = "something";
	int ftpPort = "something";

	FTPClient ftpClient = new FTPClient();

	try {
		InetAddress inetAddress = InetAddress.getByName(ftpServer);
		ftpClient.connect(inetAddress, ftpPort);

		// After connection attempt, you should check the reply code to verify success.
		if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
			ftpClient.disconnect();
			throw new NeSystemException("FTP server refused connection! Reply string from server is: "
					+ ftpClient.getReplyString());
		}

		// Enter PASV mode because we are behind a firewall
		ftpClient.enterLocalPassiveMode();

		// Login
		ftpClient.login(ftpUsername, ftpPassword);

		// Upload file
		InputStream fis = new FileInputStream(file);
		OutputStream os = ftpClient.storeFileStream(fileName);
		byte buf[] = new byte[8192];
		while (fis.read(buf) != -1) {
			os.write(buf);
		}
		fis.close();
		os.close();

		// Must call completePendingCommand() to finish upload command.
		if (!ftpClient.completePendingCommand()) {
			if (!ftpClient.logout()) {
				if (log.isWarnEnabled()) {
					log.warn("Failed to successfully logout from ftp server! Reply code: "
							+ ftpClient.getReplyCode());
				}
			}
			throw new NeSystemException("File transfer failed!");
		}

		if (!ftpClient.logout()) {
			if (log.isWarnEnabled()) {
				log.warn("Failed to successfully logout from ftp server! Reply code: " + ftpClient.getReplyCode());
			}
		}
	} catch (Exception e) {
		throw new NeSystemException("Error uploading to ftp server '" + ftpServer + "'!", e);
	}  finally {
		if (ftpClient.isConnected()) {
			try {
				ftpClient.disconnect();
			} catch (IOException ioe) {
				if (log.isWarnEnabled()) {
					log.warn("Failed to disconnect from the ftp server!", ioe);
				}
			}
		}
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 10 2008
Added on Sep 25 2006
4 comments
498 views