java socket 图片传输
如题所示,我想用java,通过socket实现客服端像服务器端循环的传输图片,在服务器端能够正确的获取每张图片。现在我已经实现了单张传输,也可以正常的在服务器端接收了。但是当我在客服端循环不停的发送时,服务器端就没法正常的接收,图片一直只有一张,在不停的增大,相当于全部累计在一个图片文件上面去了。该怎么正确的把客服端发送的图片分割开来,存为不同的图片呢?
我在网上查了很多相关的资料,说自定义分隔符,在服务器端根据分隔符区分;或者先发送个图片的大小再发送图片文件,在服务器端通过大小来区分。但是我在服务器端怎么才能区分获取得到的就是分隔符,或者是图片的大小呢?客服端在不停的循环发送,服务器端在循环的接收。
希望做过这方面编程的人提供帮助。最好能够有代码,因为原理我已经查了很多很多了。谢谢!
[最优解释]
package net;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class DataSocketServer {
final public static int DEFAULT_PORT = 4848;
final public static String FILE_DIR = "D:/";
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(DEFAULT_PORT);
while (true) {
new Thread(new RequestProcessorTask(server.accept())).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class RequestProcessorTask implements Runnable {
private Socket socket = null;
public RequestProcessorTask(Socket socket) {
this.socket = socket;
}
public void run() {
try {
boolean isEnd = false;
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
while (!isEnd) {
int d = -1;
StringBuilder header = new StringBuilder();
while ((d = in.read()) != '\r') {
if (d == -1) {
isEnd = true;
break;
}
header.append((char) d);
}
if (!isEnd) {
String[] parms = header.toString().split(";");
FileOutputStream out = new FileOutputStream(FILE_DIR + parms[0]);
long size = Long.parseLong(parms[1]);
while (size > 0 && (d = in.read()) != -1) {
out.write(d);
size--;
}
out.flush();
out.close();
}
}
in.close();
} catch (IOException e) {
throw new RuntimeException("获取客户端输入流失败", e);
}
}
}
}
2.开始传输:
c->s发送数据,循环;
c->s发送传输完成命令,s确认,s端 close FileOutputStream。
3.循环整个流程。
应该定义一些命令号,防止传输中出现问题。比如:
1001:准备传输(包含文件名)
1002:确认准备传输
1003:文件块信息
1004:确认文件块信息
... ...
[其他解释]
自己定义个格式,比如说前2个byte为文件名长度,然后读取对应长度作为文件名,接下来定义8个byte的为文件内容的值,然后读取对应长度的byte写入文件。那接下来就是洗一张图片了
[其他解释]
OutputStream os = socket.getOutputStream(); //得到输出流
InputStream is = socket.getInputStream(); //得到输入流
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
客户端先用DataOutputStream发送图片长度。再用OutputStream发送图片字节。
服务端,先用DataInputStream接收图片长度,再用InputStream接收图片字节
[其他解释]
将图片转换成base64编码方式,然后将base64通过socket以字符串方式发出去。
[其他解释]
lz是如何发送数据的
[其他解释]
package net;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
public class DataSocket {
public static void main(String[] args) throws IOException {
File img1 = new File("C:/Documents and Settings/dell/My Documents/My Pictures/002.jpg");
File img2 = new File("C:/Documents and Settings/dell/My Documents/My Pictures/03342240.jpg");
Socket socket = new Socket("127.0.0.1", 4848);
String header1 = "a.jpg;" + img1.length();
String header2 = "b.jpg;" + img2.length();
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
FileInputStream in = new FileInputStream(img1);
FileInputStream in2 = new FileInputStream(img2);
byte[] buffer = new byte[8192];
int readBytes = -1;
out.write(header1.getBytes());
out.write("\r".getBytes());
out.flush();
while ((readBytes = in.read(buffer)) != -1) {
out.write(buffer, 0, readBytes);
}
out.flush();
out.write(header2.getBytes());
out.write("\r".getBytes());
out.flush();
while ((readBytes = in2.read(buffer)) != -1) {
out.write(buffer, 0, readBytes);
}
out.flush();
in.close();
in2.close();
out.flush();
out.close();
}
}