62,614
社区成员
发帖
与我相关
我的任务
分享
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();
}
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;
}