将多个文件上传到服务器。以下是客户端计算机的代码:
void SendFile() throws Exception
{
String path;
System.out.print("Enter File Path :");
path=br.readLine();
File f=new File(path);
File files[]=f.listFiles();
String fileName;
for(File file:files)
{
if(file.isFile())
{
fileName=file.getName();
dout.writeUTF(fileName);
System.out.println("Sending File ..."+fileName);
FileInputStream fin=new FileInputStream(f);
int ch;
do
{
ch=fin.read();
dout.writeUTF(String.valueOf(ch));
}
while(ch!=-1);
fin.close();
System.out.println(din.readUTF());
}
}
}
但是在发送单个文件后,程序会产生错误:
Exception in thread "main" java.io.FileNotFoundException: /home/bdi-user/Desktop/files (Is a directory)
答案 0 :(得分:2)
请勿从目录f
中读取,而是从您调用file
的文件中读取。
FileInputStream fin=new FileInputStream(file);