Java - Image transfer Server between and Client

Associate
Joined
7 Dec 2007
Posts
302
Location
Derbyshire, Tibshelf
Hi, I've managed to get most of my client and server program working in Java but the next part involves sending an image between the client and server...

I've googled around and so far I've come across a few examples such as...

// Server code
FileInputStream fis = new FileInputStream("images\\picture.jpg");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
ObjectOutputStream oos = new ObjectOutputStream(serverSocket.getOutputStream()) ;
oos.writeObject(buffer);

// Client code
ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
byte[] buffer = (byte[])ois.readObject();
FileOutputStream fos = new FileOutputStream("downloaded\\picture.jpg");
fos.write(buffer);

Edit: got the directory stuff working... c:/client and c:/server worked fine :/ sure it didn't the first time lol

And also once i've got it to transfer from client to server, the email parts were being stored in a Vector String[] soo "[email protected]", "subject", "body" I'm guessing once the file has been transfered to the server, you just have a directory of where it is on the server like "serverimages\\picture.jpg" inside the String[] and then send that across to the client?

Edit2: How can you check what kind of file it is? perhaps send the file extension across?

Thanks
 
Last edited:
Update: Managed to get it all working now, pretty much using the above and sending the files extension across...

the only problem I've got now seems to be txt files, they send to the server but sending them back moans about IOException: java.io.EOFException

Despite it randomly sometimes working :s

In fact its doing it for images as well now :/ and it seems to randomly work sending objects from server to client... perhaps I need some sort of delay as its processing something? I'm a little bit worried that it's not gonna work on the day of showing it to him and hes gonna assume it doesn't work :/
 
I'm opening these streams on the client:

BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");

checking what is inside them multiple times, doing the objectoutputstream and then closing the connection... should I be closing the connection after the first stream, reconnecting?

Having a quick google around seems to sya something about being good practise to close streams but having just tried it, made it say that the socket was closed... :s not quite sure where to close streams etc and I was expecting that it would close just the stream rather than the entire socket (assuming it did what it said :P)

It's just that I first send across what I want the server to do, such as RECEIVEFILE then the client sends across the ID of the file it wants, then opens the fileinputstream thingybob...

}else if (process.equals("RECEIVEFILE")){
osw.write(process+(char) 13);
osw.write(id_mail+(char) 13);
osw.flush();
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");
int c;

StringBuffer filetype = new StringBuffer();
while((c = isr.read()) != 13) {
filetype.append((char)c);
}

if (filetype.toString().equals("NOFILE")){
// No file has been sent
}else{
ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
byte[] buffer = (byte[])ois.readObject();
FileOutputStream fos = new FileOutputStream(newfile);
fos.write(buffer);
System.out.println("File received: "+newfile);
}
connection.close();
}

the server does soemthing like:

BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
InputStreamReader isr = new InputStreamReader(is);
int character;
String returnCode = "" + (char) 13;
StringBuffer process = new StringBuffer();
while((character = isr.read()) != 13) {
process.append((char)character);
}

// some other code

}else if (process.toString().equals("RECEIVEFILE")){

StringBuffer id_mail = new StringBuffer();
while((character = isr.read()) != 13) {
id_mail.append((char)character);
}
System.out.println(process+" | mail ID: ("+id_mail+")");

String file = emails.getFile(id_mail.toString());

if (file.equals("NOFILE")){
// no file
returnCode = "NOFILE" + (char) 13;
BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
osw.write(returnCode);
osw.flush();
}else{
String ext = file.substring(file.lastIndexOf('.')+1, file.length());
returnCode = ext + (char) 13;
BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
osw.write(returnCode);
osw.flush();

System.out.println("Sending File attachment: "+file);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream()) ;
oos.writeObject(buffer);
oos.flush();
}

}

Hopefully the formatting doesn't mess up :S


I was having this problem before when I waas sending objects from the server to the client :( but haven't a clue why
 
Last edited:
fixed one problem, the IOException EOF thingybob, was sending a file that didn't exist...

problem is now though, if you try to send multiple emails at once, it doesn't quite work, ends up sending a random amount :/ meh whatever lol
 
Back
Top Bottom