使用JAVA通过套接字传输文件

时间:2012-07-10 14:54:29

标签: java sockets tcp client file-transfer

我在java中搜索一个代码,用于通过套接字发送多个文件,我发现这个代码由一个TX main,一个RX main和一个用于我假设的所有脏工作的类。代码运行没有错误,但我对专家有疑问,

在代码中,用户输入他/她想要发送到服务器的文件? 在服务器main中,服务器存储接收文件的位置是什么,以及名称是什么?

在这段代码(TX / RX / ByteStream)中,我应该修改指定哪个文件进入?

我想在客户端(TX)端自己输入文件名,我还会在其中包含一个JFileChooser,供用户选择Graphically哪个文件要发送。

package file_rx;

import java.io.*;
import java.net.*;

public class File_RX implements Runnable 
{
    private static final int port = 4711;
    private Socket socket;

    public static void main(String[] _) 
    {
        try
        {
            ServerSocket listener = new ServerSocket(port);

            while (true)
            {
                File_RX file_rec = new File_RX();
                file_rec.socket = listener.accept();

                new Thread(file_rec).start();
            }
        }
        catch (java.lang.Exception e)
        {
            e.printStackTrace(System.out);
        }
    }

    public void run()
    {
        try
        {
            InputStream in = socket.getInputStream();
            int nof_files = ByteStream.toInt(in);

            for (int cur_file = 0; cur_file < nof_files; cur_file++)
            {
                String file_name = ByteStream.toString(in);
                File file = new File(file_name);
                ByteStream.toFile(in, file);
            }
        }
        catch (java.lang.Exception e)
        {
            e.printStackTrace(System.out);
        }
    }
}

package file_tx;

import java.io.*;
import java.net.*;

public class File_TX 
{
    private static final int    port = 4711;
    private static final String host = "localhost";

    public static void main(String[] args) 
    {
       try
       {
           Socket socket = new Socket(host, port);
           OutputStream os = socket.getOutputStream();

           int cnt_files = args.length;
           ByteStream.toStream(os, cnt_files);

           for (int cur_file = 0; cur_file < cnt_files; cur_file++)
           {
               ByteStream.toStream(os, args[cur_file]);
               ByteStream.toStream(os, new File(args[cur_file]));
           }
       }
       catch (Exception e)
       {
           e.printStackTrace();
       }
    }
}


package file_rx;

import java.io.*;

public class ByteStream 
{
    private static byte[] toByteArray(int in_int)
    {
        byte a[] = new byte[4];
        for (int i = 0; i < 4; i++)
        {
            int b_int = (in_int >> (i*8)) & 255;
            byte b = (byte) (b_int);
            a[i] = b;
        }
        return a;
    }

    private static int toInt(byte[] byte_array_4)
    {
        int ret = 0;
        for (int i = 0; i < 4; i++)
        {
            int b = (int) byte_array_4[i];
            if (i < 3 && b < 0)
            {
                b = 256 + b;
            }
            ret += b << (i * 8);
        }
        return ret;
    }

    public static int toInt(InputStream in) throws java.io.IOException
    {
        byte[] byte_array_4 = new byte[4];

        byte_array_4[0] = (byte)in.read();
        byte_array_4[1] = (byte)in.read();
        byte_array_4[2] = (byte)in.read();
        byte_array_4[3] = (byte)in.read();

        return toInt(byte_array_4);
    }

    public static String toString(InputStream ins) throws java.io.IOException
    {
        int len = toInt(ins);
        return toString(ins, len);
    }

    private static String toString(InputStream ins, int len) throws java.io.IOException
    {
        String ret = new String();
        for (int i = 0; i < len; i++)
        {
            ret += (char) ins.read();
        }
        return ret;
    }

    public static void toStream(OutputStream os, int i) throws java.io.IOException
    {
        byte [] byte_array_4 = toByteArray(i);
        os.write(byte_array_4);
    }

    public static void toStream(OutputStream os, String s) throws java.io.IOException
    {
        int len_s = s.length();
        toStream(os, len_s);
        for (int i = 0; i < len_s; i++)
        {
            os.write((byte) s.charAt(i));
        }
        os.flush();
    }

    private static byte[] toByteArray(InputStream ins, int an_int) throws java.io.IOException
    {
        byte[] ret = new byte[an_int];

        int offset      = 0;
        int numRead     = 0;
        int outstanding = an_int;

        while ((offset < an_int) && (numRead = ins.read(ret, offset, outstanding)) > 0)
        {
            offset += numRead;
            outstanding = an_int - offset;
        }
        if (offset < ret.length)
        {
            //throw new Exception("Could not completely read from stream, numRead =" + numRead + ", ret.lenght = " + ret.length);
        }
        return ret;
    }

    private static void toFile(InputStream ins, FileOutputStream fos, int len, int buf_size) throws java.io.IOException, java.io.FileNotFoundException
    {
        byte[] buffer = new byte[buf_size];

        int len_read = 0;
        int total_len_read = 0;

        while (total_len_read + buf_size <= len)
        {
            len_read = ins.read(buffer);
            total_len_read += len_read;
            fos.write(buffer, 0, len_read);
        }
        if (total_len_read < len)
        {
            toFile(ins, fos, len - total_len_read, buf_size / 2);
        }
    }

    private static void toFile(InputStream ins, File file, int len) throws java.io.IOException, java.io.FileNotFoundException
    {
        FileOutputStream fos = new FileOutputStream(file);
        toFile(ins, fos, len, 1024);
    }

    public static void toFile (InputStream ins, File file) throws java.io.IOException, java.io.FileNotFoundException
    {
        int len = toInt(ins);
        toFile(ins, file, len);
    }

    public static void toStream(OutputStream os, File file) throws java.io.IOException, java.io.FileNotFoundException
    {
        toStream(os, (int) file.length());

        byte b[] = new byte[1024];
        InputStream is = new FileInputStream(file);
        int numRead = 0;

        while ((numRead = is.read(b)) > 0)
        {
            os.write(b, 0, numRead);
        }
        os.flush();
    }
}

1 个答案:

答案 0 :(得分:0)

要传输的文件的名称(和路径)被指定为File_TX类中main方法的参数。在服务器端(File_RX类),文件将相对于File_RX.class文件的当前目录保存,与上面的输入参数具有相同的相对路径。