Java FTP writing

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Java FTP writing

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

            while ((byte0 = (byte)bufferedinputstream.read()) != null) 
            { 
                bufferedoutputstream.write(byte0); 
            }  
untested.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ahha see im learning! thanks kids
timarcher52
Forum Newbie
Posts: 1
Joined: Sun Apr 22, 2007 10:20 pm

Post 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
Post Reply