Uploaded image for project: 'Commons Net'
  1. Commons Net
  2. NET-429

FTPClient storeFile method creates a file of 0 bytes

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Cannot Reproduce
    • Affects Version/s: 1.4, 3.0.1
    • Fix Version/s: None
    • Component/s: FTP
    • Environment:

      jdk1.6.0_27
      Windows 7 Pro SP1 local machine
      Uploading to a GoDaddy Linux shared hosting environment. File is created with 0x604 permissions.

      Description

      Using the FTPClient object in commons net the way it is shown in the Javadoc results in a file that is 0 bytes on the remote host. When you get to the storeFile(..) method, it waits for about 20 seconds before it returns the error message shown to System.out.

      I've tried a number of different things

      1. commons-net version 3.0.1 (supposedly had the bug fix for this, but didn't work for me)
      2. [boolean] storeFile (String, InputStream)
      3. [OutputStream] storeFile (String)
      4. commons-net version 1.4 (still no dice)
      5. ftp.connect(server) and ftp.connect(server, 21)

      Here's my code:

      		boolean error = false;
      		FTPClient ftp = new FTPClient();
      		InputStream localStream = null;
      		try {
      			int reply;
      			String server = "ftp.myserver.com";
      			ftp.connect(server, 21);
      			System.out.println("Connected to " + server + ".");
      			System.out.print(ftp.getReplyString());
      
      			// After connection attempt, you should check the reply code to
      			// verify
      			// success.
      			reply = ftp.getReplyCode();
      
      			if (!FTPReply.isPositiveCompletion(reply)) {
      				ftp.disconnect();
      				System.err.println("FTP server refused connection.");
      				System.exit(1);
      			}
      			boolean wasSuccessful = ftp.login("user", "password");
      			if (!wasSuccessful) {
      				System.err.println("Login unsuccessful");
      				System.exit(1);
      			}
      			
      			System.out.println("login successful");
      			String localPath="C:/projects/TEMP/uga.html";
      			File localFile = new File(localPath);
      			
      			if (localFile.exists()) {
      				localStream = new BufferedInputStream(new FileInputStream(localPath));
      				wasSuccessful = ftp.storeFile("/offline3/uga.html", localStream);
      				if (!wasSuccessful) {
      					System.err.println("could not store file: " + localPath);
      					System.exit(1);
      				} else {
      					System.out.println("Successfully saved file!");
      				}
      			}
      
      			
      			// transfer files
      			ftp.logout();
      		} catch (IOException e) {
      			error = true;
      			e.printStackTrace();
      		} finally {
      			if (ftp.isConnected()) {
      				try {
      					ftp.disconnect();
      				} catch (IOException ioe) {
      					// do nothing
      				}
      			}
      			if (localStream != null) {
      				try {
      					localStream.close();
      				} catch (IOException e) {
      				}
      			}
      			System.exit(error ? 1 : 0);
      	}
      

      Here's my System.out:

      Connected to ftp.harmari.com.
      220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
      220-You are user number 16 of 500 allowed.
      220-Local time is now 05:59. Server port: 21.
      220-This is a private system - No anonymous login
      220 You will be disconnected after 3 minutes of inactivity.
      login successful
      could not store file: C:/projects/TEMP/uga.html
      
      

      More background:
      FileZilla works on my machine
      I've also tried the primitive Sun API, and it works perfectly:

      		try {
      			url = new URL("ftp://username:password@ftp.myserver.com/offline3/uga.html;type=i");
      			URLConnection urlc = url.openConnection();
      			OutputStream os = urlc.getOutputStream(); // To upload
      			
      			String localPath = "C:/projects/TEMP/uga.html";
      			InputStream bis = new BufferedInputStream(new FileInputStream(localPath));
      			
      			byte[] bstr = new byte[1024];
      			while (bis.read(bstr) > 0) {
      				os.write(bstr);				
      			}
      		} catch (MalformedURLException e) {
      			// TODO Auto-generated catch block
      			e.printStackTrace();
      		} catch (IOException e) {
      			// TODO Auto-generated catch block
      			e.printStackTrace();
      		}
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              allen.atamer Allen Atamer

              Dates

              • Created:
                Updated:
                Resolved:

                Issue deployment