Please help me I want to make a GUI application which upload a file from client to server. when I click on browse button then file is copy in bytes form because we travel data in bytes on network but when I click on upload button then file cannot upload.
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class ClientUpload extends JFrame implements ActionListener {
JFileChooser fc;
JButton b, b1;
JTextField tf;
FileInputStream in;
Socket s;
DataOutputStream dout;
DataInputStream din;
int i;
ClientUpload() {
super("client");
tf = new JTextField();
tf.setBounds(20, 50, 190, 30);
add(tf);
b = new JButton("Browse");
b.setBounds(250, 50, 80, 30);
add(b);
b.addActionListener(this);
b1 = new JButton("Upload");
b1.setBounds(250, 100, 80, 30);
add(b1);
b1.addActionListener(this);
fc = new JFileChooser();
setLayout(null);
setSize(400, 300);
setVisible(true);
try {
s = new Socket("localhost", 10);
dout = new DataOutputStream(s.getOutputStream());
din = new DataInputStream(s.getInputStream());
send();
} catch (Exception e) {
}
}
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == b) {
int x = fc.showOpenDialog(null);
if (x == JFileChooser.APPROVE_OPTION) {
copy();
}
}
if (e.getSource() == b1) {
send();
}
} catch (Exception ex) {
}
}
public void copy() throws IOException {
File f1 = fc.getSelectedFile();
tf.setText(f1.getAbsolutePath());
in = new FileInputStream(f1.getAbsolutePath());
while ((i = in.read()) != -1) {
System.out.print(i);
}
}
public void send() throws IOException {
dout.write(i);
dout.flush();
}
public static void main(String... d) {
new ClientUpload();
}
}
You are basically reading the contents of the file when you select it, discard the contents and then trying to send the last int
value you read from it to the server. A file my be many, many, many bytes long (think giga bytes), an int
ins't going to hold that much information (poor int
).
Instead, when the user selects a File
, you should maintain a reference to it until you actually need it...
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) {
}
}
When the user presses the "send" button, you would simply copy the file input stream to the sockets output stream...
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) {
}
}
That should at least get you one step closer.
The next problem you are likely to hit is the fact that the UI will stop responding while the transfer is underway.
In this case, you should take a look at Concurrency in Swing.
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.
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.
Take a look at Layout out Components within a Container for more deatils
You may also want to take a closer look at Basic I/O and All about sockets for more details...