我有一個java文件服務器發送文件到android客戶端。出於某種原因,服務器似乎在關閉套接字後,只有一次通過,我希望它發送所有文件,然後關閉套接字,這是我的服務器代碼。在此先感謝線程java服務器只發送一個文件
public void run() {
DataOutputStream dos = null;
try {
String path = "/home/test";
int count = 0;
dos = new DataOutputStream(
new BufferedOutputStream(socket.getOutputStream()));
DataInputStream dis = new DataInputStream(new BufferedInputStream(
socket.getInputStream()));
File[] files = new File(path).listFiles();
for (File file : files) {
String filename = file.getName();
String extension = filename.substring(
filename.lastIndexOf(".") + 1, filename.length());
if ("png".equals(extension)) {
count += 1;
}
}
System.out.println("Sending " + count + " files");
dos.writeInt(count);
dos.flush();
byte[] temp = new byte[1024];
int n = 0;
for (File file : files) {
String filename = file.getName();
String extension = filename.substring(
filename.lastIndexOf(".") + 1, filename.length());
if ("png".equals(extension)) {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte fileContent[] = new byte[(int) file.length()];
bis.read(fileContent);
int dataLength = fileContent.length;
dos.writeInt(dataLength);
dos.flush();
int open = dis.readInt();
System.out.println("still open if open = 5: open = "+open);
System.out.println(filename + " is " + dataLength
+ " bytes long");
while ((dataLength > 0)
&& (n = bis.read(temp, 0,
(int) Math.min(temp.length, dataLength))) != -1) {
dos.write(temp, 0, n);
dos.flush();
dataLength -= n;
}
System.out.println("Sent file "+filename);
fis.close();
bis.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try{
if (dos != null) {dos.close();}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
一個選項可能是代碼在循環內部拋出異常。檢查你的日誌。 –
有什麼異常?如果我們認爲代碼是您的測試代碼,您怎麼能確定它是客戶端問題。 –