使用socket,服务器连续发送多张图片,android客户端接收图片只能接收到第一张
我想完成一个功能是:使用服务器来向客户端发送很多张图片,要在android端接收到这些图片,存到一个文件夹下面,同时在listview中并显示出来,现在第一个实现就出现问题了,服务器向客户端连续发送的多张图片,在android端只能接收到第一张。
原理:服务器是按照这样的次序发送的
文件名→文件长度→文件流→文件名→文件长度→文件流……
服务器端关键代码如下:
try{
out=new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
System.out.println(signal[0]+" "+signal[1]);
out.writeUTF(signal[0]);
out.flush();
out.writeUTF(signal[1]);
out.flush();
for(int i=0;i<signal.length-2;i++){
File file=new File(signal[i+2]);
String[] strs=signal[i+2].split("/");
String filename=strs[strs.length-1];
System.out.println("~~~~~~~~~~~~~~~~~"+filename);
out.writeUTF(filename);
out.flush();//发送文件名作为开始标记
long length=file.length();
out.writeLong(length);
out.flush();//发送长度
writeImage(signal[i+2],length);//发送文件
out.writeUTF("END");//发送一个结束标记
out.flush();
}
out.close();
socket.close();
}catch(Exception e){
System.out.println("发送消息出现错误!!!");
e.printStackTrace();
}
客户端关键代码
{
^^^^^
try{
for(int i=0;i<goods.size();i++){
String filename=getMessageStream.readUTF();
String path=savePath+filename;//图片的完整路径
System.out.println("图片的路径是"+path);
long length=getMessageStream.readLong();//图片的长度
System.out.println("图片的长度是"+length);
writePicToSD(path, getMessageStream,length);//存储图片
}
}catch(Exception e){
e.printStackTrace();
}
}
public void writePicToSD(String path,DataInputStream getMessageStream,long length) throws Exception{
byte[] buf=new byte[(int)length];
int passedlen=0;
long len=1;
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(path))));
//从socket中获得图片的流,首先在上面的路径下面获得能够写入的通道
while (true) {
int read = 0;
if (getMessageStream != null) {
read = getMessageStream.read(buf);
}
passedlen += read;
if (read == -1) {
break;
}
fileOut.write(buf, 0, read);
}
fileOut.close();
//socket.shutdownInput();
}
这样只能成功存储第一张,第二章的时候就报错了,错误如下:
现在很着急解决,不出世的大神们,出来99啊~~~
服务器
Android
图片
socket
String
--------------------编程问答--------------------
代码没贴全吧,个人估计是getMessageStream这个对象没处理干净或者重新设置。
--------------------编程问答--------------------
求大神~~如果只上传一张图片的话,在客户端接受完图片后关闭socket就不会出错,但是多张图片难道要每一次收到图片就关闭socket?然后再打开???
--------------------编程问答--------------------
客户端代码省略的地方是获得两个字符串了,那个不是关键~
--------------------编程问答--------------------
这代码写的不伦不类 稍等片刻
--------------------编程问答--------------------
package example;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) throws Exception {
new Thread(new ServerExample("C:\\Documents and Settings\\zhaojp\\My Documents\\My Pictures\\SS设定、概念图、壁纸\\", 5678)).start();
TimeUnit.SECONDS.sleep(2);
new Thread(new ClientExample("D:/aaaaaa/", "127.0.0.1", 5678)).start();
}
}
class ClientExample implements Runnable {
public ClientExample(String dirpath, String host, int port) {
this.dirpath = dirpath;
this.host = host;
this.port = port;
}
@Override
public void run() {
Socket client = null;
DataInputStream in = null;
BufferedOutputStream out = null;
try {
client = new Socket(host, port);
in = new DataInputStream(client.getInputStream());
int fileNums = in.readInt();
for (int i = 0; i < fileNums; i++) {
int nameSize = in.readInt();// 名称长度
long imgSize = in.readLong();// 文件长度
byte[] buffer = new byte[nameSize];
in.readFully(buffer);
out = new BufferedOutputStream(new FileOutputStream(new File(dirpath + new String(buffer))));
buffer = new byte[2048];
int len = -1;
while (imgSize > 0 && (len = in.read(buffer, 0, imgSize < buffer.length ? (int) imgSize : buffer.length)) != -1) {
out.write(buffer, 0, len);
imgSize -= len;
}
out.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {}
}
if (client != null && !client.isClosed()) {
try {
client.close();
} catch (IOException e) {}
}
}
}
private final String dirpath;
private final String host;
private final int port;
}
class ServerExample implements Runnable {
public ServerExample(String dirpath, int port) {
this.dirpath = dirpath;
this.port = port;
}
@Override
public void run() {
File dir = null;
ServerSocket server = null;
Socket socket = null;
try {
server = new ServerSocket(port);
while (true) {
socket = server.accept();
dir = new File(dirpath);
if (!dir.isDirectory()) {
throw new FileNotFoundException("当前路径指向位置并非目录");
}
BufferedInputStream in = null;
DataOutputStream out = null;
try {
File[] list = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".jpg") || pathname.getName().endsWith(".bmp") || pathname.getName().endsWith(".jpeg") || pathname.getName().endsWith(".gif");
}
});
out = new DataOutputStream(socket.getOutputStream());
out.writeInt(list.length);// 文件数量
for (File file : list) {
in = new BufferedInputStream(new FileInputStream(file));
out.writeInt(file.getName().getBytes().length);// 名称长度
out.writeLong(file.length());// 文件长度
out.write(file.getName().getBytes());
byte[] buffer = new byte[2048];
int len = -1;
while ((len = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
in.close();
}
} catch (IOException e) {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {}
}
if (server != null && !server.isClosed()) {
try {
server.close();
} catch (IOException e) {}
}
}
}
private final String dirpath;
private final int port;
}
--------------------编程问答--------------------
引用 5 楼 BearKin 的回复: package example;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) throws Exception {
new Thread(new ServerExample("C:\\Documents and Settings\\zhaojp\\My Documents\\My Pictures\\SS设定、概念图、壁纸\\", 5678)).start();
TimeUnit.SECONDS.sleep(2);
new Thread(new ClientExample("D:/aaaaaa/", "127.0.0.1", 5678)).start();
}
}
class ClientExample implements Runnable {
public ClientExample(String dirpath, String host, int port) {
this.dirpath = dirpath;
this.host = host;
this.port = port;
}
@Override
public void run() {
Socket client = null;
DataInputStream in = null;
BufferedOutputStream out = null;
try {
client = new Socket(host, port);
in = new DataInputStream(client.getInputStream());
int fileNums = in.readInt();
for (int i = 0; i < fileNums; i++) {
int nameSize = in.readInt();// 名称长度
long imgSize = in.readLong();// 文件长度
byte[] buffer = new byte[nameSize];
in.readFully(buffer);
out = new BufferedOutputStream(new FileOutputStream(new File(dirpath + new String(buffer))));
buffer = new byte[2048];
int len = -1;
while (imgSize > 0 && (len = in.read(buffer, 0, imgSize < buffer.length ? (int) imgSize : buffer.length)) != -1) {
out.write(buffer, 0, len);
imgSize -= len;
}
out.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {}
}
if (client != null && !client.isClosed()) {
try {
client.close();
} catch (IOException e) {}
}
}
}
private final String dirpath;
private final String host;
private final int port;
}
class ServerExample implements Runnable {
public ServerExample(String dirpath, int port) {
this.dirpath = dirpath;
this.port = port;
}
@Override
public void run() {
File dir = null;
ServerSocket server = null;
Socket socket = null;
try {
server = new ServerSocket(port);
while (true) {
socket = server.accept();
dir = new File(dirpath);
if (!dir.isDirectory()) {
throw new FileNotFoundException("当前路径指向位置并非目录");
}
BufferedInputStream in = null;
DataOutputStream out = null;
try {
File[] list = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".jpg") || pathname.getName().endsWith(".bmp") || pathname.getName().endsWith(".jpeg") || pathname.getName().endsWith(".gif");
}
});
out = new DataOutputStream(socket.getOutputStream());
out.writeInt(list.length);// 文件数量
for (File file : list) {
in = new BufferedInputStream(new FileInputStream(file));
out.writeInt(file.getName().getBytes().length);// 名称长度
out.writeLong(file.length());// 文件长度
out.write(file.getName().getBytes());
byte[] buffer = new byte[2048];
int len = -1;
while ((len = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
in.close();
}
} catch (IOException e) {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {}
}
if (server != null && !server.isClosed()) {
try {
server.close();
} catch (IOException e) {}
}
}
}
private final String dirpath;
private final int port;
}
我试了你的,在电脑上运行正常,但是一个放在电脑上,一个使用android就出错了,传了三个文件,在android端直接受到两个,但是都是无效的文件~~
--------------------编程问答--------------------
引用 6 楼 Jennis1990 的回复: Quote: 引用 5 楼 BearKin 的回复:
package example;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) throws Exception {
new Thread(new ServerExample("C:\\Documents and Settings\\zhaojp\\My Documents\\My Pictures\\SS设定、概念图、壁纸\\", 5678)).start();
TimeUnit.SECONDS.sleep(2);
new Thread(new ClientExample("D:/aaaaaa/", "127.0.0.1", 5678)).start();
}
}
class ClientExample implements Runnable {
public ClientExample(String dirpath, String host, int port) {
this.dirpath = dirpath;
this.host = host;
this.port = port;
}
@Override
public void run() {
Socket client = null;
DataInputStream in = null;
BufferedOutputStream out = null;
try {
client = new Socket(host, port);
in = new DataInputStream(client.getInputStream());
int fileNums = in.readInt();
for (int i = 0; i < fileNums; i++) {
int nameSize = in.readInt();// 名称长度
long imgSize = in.readLong();// 文件长度
byte[] buffer = new byte[nameSize];
in.readFully(buffer);
out = new BufferedOutputStream(new FileOutputStream(new File(dirpath + new String(buffer))));
buffer = new byte[2048];
int len = -1;
while (imgSize > 0 && (len = in.read(buffer, 0, imgSize < buffer.length ? (int) imgSize : buffer.length)) != -1) {
out.write(buffer, 0, len);
imgSize -= len;
}
out.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {}
}
if (client != null && !client.isClosed()) {
try {
client.close();
} catch (IOException e) {}
}
}
}
private final String dirpath;
private final String host;
private final int port;
}
class ServerExample implements Runnable {
public ServerExample(String dirpath, int port) {
this.dirpath = dirpath;
this.port = port;
}
@Override
public void run() {
File dir = null;
ServerSocket server = null;
Socket socket = null;
try {
server = new ServerSocket(port);
while (true) {
socket = server.accept();
dir = new File(dirpath);
if (!dir.isDirectory()) {
throw new FileNotFoundException("当前路径指向位置并非目录");
}
BufferedInputStream in = null;
DataOutputStream out = null;
try {
File[] list = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".jpg") || pathname.getName().endsWith(".bmp") || pathname.getName().endsWith(".jpeg") || pathname.getName().endsWith(".gif");
}
});
out = new DataOutputStream(socket.getOutputStream());
out.writeInt(list.length);// 文件数量
for (File file : list) {
in = new BufferedInputStream(new FileInputStream(file));
out.writeInt(file.getName().getBytes().length);// 名称长度
out.writeLong(file.length());// 文件长度
out.write(file.getName().getBytes());
byte[] buffer = new byte[2048];
int len = -1;
while ((len = in.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
in.close();
}
} catch (IOException e) {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {}
}
if (server != null && !server.isClosed()) {
try {
server.close();
} catch (IOException e) {}
}
}
}
private final String dirpath;
private final int port;
}
我试了你的,在电脑上运行正常,但是一个放在电脑上,一个使用android就出错了,传了三个文件,在android端直接受到两个,但是都是无效的文件~~
具体异常贴贴
--------------------编程问答--------------------
LZ解决了吗?解决了发给demo给我:gfj19900401@163.com O(∩_∩)O谢谢楼主
--------------------编程问答--------------------
楼主,可以讲服务器中的所有要发送的图片打包成一个.zip包,再发送,然后客户端解压。。。。
补充: Java , Java SE