简体   繁体   中英

Java - cannot write to a second file

I've been having some trouble trying to send a file from a server to a client. I can't seem to send the same file from the server to the client into two SEPERATE files. Instead, it just appends to the end of the first file! Any help would be appreciated.

EDIT : I've modified the code. I've modularized the file sending and recieving tasks into 2 functions 'sendfile' and 'recievefile'. The error I'm getting now is that the socket is closed at the sendfile and recievefile functions after the second call. But all I'm closing is the file and the output and input streams! Perhaps closing those streams will close the socket too...? Anyways, I've tried NOT closing the input and output streams and what happens is - 1)nothing gets transferred to the destination file. So I just get a blankly created file at the server end. 2)The second file doesn't even get created. Great.

Any help would, as usual, be appreciated.

Server:

package com.http.server;
import java.io.*;
import java.net.*;

public class Server
{
    public static void main(String args[])throws Exception
    {
        System.out.println("Server running...");

        /* Listen on port 5555 */

        ServerSocket server = new ServerSocket(5555);

        /* Accept the sk */

        Socket sk = server.accept();

        System.out.println("Server accepted client");


        BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
        BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));

        /* Read the filename */
        String serverlocation = "C:/Users/Arjun/Desktop/CNW model/HTTP Website/";
        String filename = serverlocation + inReader.readLine();
        if ( !filename.equals("") ){

            /* Reply back to client with READY status */

            outReader.write("READY\n");
            outReader.flush();          
        }

        sendfile(sk, filename);
        sendfile(sk, filename);

        outReader.close();
        inReader.close();


        sk.close();
        server.close();

    }

    public static void sendfile(Socket sk, String filename)
    {
        try{

            OutputStream output = sk.getOutputStream();

        FileInputStream file = new FileInputStream(filename);

        byte[] buffer = new byte[sk.getSendBufferSize()];

        int bytesRead = 0; 

        while((bytesRead = file.read(buffer))>0)
        {
            output.write(buffer,0,bytesRead);
        }

        file.close();  
        output.close();
    }
        catch (Exception ex){
            System.out.println(ex);
        }
        }





    }

Client:

package com.http.client;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class Client extends JFrame implements ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JTextField txtFile;

    public static void main(String args[]){

        /* Create and display the client form */

        Client clientForm = new Client();
        clientForm.Display();
    }

    public void Display(){

        JFrame frame = new JFrame();
        frame.setTitle("Client");

        FlowLayout layout = new FlowLayout();
        layout.setAlignment(FlowLayout.LEFT);

        JLabel lblFile = new JLabel("URL:");

        txtFile = new JTextField();
        txtFile.setPreferredSize(new Dimension(150,30));

        JButton btnTransfer = new JButton("Get");
        btnTransfer.addActionListener(this);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(layout);
        mainPanel.add(lblFile);
        mainPanel.add(txtFile);
        mainPanel.add(btnTransfer);

        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        /* File Open Dialog box allows the user to select a file */

        String filename=txtFile.getText();

        try{

            /* Try to connect to the server on localhost, port 5555 */

            Socket sk = new Socket("localhost", 5555);


            /* Send filename to server */

            OutputStreamWriter outreader = new OutputStreamWriter(sk.getOutputStream());
            outreader.write(filename + "\n");
            outreader.flush();

            /* Get response from server */

            BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));

            String serverStatus = inReader.readLine(); // Read the first line
            /* If server is ready, receive the file */

            while ( !serverStatus.equals("READY") ){}

                /* Create a new file in the tmp directory using the filename */

               recievefile(sk,filename);
               recievefile(sk,"xx"+filename);


                JOptionPane.showMessageDialog(this, "Transfer complete");


                inReader.close();
                outreader.close();
            sk.close();

        }
        catch (Exception ex){
            /* Catch any errors */
            JOptionPane.showMessageDialog(this, ex.getMessage());
            System.out.println(ex);
        }
    }

    public void recievefile(Socket sk, String filename)
    {
        try{

            InputStream input = sk.getInputStream();

        FileOutputStream wr = new FileOutputStream(new File("C:/tmp/" + new File(filename).getName()));

        byte[] buffer = new byte[sk.getReceiveBufferSize()];
        int bytesReceived = 0;

        while((bytesReceived = input.read(buffer))>0)
        {
            /* Write to the file */
           wr.write(buffer,0,bytesReceived);
        } 
        wr.close();
        input.close();

    }
        catch (Exception ex){
            /* Catch any errors */
            JOptionPane.showMessageDialog(this, ex.getMessage());
            System.out.println(ex);
        }

}




}

When you send the files, you are just appending one to the end of the other so thats how the client see them.

You need to let the client know when to stop reading one file and start reading the second.

The simplest approach is to send the length of the file before the file and only read exactly than amount of data.

BTW: You cannot combine binary and text streams the way you have. This is more likely to lead to confusion. In your case you need to send binary so make everything binary with DataInputStream and DataOutputStream.


What I had in mind is something like.

public static void sendFile(DataOutput out, String filename) throws IOException {
    FileInputStream fis = new FileInputStream(filename);
    byte[] bytes = new byte[8192];
    try {
        long size = fis.getChannel().size();
        out.writeLong(size);
        for (int len; (len = fis.read(bytes, 0, (int) Math.min(bytes.length, size))) > 0; ) {
            out.write(bytes, 0, len);
            size -= len;
        }
        assert size == 0;
    } finally {
        fis.close();
    }
}

public void receiveFile(DataInput in, String filename) throws IOException {
    long size = in.readLong();
    FileOutputStream fos = new FileOutputStream(filename);
    byte[] bytes = new byte[8192];
    try {
        for (int len; (len = (int) Math.min(size, bytes.length)) > 0; ) {
            in.readFully(bytes, 0, len);
            fos.write(bytes, 0, len);
            size -= len;
        }
    } finally {
        fos.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM