如何通过套接字发送文件?java

ie3xauqp  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(190)

我正在尝试通过套接字发送一个文件,当前从客户端发送到服务器。要写入的文件已创建,但当我打开它时,它是空的。另外,如果我在发送文件之后向服务器发送一个字符串,那么我发送的内容会被写入应该接收的文本文件中。
我只测试文本文件。
为什么会这样?我怎样才能纠正它?

jbutton.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        JFileChooser fc = new JFileChooser();
                        fc.setCurrentDirectory(new File(System.getProperty("user.home")));
                        FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg","gif","png","txt");
                        fc.addChoosableFileFilter(filter);
                        int result = fc.showSaveDialog(null);
                         if(result == JFileChooser.APPROVE_OPTION){
                             File selectedFile = fc.getSelectedFile();
                             String path = selectedFile.getAbsolutePath();
                            try {
                                userText.setText("Sending File: " + selectedFile);
                                file = new FileInputStream(path);
                                byte b[] = new byte[30000];
                                file.read(b,0,b.length);
                                os = connection.getOutputStream();
                                os.write(b,0,b.length);

                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            catch (IOException e) {
                                e.printStackTrace();
                            }

                         }
                         else if(result == JFileChooser.CANCEL_OPTION){
                             System.out.println("No File Select");
                         }  
                    }
//get stream to send and receive data 
    private void setupStreams() throws IOException{
        output = new ObjectOutputStream(socket.getOutputStream());
        output.flush();
        input = new ObjectInputStream(socket.getInputStream());
        showMessage("\n Streams Are now Set up \n");
    }

    //during the chat conversation
    private void whileChatting() throws IOException{
        String message = "You are now connected ";
        showMessage(message);
        ableToType(true);
        do {
            try {
                message = (String) input.readObject();
                showMessage("\n" + message);

                byte b[] = new byte[30000];
                InputStream is = socket.getInputStream();
                FileOutputStream fr = new FileOutputStream("C:\\Users\\Documents\\TestingFileClientToServer.txt");
                is.read(b,0,b.length);
                fr.write(b,0,b.length);

            }catch(ClassNotFoundException classNotFoundException) {
                showMessage("\n Error on input \n");
            }

        }while(!message.equals("CLIENT - END"));
    }
r8xiu3jd

r8xiu3jd1#

所以这是错误的(在两个地方):

fr.write(b,0,b.length);

您应该获得在read调用中实际读取的字节数,并在write调用中使用该大小。
这也是一个问题:

message = (String) input.readObject();

不要使用这个,除非你真的是通过流发送对象。。。你不应该这样
至于回答你为什么文件没有内容的问题:你需要在所有数据被发送和刷新之后关闭发送端的套接字。然后在接收端,需要捕获socket闭包的socketexception,然后关闭要写入的文件。不关闭文件就是看不到任何内容的原因。
另一件事是,您无缘无故地创建对象流。不要使用对象流和数据流。只需使用原始输入/输出流。您不需要缓冲区,因为您正在进行高效的读写。

相关问题