Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java socket file transfer problem

807580Mar 8 2009 — edited Mar 12 2010
i have problem in transferring data through java socket
the funny thing is able to transfer from server to client but not from client to server with same code
here is the code that i transfer from client to server:

server download file code
public void downloadFile(){

         try{
            String fName =  serverIn.readUTF();
            String username = serverIn.readUTF();
            //int bytesRead;

            fName = getFileName(fName);
            System.out.println(fName);
            String fileFolder = System.getProperty("user.dir")+"\\ServerFTPFolder\\"+username+"\\"+fName;

            File f = new File(fileFolder);

            FileOutputStream fout=new FileOutputStream(f);
            BufferedOutputStream bos = new BufferedOutputStream(fout);
            int len;

            while ((len = serverIn.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
                System.out.println("#" + len);
            }
            System.out.println("lennnnn"+len);
           
            System.out.println("file finish. sending object now...");
			this.sendObject(username);
			System.out.println("File Send Successfully");
             serverIn.close();
            bos.flush();
            bos.close();
            servSocket.close();
            clientSocket.close();
         }catch(Exception ex){
            System.out.println(ex);
            ex.printStackTrace();
         }

     }
upload code in client:
public void uploadFile(String fName){
         try{

                clientSocket = new Socket(ccon.ip, 6790);
                System.out.println("connecting to server");
                clientOut = new ObjectOutputStream(clientSocket.getOutputStream());
                clientIn =new ObjectInputStream (clientSocket.getInputStream());
                clientOut.flush();


            long start = System.currentTimeMillis();
            File f = new File(fName);
            
            clientOut.writeUTF(fName.toString());
            clientOut.writeUTF(ccon.userMatrix);
            clientOut.flush();

            statusLbl.setText("Status: Please wait! Transferring file...");
            statusLbl.setForeground(Color.RED);
            if(f.exists()){
                FileInputStream fin=new FileInputStream(f);
                BufferedInputStream bis = new BufferedInputStream(fin);
                int len = 0;
                while ((len = bis.read(buffer)) != -1) {
                    clientOut.write(buffer, 0, len);
                    clientOut.flush();
                    System.out.println("#" + len);
                }
                System.out.println("lennnn" + len);


             
                bis.close();
                long end = System.currentTimeMillis();
                System.out.println(end-start);

                FileTreeModel newObject = null;
                Object ftm = new Object();
                ftm = clientIn.readObject();
                System.out.println(ftm);
                newObject = (FileTreeModel) ftm;
                if(newObject!=null){
                    setTreeModel(newObject);
                }
            }
            else{
                System.out.println("file not exist!");
            }
            statusLbl.setText("Status: connected, ready for transferring");
            statusLbl.setForeground(Color.BLUE);
            clientSocket.close();
         }catch(Exception ex){
             ex.printStackTrace();
         }

     }
any help????
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 9 2010
Added on Mar 8 2009
13 comments
778 views