我正在尝试使用套接字创建文件传输系统。在我开始从服务器向客户端发送String fileName以使文件具有相同名称之前,我的代码曾经正常工作。现在,每当我尝试发送文件时,它都会在客户端和服务器中不断给出错误。
服务器端代码:
public void soc_server() throws IOException {
long time = System.currentTimeMillis();
long totalSent = 0;
ServerSocket servsock = null;
Socket sock = null;
PrintWriter pw = null;
FileInputStream fileInputStream = null;
try {
servsock = new ServerSocket(55000);
sock = servsock.accept();
System.out.println("Hello Server");
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the file name or file path");
String s = sc.nextLine();
sc.close();
File file = new File(s);
if (file.exists())
System.out.println("File found");
else
System.out.println("File not found");
OutputStream out = sock.getOutputStream();
pw = new PrintWriter(sock.getOutputStream(), true);
pw.print(s);
fileInputStream = new FileInputStream(s);
byte[] buffer = new byte[100 * 1024];
int bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
if (bytesRead > 0) {
out.write(buffer, 0, bytesRead);
totalSent += bytesRead;
System.out.println("sent " + (totalSent / 1024) + " KB "
+ ((System.currentTimeMillis() - time) / 1000)
+ " sec");
}
}
} catch (Exception e) {
System.out.println("exception " + e);
} finally {
sock.close();
pw.close();
servsock.close();
fileInputStream.close();
System.out.println("Sent " + (totalSent / 1024) + " kilobytes in "
+ ((System.currentTimeMillis() - time) / 1000) + " seconds");
}
}
客户端代码:
public void soc_client() throws Exception {
long time = System.currentTimeMillis();
long totalRecieved = 0;
Socket sock = null;
InputStream in = null;
BufferedReader br = null;
FileOutputStream fileOutputStream = null;
try {
sock = new Socket("172.16.27.106", 55000);
System.out.println("Hello Client");
in = sock.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
String fileName = br.readLine();
File outputFile = new File(fileName + "");
fileOutputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[100 * 1024];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
totalRecieved += bytesRead;
System.out.println("Recieved " + (totalRecieved / 1024)
+ " kilobytes in "
+ ((System.currentTimeMillis() - time) / 1000)
+ " seconds");
}
} catch (Exception e) {
System.out.println("Exception " + e);
} finally {
br.close(); // CLOSING BufferedReader
fileOutputStream.close();
sock.close();
System.out.println("Recieved " + totalRecieved + " bytes in "
+ (System.currentTimeMillis() - time) + "ms.");
}
}
例外:
客户端:
Exception java.io.FileNotFoundException: Invalid file path
Exception: java.lang.NullPointerException
Exception in thread "main" java.io.FileNotFoundException: Invalid file path
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at Client.soc_client(Client.java:25)
at Index.main(Index.java:24)
服务器端:
Exception java.net.SocketException: Connection reset
Exception: java.util.NoSuchElementException
Exception java.net.SocketException: Broken pipe
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at Server.soc_server(Server.java:59)
at Index.main(Index.java:21)
我尝试发送的文件与编译该类的目录(桌面)相同。 谢谢。
答案 0 :(得分:-1)
尝试直接在客户端的路径中提供文件名。
File outputFile = new File("yourfile.txt");
然后将其发送到服务器。
由于客户端的异常FileNotFound
,您正在关闭finaly
块的流。
在关闭客户端流时,服务器端无法识别正在读取的流,因此会出现Connection reset
异常。
由于没有用于在服务器端读取数据的流,因此您将获得NoSuchElement
例外
修改强>
另一件事是,在写入客户端后,您没有刷新流,
pw.flush();
和pw.print(s)
out.write()
也是如此