File transfer from Client to server
hi,,
I've been trying to make an application where the user select several fiels using JFileChooser and then the program read all the files and then save the content of all in 1 file and then send it to the server using client/server sockets, I've tried many ways and non seems to working .So whats the proper way to do it ?
Code:
public class Main {
static File[] f;
public static void main(String[] args) throws Exception {
JFileChooser jf = new JFileChooser("C:");
jf.setMultiSelectionEnabled(true);
jf.showOpenDialog(null);
int count=0;
f = jf.getSelectedFiles();
File allinone = new File("All.dat");
JFileChooser jf2 = new JFileChooser(allinone);
int returnVal = jf2.showSaveDialog(null);
DataOutputStream dos = new DataOutputStream(new FileOutputStream(allinone));
FileInputStream fis=null;
for (int i = 0; i < f.length; i++) {
fis = new FileInputStream(f[i]);
int fileSize = (int) f[i].length();
byte[] b = new byte[fileSize];
fis.read(b);
String text=new String(b);
String filename = f[i].getName();
System.out.println("FIle name " + filename + " size = " + fileSize);
dos.writeUTF(filename);
dos.writeUTF(text);
dos.flush();
fis.close();
count= count+2;
}
dos.writeUTF("TheEnd");
dos.close();
// Thats the part which on client side,then i want to send the file allinone.dat to the server to do the following
DataInputStream dis = new DataInputStream(new FileInputStream(allinone)); //the part where i want to replace allinone with the incoming file from the client socket
while(count!=0){
String fn=dis.readUTF();
System.out.println(fn); // test
String text=dis.readUTF();
System.out.println(text); // test
count=count-2;
byte[] textB=text.getBytes();
File f=new File("new"+fn);
FileOutputStream ff=new FileOutputStream(f);
DataOutputStream dd=new DataOutputStream(ff);
dd.write(textB);
}
dis.close();
}
}