Java 服务器客户端文件传输程序出错?
服务器类:Java 服务器客户端文件传输程序出错?,java,eclipse,sockets,socketserver,Java,Eclipse,Sockets,Socketserver,服务器类: import java.net.*; import java.io.*; public class fileserver { public static void main(String args []) { ServerSocket ss=new ServerSocket(2345); Socket s= ss.accept(); FileInputStream f=new FileInputStream("D:\\
import java.net.*;
import java.io.*;
public class fileserver {
public static void main(String args []) {
ServerSocket ss=new ServerSocket(2345);
Socket s= ss.accept();
FileInputStream f=new FileInputStream("D:\\FEATURED.txt");
DataOutputStream dout= new DataOutputStream(s.getOutputStream);
byte[] b=new byte[2002];
f.read(b,0,b.length);
dout.write(b,0,b.length);
dout.close();
f.close();
s.close();
}
}
客户端类:
import java.io.*;
import java.net.*;
public class clientserver {
public static void main(String args []) {
Socket s=new Socket("localhost",2345);
FileOutputStream f=new FileOutputStream("E:\\FEATUREDCOPIED.txt");
DataInputStream din= new DataInputStream(s.getInputStream);
byte[] b=new byte[2002];
din.read(b,0,b.length);
f.write(b,0,b.length);
din.close();
f.close();
s.close();
}
}
错误:
请帮我解决这个问题。
getInputStream
和getOutputStream
都是方法,所以你必须调用它们,这意味着在它们的名字后面加上括号:s.getInputStream()
,而不仅仅是s.getInputStream
请注意这个妄想,因为您在套接字中调用了一个方法:s.getOutputStream**()**
s.getInputStream**()**
**更正了代码**
修复的bug是
1.更改s.getInputStream->s.getInputStream()
2.更改s.getOutputStream->s.getOutputStream()
3.创建一个文件读写循环,否则数据可能丢失
服务器类
:
import java.net.*;
import java.io.*;
public class fileserver {
static Final int PORT_NUM= 4465;
public static void main(String args []) {
try {
ServerSocket ss=new ServerSocket(PORT_NUM);
Socket s= ss.accept();// new socket is created which is now connected with client.
FileInputStream fin=new FileInputStream("D:\\FEATURED.txt");
DataOutputStream dout= new DataOutputStream(s.getOutputStream());
byte[] Buffer=new byte[20];
int read;//read variable is use to read length of bytes which is read by fin in one iterations.
while((read=fin.read(Buffer,0,Buffer.length))>0){
dout.write(Buffer,0,read);
}
dout.close();
fin.close();
s.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
客户端类
:
import java.io.*;
import java.net.*;
import java.lang.*;
public class clientserver {
static Final int PORT_NUM=4465;
public static void main(String args []) {
try {
Socket s=new Socket("localhost",PORT_NUM);
FileOutputStream fout=new FileOutputStream("E:\\FEATUREDCOPIED.txt");// Output file stream is used to write on file
DataInputStream din= new DataInputStream(s.getInputStream());// input stream is created to read data from buffer.
byte[] Buffer=new byte[20];
int readlen;//readlen variable is to read the length of bytes which is read by din in one iteration.
while((readlen=din.read(Buffer,0,Buffer.length))>0){
f.write(Buffer,0,readlen);
}
din.close();
fout.close();
s.close();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
提供无注释的可复制粘贴代码是一个糟糕的想法,因为它什么也教不到。此外,您甚至没有修复损坏的read With loop。@Joachim Sauer我无法理解您的评论。你能详细说明一下吗?然后我可以解决你想指定的问题。有两个问题:1。您发布代码时没有解释所修复的内容,这无助于学习。2.调用
read
,而不检查返回值<代码>阅读< /代码>不能保证读取<代码>输入流的全长,并且调用它在一个循环之外几乎总是错误的。“JoajimSouER,我明白你的意思,你是对的,我修复了这个bug。