This is kinda urgent. Please help! :(

MrGrim

Golden Member
Oct 20, 1999
1,653
0
0
I've got this Java project to hand in today and it's not done yet. It's an FTP program and I'm having problems with transfering files. Apparently something goes wrong right at the end of the file (not all buffers flushed?) and it doesn't finalise properly. The very first file will be transfered, but if I try another one after that it will fail. This is the code for the File Trasfer.

class NetIO {
static final int BUFFER_SIZE = 10000;
public static void fileSend (Socket uploadSocket, String localSource) {
try {
NetInput netInput = new NetInput(uploadSocket);
byte[] buffer = new byte[BUFFER_SIZE];
NetShareProtocol fileMap = new NetShareProtocol();
FileInputStream in = new FileInputStream(localSource);
int remaining = (int) (new File(localSource)).length();
System.out.println(Integer.toString(remaining));
fileMap.setGData("length", Integer.toString(remaining));
fileMap.setGData("buffers", "0");
long startTime = System.currentTimeMillis();
while(remaining > 0) {
if (remaining >= BUFFER_SIZE) {
remaining = remaining - BUFFER_SIZE;
}
else {
buffer = new byte[remaining];
remaining = 0;
}
in.read(buffer);
fileMap.setGData("buffers", Integer.toString(Integer.parseInt(fileMap.getGData("buffers"))+1));
fileMap.setGData(Integer.toString(Integer.parseInt(fileMap.getGData("buffers"))), String.valueOf(buffer));
}
netInput.put(fileMap.getHashtable());
System.out.println("Transfer took "+ (System.currentTimeMillis()-startTime)/1000.0+" seconds. Used " + fileMap.getGData("buffers") + " buffers.");
in.close();
}
catch (Exception err) {
System.err.println("Couldn't upload " + localSource + " to " + uploadSocket.getInetAddress() + " System returned: " + err.getMessage());
}
}
public static void fileReceive (Socket downloadSocket, String localDest) {
try {
NetOutput netOutput = new NetOutput(downloadSocket);
NetShareProtocol fileMap = new NetShareProtocol();
fileMap.setHashtable(netOutput.get());
FileOutputStream out = new FileOutputStream(localDest);
int remaining = Integer.parseInt(fileMap.getGData("length"));
System.out.println(Integer.toString(remaining));
long startTime = System.currentTimeMillis();
for (int i=1; i<Integer.parseInt(fileMap.getGData(&quot;buffers&quot;)); i++) {
out.write((fileMap.getGData(Integer.toString(i))).getBytes());
out.flush();
}
out.write(-1);
System.out.println(&quot;Transfer took &quot;+ (System.currentTimeMillis()-startTime)/1000.0+&quot; seconds. Used &quot; + fileMap.getGData(&quot;buffers&quot;) + &quot; buffers.&quot;);
out.close();
}
catch (Exception err) {
System.err.println(&quot;Couldn't download &quot; + localDest + &quot; from &quot; + downloadSocket.getInetAddress() + &quot; System returned: &quot; + err.getMessage());
}
}
}

I would really appreciate some help. Thanx in advance.