I want to send more than one image file from client to server for this I write the code in my application, but it will send only one image.
In client application one frame is there and in server application also there is a frame to start/stop the server.
One more problem is there when Client application send the image file then this image file shown on server computer but when I try to open this image file then nothing is there but when I close server application(server frame) then I am able to see the image.
code:
client site:
public void sendPhotoToServer(String str){ // str is image location
try {
InputStream input = new FileInputStream(str);
byte[] buffer=new byte[1024];
int readData;
while((readData=input.read(buffer))!=-1){
dos.write(buffer,0,readData); // dos is DataOutputStream
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
In server side this code is running into thread:
public void run() {
while (true) {
try {
byte[] buffer = new byte[8192];
fis = new FileOutputStream("C:\\"+(s1++)+".jpg"); // fis is FileOutputStream
while ((count = in.read(buffer)) > 0){ //count is a integer and 'in' is InputStream
fis.write(buffer, 0, count);
fis.flush();
}
} catch (Exception e) {}
}
}
Problem:
- only 1st image is copying which is send by the client.
- I am able to see this Image only when I close the server application.
no exception is there and i call sendPhotoToServer method in other class consecutively to send all the image file as:
if (photoSourcePath != null) {
clientClass.sendPhotoToServer(photoSourcePath+"\\"+rowData.get(5));
}
Your server side should stop the thread when its job is done. The
whileloop just keeps running forever and keeps the stream open (that’s why you see the image when you shut down the server, the threads only stops then).Try changing the server side to this: