Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are basically reading the contents of the file when you select it, discard the contents and then trying to send the last <code>int</code> value you read from it to the server. A file my be many, many, many bytes long (think giga bytes), an <code>int</code> ins't going to hold that much information (poor <code>int</code>).</p> <p>Instead, when the user selects a <code>File</code>, you should maintain a reference to it until you actually need it...</p> <pre><code>public void actionPerformed(ActionEvent e) { try { if (e.getSource() == b) { int x = fc.showOpenDialog(null); if (x == JFileChooser.APPROVE_OPTION) { fileToBeSent = fc.getSelectedFile(); tf.setText(f1.getAbsolutePath()); b1.setEnabled(true); } else { fileToBeSent = null; tf.setText(null;); b1.setEnabled(false); } } if (e.getSource() == b1) { send(); } } catch (Exception ex) { } } </code></pre> <p>When the user presses the "send" button, you would simply copy the file input stream to the sockets output stream...</p> <pre><code>FileInputStream in = null; try { in = new FileInputStream(fileToBeSent); byte[] buffer = new byte[1024]; int bytesIn = -1; while ((bytesIn = in.read(buffer)) != -1) { dout.write(buffer, 0, bytesIn); } dout.flush(); } finally { try { in.close(); } catch (Exception exp) { } } </code></pre> <p>That should at least get you one step closer.</p> <p>The next problem you are likely to hit is the fact that the UI will stop responding while the transfer is underway.</p> <p>In this case, you should take a look at <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/" rel="nofollow">Concurrency in Swing</a>.</p> <p>Modern UI's need to deal with many different characteristics of client machines, included different fonts, font rendering capabilities, DPI, screen resolutions and lots more. You're putting your self in a very dark and deep hole by using absolute layouts.</p> <p>You should take advantage of the layout manager API which is designed to easy the pressure of these concerns and make you UI more accommodating on different operating environments.</p> <p>Take a look at <a href="http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html" rel="nofollow">Layout out Components within a Container</a> for more deatils</p> <p>You may also want to take a closer look at <a href="http://docs.oracle.com/javase/tutorial/essential/io/" rel="nofollow">Basic I/O</a> and <a href="http://docs.oracle.com/javase/tutorial/networking/sockets/" rel="nofollow">All about sockets</a> for more details...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload