String的readObject中的异常

时间:2016-10-13 14:14:57

标签: java eclipse sockets

我正在尝试从字符串中逐字地读取文本文件(从文件中逐行读取)。

它工作正常,直到客户端的readObject没有任何东西可读,而且我得到异常并转到“客户端错误”。

我试图关闭流和套接字,提出问题,我也尝试使用扫描仪,但上面的选项都没有帮助我。

你帮我吗?

客户方:

package hit.model;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JOptionPane;    

public class MMUClient {
     private ArrayList<String> userDetails;
     private String fileFromServer =null;
     private ObjectOutputStream outToServer;
     private ObjectInputStream inFromServer;
     private String fileName;
     private boolean ERROR = true;
     private String messageError = "No Errors";
     private PrintWriter printerWriter;

     public MMUClient(ArrayList<String> userParameters){
      userDetails = userParameters;
     };

     public MMUClient(String filePath){
         fileName = filePath;
     };

     public ArrayList<String> getUserDetails() {
      return userDetails;
     }

     public void setUserDetails(ArrayList<String> userDetails) {
      this.userDetails = userDetails;
      clientAuthenticate();
     }

     public void clientAuthenticate(){
       try{
             Socket myServer = null;
             try {
                     //1. creating a socket to connect to the server
                     myServer = new Socket("localhost", 12345);
                     System.out.println("Connected to localhost in port 12345");
                     //2. get Input and Output streams
                     outToServer = new ObjectOutputStream(myServer.getOutputStream());
                     inFromServer=new ObjectInputStream(myServer.getInputStream());
                     //3: Communicating with the server
                     outToServer.writeObject(userDetails);
                     //4. get server answer
                     fileFromServer =  (String) inFromServer.readObject();
                     printerWriter = new PrintWriter("logClient.txt");
                         if(fileFromServer.contains("Error")){
                            messageError = "Error";
                            ERROR = true;
                        }
                         else{
                             if (fileFromServer.contains("Wrong")){
                                 messageError = "Wrong";
                                 ERROR = true;
                             }
                             else
                                 while(fileFromServer != null){
                                //   messageError = "No Errors";
                                //   ERROR = false;
                                     System.out.println(fileFromServer);
                                     printerWriter.println(fileFromServer);
                                //   writeData(fileFromServer);
                                     fileFromServer = (String) inFromServer.readObject();
                                 }

                                printerWriter.close();
                         }                      

                 } catch (IOException e) {
                  System.out.println("Client error");
                 }finally{
                     inFromServer.close();
                     outToServer.close();
                     myServer.close();
                 }

             }catch (Exception e) {
              System.out.println("Client error Details");
             }         
         }


//**********************write into text file from server***************************
/*  private void writeData(String lineToWrite) {
        FileWriter fileWriter = null;
        String filetowrite = "logClient.txt";
        try {
                PrintWriter printerWriter = new PrintWriter(filetowrite);
                printerWriter.println(lineToWrite);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
    }
*/
//************************if there is any error with the client******************************
    public boolean getError(){
        return ERROR;
    }

    public String getMessageError() {
        return messageError;
    }

    public void setMessageError(String messageError) {
        this.messageError = messageError;
    }
}

服务器端:

package hit.applicationservice;    

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;
import hit.login.AuthenticationManager;

public class MMULogFileApplicationService implements Runnable {

    //MMULogService logService;
    AuthenticationManager authenticateDetails;
    MMULogFileBrowsing browsing;
    ArrayList<String> userDetails;
    private Socket someClient = null;
    private ObjectOutputStream outToClient;
    private ObjectInputStream inFromClient;
    String filePath = "/Users/oferg/Desktop/lastVirsion/MMUProject/log.txt";

    public MMULogFileApplicationService (Socket socket ){
        someClient = socket;
    };

    @Override
    public void run() {
         //3. get Input and Output streams
        try{
        outToClient = new ObjectOutputStream(someClient.getOutputStream());
        inFromClient = new ObjectInputStream(someClient.getInputStream());
        userDetails = (ArrayList<String>) inFromClient.readObject();
        System.out.println("Connection successful ");
        }catch(IOException | ClassNotFoundException ioException){
            ioException.printStackTrace();
        }
        boolean userFound = false;
        try {
            authenticateDetails = new AuthenticationManager();
            userFound = authenticateDetails.authenticate(userDetails.get(0), userDetails.get(1));
            if(userFound)
            {
                browsing = new MMULogFileBrowsing(someClient, userDetails.get(2), filePath);
                if(!browsing.searchIfFileExist()){      
                     //write object to Socket
                    String sendMessage = "Wrong FileName the file isn't found";
                    outToClient.writeObject(sendMessage);
                    }
                else{
                    getFileToClient();
                }                       
            }
            else
            {
                //write object to Socket
                String sendMessage = "Error - the user isn't exist";
                outToClient.writeObject(sendMessage);
            }
        } catch (ClassNotFoundException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                inFromClient.close();
                outToClient.close();
                someClient.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }               
        }               
    }

    private void getFileToClient() throws IOException {
        FileReader fileReader = null;
        String currentLine = null;

        try {
            fileReader = new FileReader(filePath);
        }   catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader bufferedReader = new BufferedReader(fileReader);
            while ((currentLine = bufferedReader.readLine())!= null){
                if (currentLine.isEmpty() == false ){
                    outToClient.writeObject(currentLine);
                    outToClient.flush();
                }
            }
            outToClient.close();
            bufferedReader.close();
            fileReader.close();
        }
}

1 个答案:

答案 0 :(得分:0)

每个人都得到答案。 我想我找到了通过下一个简单循环代码传递文本文件的方法:

String currentLine = "";
BufferedReader bufferedReader = new BufferedReader(fileReader);
    while ((tempLine = bufferedReader.readLine())!= null){
        if (tempLine.isEmpty() == false ){
            currentLine = currentLine+tempLine+"\n";
        }
    }

以这种方式我逐行复制文本文件并将其发送到客户端1个字符串,而不是我能做任何我想做的事。

tnx every1。 和平