Skip to Main Content

Java APIs

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!

rmi file transfer

843793May 2 2007 — edited May 2 2007
hi,
i tried http://java.sun.com/developer/technicalArticles/RMI/rmi_corba/ and used this tutorial to implement file transfer in my project of a client server architecture
i wished to complete my project on urgent basis but was not able to.

the problem i am having is when i try sending file its says

java.io.FileNotFoundException: (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method)

The full stack trace is
FileImpl: ss.m3u (The system cannot find the file specified)
java.io.FileNotFoundException: ss.m3u (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.io.FileInputStream.<init>(FileInputStream.java:66)
        at chat.fileTransferSender.downloadFile(fileTransferSender.java:114)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
        at sun.rmi.transport.Transport$1.run(Transport.java:159)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
        at java.lang.Thread.run(Thread.java:619)
FileServer exception: null
java.lang.NullPointerException
        at chat.fileTransferReceiver.<init>(fileTransferReceiver.java:130)
        at chat.privateChat$IncomingReaderPrivate.run(privateChat.java:369)
        at java.lang.Thread.run(Thread.java:619)
The code i am using is

//opening sending file transfer window with parameters
// filename = sjs.txt {string}
//filesize = 324 {long}
//username1 & 2 are strings sender and receiver
fileTransferSender ft = new fileTransferSender(fileName,fileSize,username1,username2);
"filetransfersender.java"
public class fileTransferSender extends javax.swing.JFrame implements fileTransferIntf, Remote {
    public static int FILEPORT = 12346 ;
public byte[] downloadFile(String fileName) throws RemoteException {
        try {
         File file = new File(fileName);
         byte buffer[] = new byte[(int)file.length()];
         BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileName));
         input.read(buffer,0,buffer.length);
         input.close();
         return(buffer);
      } catch(Exception e){
         System.out.println("FileImpl: "+e.getMessage());
         e.printStackTrace();
         return(null);
      }
    }
  
    //opening sender side filetransfer window
    public fileTransferSender(String fileName,long fileSize,String username1,String username2) throws RemoteException {
        this.initComponents();
        this.setVisible(true);
        this.setTitle("File Transfer");
        jLabel2.setText(username1);
        jLabel3.setText(username2);
        jLabel5.setText("Sending file to: "+username2+ "\n" + " File Name: " + fileName + ", File Size: " + fileSize );
        String fileServer = "fileServer";
        if(System.getSecurityManager() == null) {
         System.setSecurityManager(new RMISecurityManager());
        }
           try {
                fileTransferIntf fileTransferImpl = this;
                fileTransferIntf stub = (fileTransferIntf) UnicastRemoteObject.exportObject(fileTransferImpl,FILEPORT);
                Registry registry = LocateRegistry.createRegistry(FILEPORT);  
                registry.rebind(fileServer,stub);
                System.out.println("done");
                }
            catch(Exception e) {
                System.out.println("Exception: " + e);
                }
    }
"filetransferreceiver.java"
public class fileTransferReceiver extends javax.swing.JFrame{
    public static int FILEPORT = 12346 ;
//opening sender side filetransfer window
    public fileTransferReceiver(String receiverIP, String fileName,String fileSize,String username1,String username2) throws RemoteException {
        this.initComponents();
        this.setVisible(true);
        this.setTitle("File Transfer");
        jLabel2.setText(username1);
        jLabel3.setText(username2);
        //int indx = fileName.lastIndexOf("\\");
        //fileName = fileName.substring(indx+1);
        jLabel5.setText("Receiving file from: "+username2+ "\n" + " File Name: " + fileName + ", File Size: " + fileSize );
                
        try {
                 String fileServer = "fileServer";
                 Registry registry = LocateRegistry.getRegistry(receiverIP,FILEPORT);
                 fileTransferIntf fileTransferImpl = (fileTransferIntf)registry.lookup(fileServer);
                 byte[] filedata = fileTransferImpl.downloadFile(fileName);
                 File file = new File(fileName);
                 BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file.getName()));
                 output.write(filedata,0,filedata.length);
                 output.flush();
                 output.close();
        } catch(Exception e){
            System.err.println("FileServer exception: "+ e.getMessage());
         e.printStackTrace();
        }
""filetransferintf.java"
public interface fileTransferIntf extends Remote {
   public byte[] downloadFile(String fileName) throws RemoteException;    
}
some code on other side when receives a call to receive file is like
String mes = "User " + username2 + " wants to send file, File Name: " + fileName + ", File Size: "+fileSize;
                            int value = JOptionPane.showConfirmDialog(f,mes,"File Transfer",0,3);    
                            if(value == 0)
                            {
                                File file = new File(fileName);
                                JFileChooser fc = new JFileChooser();
                                fc.setSelectedFile(file);
                                //file.delete();
                                int temp = fc.showSaveDialog(f);
                                
                                if(temp == 0)
                                {
                                    //file = fc.getSelectedFile();      
                                    
                                    //opening file transfer window on receiver side
                                    fileTransferReceiver ft = new fileTransferReceiver(localIP,fileName,fileSize,username1,username2);                                   
                                }                  
will be glad if you people can help me run this code correctly and make the file save on receiver end if user specified place on his /her pc
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 30 2007
Added on May 2 2007
6 comments
283 views