英文:
How can I send an image from java socket Server to a navigator wihtout being corrupted?
问题
我正在尝试编写一个Java套接字服务器,从该服务器将图像发送到导航器(客户端)。但是,当我发送图像时,文件似乎损坏(显示带有符号的图像)。
DataOutputStream dout = new DataOutputStream(connectS.getOutputStream());
String file = "image.jpg";
File filename = new File(file);
FileInputStream fin = new FileInputStream(filename);
// dout.writeUTF(file);
System.out.println("正在发送图像...");
int size = (int) filename.length();
byte[] readData = new byte[size];
fin.read(readData);
dout.writeInt(size);
dout.flush();
dout.write(readData, 0, size);
/*for(int i=0;i<readData.length;i++) {
dout.write(readData, 0, i);
}*/
dout.flush();
这是我启动程序的方式:
ServerSocket ss = new ServerSocket(8080);
Socket connectS = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(connectS.getInputStream()));
感谢您的帮助!
英文:
I am trying to code a java socket server to send an image from this server to the navigator(client side). But when I send the image the file seen to be corrupted (display the image with symbols).
DataOutputStream dout = new DataOutputStream(connectS.getOutputStream());
String file="image.jpg";
File filename = new File(file);
FileInputStream fin = new FileInputStream(filename);
//dout.writeUTF(file);
System.out.println("Sending image...");
int size=(int) filename.length();
byte[] readData = new byte[size];
fin.read(readData);
dout.writeInt(size);
dout.flush();
dout.write(readData,0,size);
/*for(int i=0;i<readData.length;i++) {
dout.write(readData, 0, i);
}*/
dout.flush();
Here is how I start the program
ServerSocket ss=new ServerSocket(8080);
Socket connectS=ss.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(connectS.getInputStream()));
Thanks for your help!!!
答案1
得分: -1
我解决了这个问题。
首先,删除dout.readInt();
然后,打开Microsoft Edge;(Mozilla显示图像,但似乎存在编码问题,这是我的观点,可能不是真正的原因);
英文:
I solved the problem.
First, remove dout.readInt();
Then, open microsoft edge;(mozilla shows the image but there seems to be an encoding issue,that's my opinion,maybe not the true);
专注分享java语言的经验与见解,让所有开发者获益!
评论