Java 从套接字方法下载文件时,下载不会';我没做完手术
我正在编写一个程序,该程序能够使用以下方法通过套接字移动文件,但当我从套接字下载文件时,客户端程序中的下载方法不会结束操作并退出循环,尽管服务器端的上载方法已完成操作。 这是我在客户端的下载方法Java 从套接字方法下载文件时,下载不会';我没做完手术,java,Java,我正在编写一个程序,该程序能够使用以下方法通过套接字移动文件,但当我从套接字下载文件时,客户端程序中的下载方法不会结束操作并退出循环,尽管服务器端的上载方法已完成操作。 这是我在客户端的下载方法 public synchronized void downloadFile(String url) throws IOException{ try (FileOutputStream fileOutputStream = new FileOutputStream(url)) {
public synchronized void downloadFile(String url) throws IOException{
try (FileOutputStream fileOutputStream = new FileOutputStream(url)) {
int countedBytes;
byte[] buffer = new byte[kbBlockSize];
while ((countedBytes = input.read(buffer)) > 0)
fileOutputStream.write(buffer, 0, countedBytes);//end while
fileOutputStream.flush();
}//end try with resources block
}//end method downloadFile
下面是服务器端的上传方法
public synchronized void uploadFile(String url) throws IOException {
try (FileInputStream fileInputStream = new FileInputStream(url)) {
int countedBytes;
byte[] buffer = new byte[kbBlockSize];
while ((countedBytes = fileInputStream.read(buffer)) > 0)
output.write(buffer, 0, countedBytes);//end while
output.flush();
}//end try with resources block
}//end method uploadFile
但由于上述方法已完成工作,下载方法无法完成
如果有人能提供帮助,我将不胜感激。除非inputstream关闭,否则input.read(缓冲区)将始终大于0。
它将阻塞,直到至少有一个字节或EOF
因此,要么服务器必须关闭流,要么单独发送下载大小,比如某种头文件
例如,在http中,可以设置内容长度或使用分块编码来表示数据大小
为什么要创建一个新的协议,而不是使用http或ftp之类的东西 什么是
输入
和输出
?请提供。输入为客户端套接字的ObjectInputStream,输出为服务器套接字的ObjectOutStream