[英]How to send multiple Images from client to server through java sockets?
更新
我正在嘗試從客戶端向服務器發送多個圖像。 客戶端有兩個線程
線程1正在截屏
線程2正在向服務器發送屏幕截圖
這段代碼正在拍2張截圖。 但只有第一個屏幕截圖成功保存在服務器上。 請幫我發送並保存多個圖像到服務器。
沒有錯誤,沒有例外。
輸出:
thread1正在運行...
thread2正在運行...
thread1正在運行...
thread2正在運行...
客戶
abstract class ScreenCapture implements Runnable{
static BufferedImage screencapture;
static ByteArrayOutputStream baos;
static byte[] ImageInBytes;
public static void main(String args[]) throws
AWTException, IOException, InterruptedException {
// Open your connection to a server, at port 1234
final Socket ClientSocket = new Socket("localhost",1234);
final DataOutputStream dos= new DataOutputStream(ClientSocket.getOutputStream());
DataInputStream in = new DataInputStream(ClientSocket.getInputStream() );
baos = new ByteArrayOutputStream();
try{
//First thread that is Taking screenshot
Thread TakeScreenShotthread = new Thread ()
{
public void run () {
// Capture Screen using BufferedImage Library
try {
screencapture = new Robot().createScreenCapture(
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
System.out.println("thread1 is running...");
} catch (HeadlessException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
}
}
};
//Thread 2 that is Sending Screenshot to server
Thread sendingScreenShotThread =new Thread () {
public void run () {
//Sending Screen to Server
try {
ImageIO.write(screencapture, "jpg", baos);
ImageInBytes = baos.toByteArray();
dos.write(ImageInBytes);
// File Rif = new File(System.currentTimeMillis() + ".jpg");
//ImageIO.write(screencapture, "jpg", Rif);
System.out.println("thread2 is running...");
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
baos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
TakeScreenShotthread.start();
TakeScreenShotthread.sleep(1000);
sendingScreenShotThread.start();
sendingScreenShotThread.sleep(1000);
TakeScreenShotthread.run();
sendingScreenShotThread.run();
}finally
{
//Closing Clients
in.close();
baos.close();
ClientSocket.close();
}
}
}
服務器
public class ServerConnection
{
public static void main(String args[]) throws IOException
{
ServerSocket serversock = new ServerSocket(1234);
Socket clientSocket = null;
BufferedReader BF_RecievingGUID;
clientSocket = serversock.accept();
InputStream in=clientSocket.getInputStream();
OutputStream out = null;
try{
boolean processing=true;
while(processing)
{
try {
byte[] buffer = new byte[1024];
out = new BufferedOutputStream(new FileOutputStream(path));
while ((in.read(buffer)) >= 0) {
out.write(buffer);
}
System.out.println("Image file written successfully");
} catch (Exception e) {
}finally {
processing=false;
if (out != null) out.close();
}
}
}
finally{
BF_RecievingGUID.close();
out.close();
clientSocket.close();
serversock.close();
}
}
}
它只會讀取一次,因為你打破了while循環:
if(RecievedImage != null)
{
ImageIO.write(RecievedImage, "jpg", RecievedImageFile);
RecievedImage.flush();
System.out.println("Image file written successfully");
}else{
System.out.println("image is empty");
}break; //you break here
我不明白為什么你這樣做:
new Thread(new ThreadHandler(clientSocket)).start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.