what is the best way to send a file?

i am writing a program and i want to transfer a file from a client class to a server class... what is the best way to do that?

convert the file to bytes using the following

File file=new File("jobs.xml");

byte buffer[]=newbyte[(int)file.length()];

try{

BufferedInputStream input=new BufferedInputStream(new FileInputStream("jobs.xml"));

input.read(buffer,0,buffer.length);

input.close();

}catch(Exception e){//DIORTHOSE TA MSGS

System.out.println("reading jobs.xml->buffer: "+e.getMessage());

e.printStackTrace();

}

firstServerRef.translationService(theCallbackObjectRef, buffer);

for a reason i dont like that i am reading the file again to put it in the buffer and send the buffer... are my worries reasonable or not? is there any other better way to do that?

[1371 byte] By [panosjava] at [2007-11-26 12:16:55]
# 1
You could use new FileInputStream(file) instead of new FileInputStream("jobs.xml")I belive this way you dont open the file twiceMessage was edited by: Leo77
Leo77 at 2007-7-7 14:53:42 > top of Java-index,Archived Forums,Socket Programming...
# 2
Use a smaller buffer, repeatedly read and write, and print the exception's stack trace.What do you mean by reading again, by the way? I only see you reading the file once.
CeciNEstPasUnProgrammeur at 2007-7-7 14:53:42 > top of Java-index,Archived Forums,Socket Programming...
# 3
> I belive this way you dont open the file twiceHe's could do without the File object altogether and just use the String. Note that a File object is only a representation of a file system path; a file name, nothing else. It's not associated with a file's data.
CeciNEstPasUnProgrammeur at 2007-7-7 14:53:42 > top of Java-index,Archived Forums,Socket Programming...
# 4

> Use a smaller buffer, repeatedly read and

> write, and print the exception's stack trace.

>

> What do you mean by reading again, by the way? I only

> see you reading the file once.

hmm you mean use a smaller buffer and call the function with the smaller buffer many times in a while?

the client and the server are not on a single machine and i want to call the function only once... could you clarify the thing that you said..

yes you are correct that you see only one reading because i haven't pasted the rest of the code which is sth like...

FileWriter fw = new FileWriter("jobs.xml");

ObjectOutputStream out = xstream.createObjectOutputStream(fw);

// out.writeObject(new Jobb("ougk2", "Walnes",null));

for(int i=0;i<nameOfServices.length;i++){

Jobb translationJob=new Jobb();

//find the service !

NameComponent nc = new NameComponent(nameOfServices[i], " ");

// Resolve the object reference in naming

NameComponent path[] = {nc};

//create a ref for the servant of the service

ServiceOperations theRemoteObjRef = ServiceHelper.narrow(ncRef.resolve(path));

// JobOperations theRemoteObjRef = JobHelper.narrow(ncRef.resolve(path));

translationJob.setObjServerRef(theRemoteObjRef.toString());

if(i==0){ //this is the first job

translationJob.setForTranslation(wordForTranslation);

firstServerRef=theRemoteObjRef;

}

out.writeObject(translationJob);

}

out.close();

File file=new File("jobs.xml");

byte buffer[]=new byte[(int)file.length()];

try {

BufferedInputStream input=new BufferedInputStream(new FileInputStream("jobs.xml"));

input.read(buffer,0,buffer.length);

input.close();

} catch(Exception e) {//DIORTHOSE TA MSGS

System.out.println("reading jobs.xml->buffer: "+e.getMessage());

e.printStackTrace();

}

firstServerRef.translationService(theCallbackObjectRef, buffer);

which i believe is bad....

panosjava at 2007-7-7 14:53:42 > top of Java-index,Archived Forums,Socket Programming...