我想使用TCP协议将图像(jpg)从一台PC传输到另一台PC。我写了服务器和客户端代码。我的输出文件大小与源图像相同。但是,Windows imageviewer无法打开它。我找不到问题。你可以帮帮我吗?谢谢。
客户端代码
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
public class mytcpiclient
{
public static void main(String args[])
{
try {
Socket socket = new Socket("127.0.0.1", 6000);
DataOutputStream out = new DataOutputStream(
socket.getOutputStream());
//out.writeChar('I'); // as image,
DataInputStream dis = new DataInputStream(new FileInputStream("D:\\image\\images1.jpg"));
ByteArrayOutputStream ao = new ByteArrayOutputStream();
int read = 0;
byte[] buf = new byte[1024];
while ((read = dis.read(buf)) > -1) {
ao.write(buf, 0, read);
}
out.writeLong(ao.size());
out.write(ao.toByteArray());
out.flush();
out.close();
dis.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器代码
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class mytcpserverimage
{
public static void main(String args[])
{
try
{
ServerSocket serverSocket = new ServerSocket(6000);
System.out.println("Server started. Listening to the port 6000");
Socket clientSocket = serverSocket.accept();
byte[] mybytearray = new byte[10000000];
InputStream inputStream = clientSocket.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("E:\\bbb.jpg");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
System.out.println("Receiving...");
int bytesRead = inputStream.read(mybytearray, 0, mybytearray.length);
int current = bytesRead;
do {
bytesRead = inputStream.read(mybytearray, current, (mybytearray.length - current));
if (bytesRead >= 0) {
current += bytesRead;
}
} while (bytesRead > -1);
bufferedOutputStream.write(mybytearray, 0, current);
bufferedOutputStream.flush();
bufferedOutputStream.close();
inputStream.close();
clientSocket.close();
serverSocket.close();
System.out.println("Sever recieved the file");
}
catch(Exception e)
{
System.out.print("Whoops! It didn't work!\n"+e);
}
}
}
解决
客户端
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
public class myclienttcpimage
{
public static void main(String args[])
{
try
{
Socket socket = new Socket("127.0.0.1", 6000);
DataOutputStream out = new DataOutputStream(
socket.getOutputStream());
//out.writeChar('I'); // as image,
DataInputStream dis = new DataInputStream(new FileInputStream("d:\\g.jpg"));
ByteArrayOutputStream ao = new ByteArrayOutputStream();
int read = 0;
byte[] buf = new byte[1024];
while ((read = dis.read(buf)) > -1) {
ao.write(buf, 0, read);
}
out.writeLong(ao.size());
out.write(ao.toByteArray());
out.flush();
out.close();
dis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
服务器
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class mytcpserverimage
{
public static void main(String args[])
{
try
{
ServerSocket serverSocket = new ServerSocket(6000);
System.out.println("Server started. Listening to the port 10898");
Socket clientSocket = serverSocket.accept();
//byte[] mybytearray = new byte[10000000];
DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
//char c = dis.readChar();
//if (c == 'I')
{
long length = dis.readLong();
DataOutputStream dos = new DataOutputStream(
new FileOutputStream("e:\\aaa.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = dis.read(buffer)) != -1)
{
dos.write(buffer, 0, len);
}
dis.close();
dos.close();
}
System.out.println("Sever recieved the file");
}
catch(Exception e)
{
System.out.print("Whoops! It didn't work!\n"+e);
}
}
}