我重写了一些代码来实现下面的两个答案,并且看到我得到它发送1个图像,但是现在我只需要它继续发送,但服务器在1个单个图像之后完成。
客户(已修订):
public void transferImage() {
File file = new File(ServerStats.clientFolder);
String[] files = file.list();
int numFiles = files.length;
boolean done = false;
BufferedInputStream bis;
BufferedOutputStream bos;
int num;
byte[] byteArray;
long count;
long len;
Socket socket = null ;
while (!done){
try {
socket = new Socket(ServerStats.imgServerName,ServerStats.imgServerPort) ;
InputStream inStream = socket.getInputStream() ;
OutputStream outStream = socket.getOutputStream() ;
System.out.println("Connected to : " + ServerStats.imgServerName);
BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
for (int itor = 0; itor < numFiles; itor++) {
String fileName = files[itor];
System.out.println("transfer: " + fileName);
File sentFile = new File(fileName);
len = sentFile.length();
System.out.println(len);
out.println(len);
out.println(sentFile);
//SENDFILE
bis = new BufferedInputStream(new FileInputStream(fileName));
bos = new BufferedOutputStream(socket.getOutputStream( ));
byteArray = new byte[1000000];
count = 0;
while ( (num = bis.read(byteArray)) >0 ){
//num = bis.read(byteArray);
bos.write(byteArray,0,num);
}
bos.close();
bis.close();
System.out.println("file done: " + itor);
}
done = true;
} catch (IOException ee){
System.err.println("FileIO Error: " + ee);
}
}
}
服务器(修订):
public static void main(String[] args) {
BufferedInputStream bis;
BufferedOutputStream bos;
int num;
File file = new File(ServerStats.serverFolder);
if (!(file.exists())){
file.mkdir();
}
try {
ServerSocket socket = new ServerSocket(ServerStats.imgServerPort);
Socket incoming = socket.accept();
try {
try{
if (!(file.exists())){
file.mkdir();
}
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
String length2 = inm.readLine();
System.out.println(length2);
String filename = inm.readLine();
System.out.println("Filename = " + filename);
out.println("ACK: Filename received = " + filename);
//RECIEVE and WRITE FILE
byte[] receivedData = new byte[1000000];
bis = new BufferedInputStream(incoming.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(ServerStats.serverFolder + "/" + filename));
//long length = (long)Integer.parseInt(length2);
//length++;
while ( (num = bis.read(receivedData)) > 0){
bos.write(receivedData,0,num);
}
//System.out.println(counter);
bos.close();
bis.close();
File receivedFile = new File(filename);
long receivedLen = receivedFile.length();
out.println("ACK: Length of received file = " + receivedLen);
} finally {
incoming.close();
}
} catch (IOException e){
e.printStackTrace();
}
} catch (IOException e1){
e1.printStackTrace();
}
}
输出:
Welcome to DrJava. Working directory is C:\Documents and Settings\Dapper Dan\Desktop
> run ServerQueue.Client
Connected to : localhost
transfer: Sunset.jpg
71189
file done: 0
transfer: Water lilies.jpg
83794
FileIO Error: java.net.SocketException: Socket is closed
Connected to : localhost
transfer: Sunset.jpg
71189
>
这是客户端的输出,唯一被转移的图像是Sunset.jpg,如文件完成声明所示。
关于导致这种情况的任何想法?
答案 0 :(得分:1)
您的尺寸计数器,它是否真的更新了您读过的字节数? 不应该是:
while (counter < length ){
num = bis.read(receivedData);
bos.write(receivedData,0,num);
counter += num;
}
答案 1 :(得分:1)
在客户端,在numFiles迭代器循环下移动套接字(和关联的流)
for (int itor = 0; itor < numFiles; itor++) {
//socket = new Socket(...
//socket's outputstream = ....
在服务器端,在while(true)循环下移动socket.accept调用及其下面的所有逻辑。