如何将数据写入图像(通过接收的数据创建图像)。
(按插座)
我的客户端从系统中读取图像并将其发送到服务器,服务器应该接收并将其保存在我的计算机中的任何其他位置。
这是我的代码:
客户端:
public class Client_Image_Transfer {
Socket clientSocket = null;
OutputStream outputStream = null;
DataOutputStream dataOutputStream = null;
InputStream inputStream = null;
public Client_Image_Transfer() {
try {
clientSocket = new Socket("localhost", 6002);
outputStream = clientSocket.getOutputStream();
dataOutputStream = new DataOutputStream(outputStream);
inputStream = clientSocket.getInputStream();
BufferedReader br = new BufferedReader(new FileReader("HelloNewPic.jpg"));
outputStream.write(br.read());
System.out.println("Client: Image sent to server");
dataOutputStream.close();
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
服务器:
public class Server_Image_Transfer {
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
public Server_Image_Transfer() {
try {
serverSocket = new ServerSocket(6002);
System.out.println("Server is Waiting for request...");
socket = serverSocket.accept();
System.out.println("Connected with: " + socket.getInetAddress());
dataInputStream = new DataInputStream(socket.getInputStream());
System.out.println("Server received image from client");
// How write ?
} catch (Exception e) {
e.printStackTrace();
}
}
我的服务器正确收到了图片,但现在如何在没有ImageIO
的情况下写图片?
这也不起作用:
File outPutFile = new File("C:\\Users\\khoy\\Desktop\\a2.jpg");
FileOutputStream fos = null;
fos = new FileOutputStream(outPutFile,true);
fos.write(socket.getInputStream()); // Compile Error
答案 0 :(得分:2)
此代码存在一些问题:
FileReader
。这适用于字符(“文本”)文件。 JPG图像是二进制文件。您应该为这样的文件使用流。outputStream.write(bufferedReader.read());
之类的内容只能读取BufferedReader
中的单个字节,并将此单个字节写入OutputStream
。为了读取文件的全部内容,您必须使用循环,并且应该一次读取多个字节以实现可接受的性能InputStream
和OutputStream
的使用似乎一般都搞砸了。这可能是问题的原因...... 但是,服务器和客户端的模式是相同的。在这两种情况下,您都必须阅读InputStream
并写入OutputStream
:
FileInputStream
并写入套接字的OutputStream
InputStream
并写入FileOutputStream
。考虑到这一点,并使用一些方便的方法,你可以大致应用这种模式(从我的头部写,所以可能没有错误,但希望显示这个想法):
private static void clientWrite(Socket socket)
{
OutputStream out = null;
InputStream in = null;
try
{
in = new FileInputStream("HelloNewPic.jpg");
out = socket.getOutputStream();
pipe(in, out);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
close(in);
close(out);
}
}
private static void serverRead(Socket socket)
{
OutputStream out = null;
InputStream in = null;
try
{
in = socket.getInputStream();
out = new FileOutputStream("HelloNewPic_Server.jpg");
pipe(in, out);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
close(in);
close(out);
}
}
/**
* Close the given Closeable if it is not <code>null</code>,
* printing an error message if an IOException is caused
*
* @param c The closeable
*/
private static void close(Closeable c)
{
if (c != null)
{
try
{
c.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
* Fully reads the given input stream and writes the result to the
* given output stream. The caller is responsible for closing the
* given streams.
*
* @param in The input stream
* @param out The output stream
* @throws IOException If an IO error occurs
*/
private static void pipe(InputStream in, OutputStream out) throws IOException
{
byte buffer[] = new byte[8192];
while (true)
{
int read = in.read(buffer);
if (read < 0)
{
break;
}
out.write(buffer, 0, read);
}
}
答案 1 :(得分:1)
只需创建一个FileOutputStream并从输入流中复制数据。有很多方法,但使用appache commons库是最简单的方法。