Java FTP writing
Posted: Thu Jan 11, 2007 12:39 pm
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:
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?
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();
}