LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   java io write file (http://www.linuxquestions.org/questions/programming-9/java-io-write-file-293394/)

dummyagain 02-22-2005 09:45 AM

java io write file
 
I use a client to send file to server, however, i can send the file with equal size, equal filename and filesize. But when i opened the sent file in the server, the file is a blank one. I would like to ask how to solve that

client side
try{
s = new Socket(ServerIP, ServerPort);
File f = new File(sentFile.getName());
fis = new FileInputStream(f);
os = s.getOutputStream();
while (fis.available()>0){
os.write(fis.read());
}
fis.close();
os.close();
s.close();
}catch(IOException ioe){
System.out.println("Get a Error in socket");
}



server side
try{
ss = new ServerSocket(port);
}catch(IOException e){
System.out.println(e);
}

while (true){
try{
s = ss.accept();
fos = new FileOutputStream(new File(filename));
is = s.getInputStream();

for (int i=0; i<fileSize; i++)
fos.write(is.read());



thank you for your help

csfalcon 02-22-2005 10:05 AM

You need to close the FileOutputStream on the server side.

dummyagain 02-22-2005 10:11 AM

i had closed it but i haven't shown the code

Looking_Lost 02-23-2005 04:50 PM

If you haven't already sorted it out try on the client side like

Code:

        int data;

        while( (data=fis.read()) != -1){
                os.write(data);
                }

        fis.close();
        os.close();

and on the server side the reverse

        int data;

        while( (data=is.read()) != -1){
                fos.write(data);
              }

        fos.close();
        is.close();



All times are GMT -5. The time now is 07:58 AM.