Does anybody know exactly how to send files over a network? My code so far is
Code:private void SendFile() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = fileChooser.showOpenDialog(this); if (result == JFileChooser.CANCEL_OPTION) return; File fileName = fileChooser.getSelectedFile(); if (fileName == null || fileName.getName().equals("")) JOptionPane.showMessageDialog(this, "Unknown file name", "Unknown file name", JOptionPane.ERROR_MESSAGE); else { try { //Try to open. FileOutputStream fos = new FileOutputStream(fileName); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(fileName); oos.flush(); oos.close(); } catch (IOException ioException) { JOptionPane.showMessageDialog(this,"Error Opening File to send.","Error",JOptionPane.ERROR_MESSAGE); ioException.printStackTrace(); } } //End else. }But nothing is happening what am I doing wrong?Code://Inner Class for accepting connections //for file transfer class RecieveFilethread extends Thread{ public void run(){ while (true) { try { //DatagramSocket fileSocket = new DatagramSocket(8008); File newFile; FileInputStream fis = new FileInputStream("temp.obj"); ObjectInputStream ois = new ObjectInputStream(fis); newFile = (File) ois.readObject(); ois.close(); String name = newFile.getName(); JOptionPane.showConfirmDialog(null, name); } catch (IOException ioEx) {} catch(ClassNotFoundException cnfe){} } } }


Reply With Quote


Bookmarks