我编写了两个类作为客户端和服务器,它们可以在两个方向上传输文件,有一些错误,第一个错误是当我选择从客户端上传文件到服务器时,文件可以成功上传,但是,服务器进程将被InputStream中的空指针异常停止pis = connectionSocket.getInputStream(); 第二个错误是当你成功下载并想要测试另一个下载时,它不会正确传输文件,但是如果你第三次下载它,它会再次运行,很奇怪。以下代码是我所拥有的,当它要求您输入文件名时,您必须输入类似c:\ users \ file.file的内容。希望有人可以帮助我,非常感谢。 tcpserver.java
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String args[]) {
String direction="";
String filename="";
byte[] aByte = new byte[1];
int bytesRead;
while (true) {
ServerSocket welcomeSocket = null;
Socket connectionSocket = null;
BufferedOutputStream outToClient = null;
BufferedReader inFromClient =null;
try {
welcomeSocket = new ServerSocket(3248);
connectionSocket = welcomeSocket.accept();
outToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
System.out.println("connection is established with "+connectionSocket.getInetAddress()+"using port "+connectionSocket.getPort());
inFromClient =new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
direction = inFromClient.readLine();
System.out.println("client wants to "+direction);
filename=inFromClient.readLine();
System.out.println("file directory and name is "+filename);
} catch (IOException ex) {
// Do exception handling
}
if (outToClient != null&&direction.equals("download")) {
File myFile = new File(filename);
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(myFile);
} catch (FileNotFoundException ex) {
System.out.println("can't find file");
// Do exception handling
}
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(mybytearray, 0, mybytearray.length);
outToClient.write(mybytearray, 0, mybytearray.length);
outToClient.flush();
outToClient.close();
connectionSocket.close();
fis.close();
bis.close();
inFromClient.close();
} catch (IOException ex) {
// Do exception handling
}
}
if(direction.equals("upload"))
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//byte[] yourByteArray = new byte[10240];
File serverFile = new File(filename);
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
InputStream pis = connectionSocket.getInputStream();
fos = new FileOutputStream(filename);
bos = new BufferedOutputStream(fos);
bytesRead = pis.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesRead = pis.read(aByte);
} while (bytesRead != -1&&aByte!=null);
bos.write(baos.toByteArray());
bos.flush();
bos.close();
//clientSocket.close();
} catch (IOException ex) {
// Do exception handling
}
System.out.println("The file transmission finishes!");
}
}
}
}
TCPClient.java
import java.io.*;
import java.io.ByteArrayOutputStream;
import java.net.*;
import java.util.*;
class TCPClient {
public static void main(String args[]) {
byte[] aByte = new byte[1];
int bytesRead;
String downloadfile;
String downloadfilere;
String uploadfile;
String uploadfilese;
Socket clientSocket = null;
InputStream is = null;
String ip;
int port;
String conti;
boolean goon=true;
int situation;
Scanner sc=new Scanner(System.in);
Scanner sc1=new Scanner(System.in);
Scanner sc2=new Scanner(System.in);
System.out.println("please input the ip address for the host");
ip=sc.nextLine();
System.out.println("please input the port number");
port=sc.nextInt();
DataOutputStream outToServer=null;
BufferedOutputStream fileoutToServer = null;
while(goon=true){
try {
clientSocket = new Socket(ip, port);
is = clientSocket.getInputStream();
System.out.println("connection is established");
outToServer = new DataOutputStream(clientSocket
.getOutputStream());
} catch (IOException ex) {
// Do exception handling
}
System.out.println("what request do you want to make: "+"\n"+"1.download"+"\n"+"2.upload");
situation=sc.nextInt();
if(situation==1){
try{ outToServer.writeBytes("download"+ "\n");
outToServer.flush();}catch(IOException ex){}
System.out.println("please enter the file name for download");
downloadfile=sc1.nextLine();
//try{ outToServer.writeBytes("download"+ "\n");}catch(IOException ex){}
System.out.println("please enter the file name you want the received file to be saved as");
downloadfilere=sc2.nextLine();
try{ outToServer.writeBytes(downloadfile+ "\n");
outToServer.flush();
}catch(IOException ex){}
if(downloadfile!=""){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (is != null) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(downloadfilere);
bos = new BufferedOutputStream(fos);
bytesRead = is.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesRead = is.read(aByte);
} while (bytesRead != -1);
bos.write(baos.toByteArray());
bos.flush();
bos.close();
clientSocket.close();
} catch (IOException ex) {
// Do exception handling
}
}}
System.out.println("file received successfully");
}
if(situation==2)
{
try{ outToServer.writeBytes("upload"+ "\n");}catch(IOException ex){}
System.out.println("please enter the file name for upload");
uploadfile=sc1.nextLine();
System.out.println("please enter the file name on server for upload");
uploadfilese=sc2.nextLine();
try{ outToServer.writeBytes(uploadfilese+ "\n");}catch(IOException ex){}
File myFile = new File(uploadfile);
byte[] yourByteArray = new byte[1024];//*
try{
FileInputStream pfis = new FileInputStream(myFile);
//BufferedInputStream bbis=new BufferedInputStream(pfis);
int pnread=0;
OutputStream pbos = clientSocket.getOutputStream();
//System.out.println("pfis.read(yourByteArray)"+pfis.read(yourByteArray));//***
while((pnread=pfis.read(yourByteArray))>0)
{
pbos.write(yourByteArray, 0, pnread);
//System.out.println("pfis.read(yourByteArray)"+pnread);//***
pbos.flush();
}
pbos.close();
}catch(IOException ex){}
}
System.out.println("do you want to do another request?");
conti=sc.nextLine();
if(conti.equals("yes"))
goon=true;
else
goon=false;
//try{clientSocket.close();}catch(IOException ex){}
}
}
}
错误消息:
Exception in thread "main" java.lang.NullPointerException
at TCPServer.main(TCPServer.java:79)
Java Result: 1
答案 0 :(得分:3)
来自的空指针异常 InputStream pis = connectionSocket.getInputStream()
所以'connectionSocket'为空。这是因为你的错误执行处理结构。考虑如果您未能构造'connectionSocket'然后在catch块之后继续执行代码会发生什么。所有使用'connectionSocket'的代码都应该在构造它的'try'块中。
您也忽略了read()方法的结果,并假设它返回了可能的最大数据量。没有指定这样做。你应该有这样的东西:
int count;
while ((count = in.read(buffer)) > 0)
out.write(buffer, 0, count);
答案 1 :(得分:1)
我怀疑问题在于您是在两次致电connectionSocket.getInputStream
。您应该只获取一次流并在连接过程的生命周期中使用它。
这是无关的,但为什么在客户端类中有三个Scanner
个实例?你只需要一个。他们都是从同一个基础流(System.in
)读取的。