Page 1 of 1

Java FTP writing

Posted: Thu Jan 11, 2007 12:39 pm
by shiznatix
I am trying to write data to a FTP server using java. I am connected and everything but when I do the uploading, it only uploads some of the file. I am trying to upload like a 3 meg file but some files it will only upload like 83 bytes of it and other will upload more but not all of it unless its like a small html file or something. It's weird. Here is the relivant part of my code:

Code: Select all

    File file = new File(s);
    if(dataType.equals("I"))
    {
            BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(file));
            BufferedOutputStream bufferedoutputstream = dataSocket.getOutputStream();
            
            byte byte0;
            boolean write = true;
            
            while (write)
            {
            	byte0 = (byte)bufferedinputstream.read();
            	bufferedoutputstream.write(byte0);
            	
            	if (byte0 == -1)
            	{
            		break;
            	}
            }            	

            bufferedinputstream.close();
            bufferedoutputstream.close();
    }
I am sure it is because byte0 is giving a -1 prematuraly but I dont even know why I am using -1 there, it was just in some other code I was looking over. How can I make sure it write the entire file to the ftp before breaking?

Posted: Thu Jan 11, 2007 1:35 pm
by Jenk

Code: Select all

            while ((byte0 = (byte)bufferedinputstream.read()) != null) 
            { 
                bufferedoutputstream.write(byte0); 
            }  
untested.

Posted: Thu Jan 11, 2007 2:18 pm
by shiznatix
that did not work either as it gave an error about the 'null' that did not make sence. I changed it to -1 instead and it did the same as it did before. But I did get this to work:

Code: Select all

byte[] buffer = new byte[4096];
int bytes_read;
            
while((bytes_read = bufferedinputstream.read(buffer)) != -1)
{
        bufferedoutputstream.write(buffer, 0, bytes_read);
}
stole it from someone elses writing to a file code. Supra.

Posted: Thu Jan 11, 2007 3:46 pm
by Chris Corbyn
How about simply:

Code: Select all

while ((byte0 = bufferedinputstream.read()) != -1)
            {
                bufferedoutputstream.write(byte0);
            }
If you read the documentation for the BufferedStreams you'll see that you should be using int, not byte. In Java, the methods are identified by there overall signatures (method name + parameters + parameter types). Just because you can pass byte as the first parameter when you provide other parameters, doesn't mean you always pass byte ;) When passing no other argument, use int. Check the return value *before* writing to the file too (i.e. in the while condition).

Posted: Thu Jan 11, 2007 6:33 pm
by shiznatix
ahha see im learning! thanks kids

Posted: Sun Apr 22, 2007 10:22 pm
by timarcher52
Hello,
I did a writeup on how to use Jakarta Commons Net to perform FTP operations at the following URL:
http://timarcher.com/?q=node/56

It has a pretty good example on sending and retrieving files, deleting files, changing directories, etc.

Tim