为什么收不到文件??
服务器端
public class FileServer
{
public FileServer()
{
try
{
ServerSocket ss = new ServerSocket(4700);
System.out.println ( "服务器启动! ");
while (true)
{
Socket socket = ss.accept();
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out=new PrintWriter(socket.getOutputStream());
FileInputStream fis=new FileInputStream( "E:\\1.txt ");
FileOutputStream fos=null;
byte[] buffer=new byte[1024];
int n=0;
while((n=fis.read(buffer))!=-1)
{
fos.write(buffer,0,n);
}
out.println(buffer);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new FileServer();
}
}
客户端:
public class FileClient
{
public FileClient()
{
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
FileInputStream fis = null;
FileOutputStream fos = null;
byte[] buffer = new byte[1024];
int n = 0;
try
{
socket = new Socket( "127.0.0.1 ",4700);
System.out.println( "连接成功! ");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(socket.getOutputStream());
fos = new FileOutputStream( "F:\\1.txt ");
while((n=fis.read(buffer)) != -1)*******
NullPointerException************
{
fos.write(buffer,0,n);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new FileClient();
}
}
我哪写错了?为什么会出现NullPointerException?希望高手帮忙!
------解决方案--------------------NullPointerException详细一点的信息?哪个Java文件的哪行?那一行是什么代码?
------解决方案--------------------fis为null
------解决方案--------------------FileInputStream fis = null; <-------这个fis一直是null,如下修改即可
------------------------
InputStream in = socket.getInputStream();
out=new PrintWriter(socket.getOutputStream());
fos = new FileOutputStream( "F:\\1.txt ");
while((n=in.read(buffer)) != -1)
...