Socket实现多张图片的传输_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Socket实现多张图片的传输

Socket实现多张图片的传输

 2011/10/20 8:10:46  JingAi_jia  http://jingai-jia.iteye.com  我要评论(0)
  • 摘要:最近在做一个项目是要用到Socket实现从服务器端的指点目录下传输多张图片到客户端,备份服务器端的图片,同时删除原来的图片。我的客户端时android的。在开发过程中,我先用java测试通过后在移植到android上。我也是最近才接触socket,想实现上述功能,到网上找了一些资料没有找到满意的。后来就结合网上找的资料自己摸索,实现了上述功能,于是想分享一下,希望对各位有帮助。废话不说,直接上代码,如有雷同,请多多包涵。服务器端:importjava.io
  • 标签:实现 图片 socket

最近在做一个项目是要用到Socket实现从服务器端的指点目录下传输多张图片到客户端,备份服务器端的图片,同时删除原来的图片。我的客户端时android的。在开发过程中,我先用java测试通过后在移植到android上。我也是最近才接触socket,想实现上述功能,到网上找了一些资料没有找到满意的。后来就结合网上找的资料自己摸索,实现了上述功能,于是想分享一下,希望对各位有帮助。废话不说,直接上代码,如有雷同,请多多包涵。
服务器端:
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
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.net.ServerSocket;
import java.net.Socket;

public class Server {
??????? //定义要读取的图片的路径
public static final String IMAGE_PATH = "D:" +File.separator+ "ftp_temp" + File.separator;
??????? //图片备份的文件夹路径
public static final String TEMP_IMAGE_PATH = IMAGE_PATH + "temp" + File.separator;


public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(9999);
Socket client = server.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
System.out.println("start server...");
String line = reader.readLine();
System.out.println("line -- > " + line);
out = readImagesFromCard(out);
out.flush();
out.close();
client.close();
server.close();
} catch(Exception e) {
e.printStackTrace();
}
}
//读取图片,并存放在socket下的outputStream输出流中
public static DataOutputStream readImagesFromCard(DataOutputStream dos) {
File[] files = null;
try {
InputStream inputStream = null;
int size = 2 * 1024;
byte[] buffer = new byte[size];
byte[] b = null;
int len;
String path = IMAGE_PATH;
File file = new File(path);
if(file.isDirectory()) {
files = file.listFiles();
for(int i = 0 ; i < files.length ; i ++) {
len = -1;
if(files[i].isFile()) {
inputStream = readImage(files[i]);
dos.writeUTF(files[i].getName());
dos.writeLong(files[i].length());
while((len = inputStream.read(buffer)) != -1) {
dos.write(buffer, 0, len);
if(len != size) {
b = new byte[size - len];
dos.write(b,0,b.length);
}
}
?//先关闭该文件的输入流,否则无法删除
inputStream.close();
files[i].delete();
}
}
dos.writeUTF("end");
}
} catch(Exception e) {
e.printStackTrace();
}
return dos;
}

??????? //如果该图片没有备份,则先备份,最后返回该文件的InputStream输入流
public static InputStream readImage(File file) {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
File tempFile = new File(TEMP_IMAGE_PATH + file.getName());
if(!tempFile.exists()) {
tempFile.createNewFile();
FileOutputStream outputStream = new FileOutputStream(tempFile);
byte[] buffer = new byte[2 * 1024];
int len = -1;
while((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer,0,len);
}
outputStream.close();
inputStream.close();
inputStream = null;
inputStream = new FileInputStream(file);
}
} catch(Exception e) {
e.printStackTrace();
}
return inputStream;
}
}



客户端:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

private static final String PATH = "D:" + File.separator + "temp" + File.separator;

public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1",9999);
PrintWriter out = new PrintWriter(new BufferedOutputStream(socket.getOutputStream()));
DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
System.out.println("start client....");
out.println("sendImage");
out.flush();
System.out.println("sendImage....");
byte[] buffer = new byte[2 * 1024];
int len = -1;
String imageName = null;
FileOutputStream fos = null;
File file = null;
long fileLength = -1;
while(true) {
imageName = inputStream.readUTF();
if(imageName != null && imageName.trim().equals("end")) {
break;
}
file = new File(PATH + imageName);
if(!file.exists()) {
file.createNewFile();
} else if(file.isFile()) {
file.delete();
file.createNewFile();
} else if(file.isDirectory()) {
continue;
}
fileLength = inputStream.readLong();
len = -1;
fos = new FileOutputStream(file);
while(fileLength > 0) {
len = inputStream.read(buffer);
fos.write(buffer, 0, len);
if(len != -1) {
fileLength -= len;
}
}
fos.flush();
fos.close();
fos = null;
imageName = null;
file = null;
}
out.close();
socket.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}

第一次发表文件,对各位造成的不便请多多包涵。

发表评论
用户名: 匿名