Java 如何随文件一起发送文件信息?

Java 如何随文件一起发送文件信息?,java,sockets,Java,Sockets,我正在编写一个服务器和客户端文件传输模块,我想知道是否可以将文件信息与文件本身一起发送。特别是我需要将服务器上的文件的文件名和文件夹结构发送到客户端。 例如,如果我在服务器上有c:\abc\efg\a.txt,我希望在客户端有。\abc\efg\a.txt 这是我正在使用的代码: 服务器端文件发送: Socket clientSocket=new Socket("Some IP",12345); OutputStream out=clientSocket.getOutputSt

我正在编写一个服务器和客户端文件传输模块,我想知道是否可以将文件信息与文件本身一起发送。特别是我需要将服务器上的文件的文件名和文件夹结构发送到客户端。 例如,如果我在服务器上有c:\abc\efg\a.txt,我希望在客户端有。\abc\efg\a.txt

这是我正在使用的代码:

服务器端文件发送:

    Socket clientSocket=new Socket("Some IP",12345);

    OutputStream out=clientSocket.getOutputStream();

    FileInputStream fis=new FileInputStream(file);
    int x=0;
    byte[] b = new byte[4194304];
    while(true){
        x=fis.read(b);
        if(x==-1)break;
        out.write(b);
   }
    out.close();
客户端侦听器:

try {
        ServerSocket ss=new ServerSocket(12345);
        while(true){
            final Socket client = ss.accept();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        InputStream in = client.getInputStream();
                        FileOutputStream fos = new FileOutputStream("rec.txt");
                        int x=0;
                        byte[] b = new byte[4194304];
                        while(true){
                            x=in.read(b);
                            if(x==-1)break;
                            fos.write(b);
                        }
                        fos.close();
                    }
                    catch(Exception e){
                    }
                }
            }).start();
        }

    } catch (Exception e) {
    }

首先发送包含所需信息的标题,然后发送分隔符(如0),然后发送文件内容。服务器端读取标题,直到使用分隔符,然后是内容。

首先发送包含所有所需信息的标题,然后发送分隔符(例如0),然后发送文件内容。服务器端读取标题直到分隔符,然后读取内容。

我以前已经这样做过。我在服务器上使用DataOutputStream,在客户端使用DataInputStream

我使用了这个协议:
1.发送文件名.writeUTF(文件名)
2.发送文件大小.writeLong(fileSize)
3.发送文件。writeBytes(byteArray);//这是在for循环中完成的,因为文件大小可能太大,无法立即放入内存。服务器和客户端大小都将使用文件大小来确定何时停止


客户端将使用相同的协议,但不是“写入”,而是“读取”。

我以前已经这样做过。我在服务器上使用DataOutputStream,在客户端使用DataInputStream

我使用了这个协议:
1.发送文件名.writeUTF(文件名)
2.发送文件大小.writeLong(fileSize)
3.发送文件。writeBytes(byteArray);//这是在for循环中完成的,因为文件大小可能太大,无法立即放入内存。服务器和客户端大小都将使用文件大小来确定何时停止


客户端将使用相同的协议,但不是“写”,而是“读”。

您需要先发送文件路径,然后发送文件名,使用64字节缓冲区来完成此操作,一旦获得路径和名称,您就可以读取文件内容

例如:

   // Server
   try
   {
       Socket clientSocket=new Socket("Some IP",12345);
       OutputStream out=clientSocket.getOutputStream();
       FileInputStream fis=new FileInputStream(file);
       byte[] info = new byte[64];
       int len = file.getPath().length();
       info = file.getPath().getBytes();
       for (int i=len; i < 64; i++) info[i]=0x00;
       out.write(info, 0, 64);
       len = file.getName().length();
       info = file.getName().getBytes();
       for (int i=len; i < 64; i++) info[i]=0x00;
       out.write(info, 0, 64);

       int x;
       byte[] b = new byte[4194304];
       while((x=fis.read(b)) > 0)
       {
         out.write(b, 0, x);
       }
      out.close();
      fis.close();
  }
  catch (Exception e)
  {
      e.printStackTrace();
  }

  // Client
  try
  {
      InputStream in = client.getInputStream();
      FileOutputStream fos = new FileOutputStream("rec.txt");
      byte[] path = new byte[64];
      in.read(path, 0, 64);
      byte[] name = new byte[64];
      in.read(name, 0, 64);

      int x=0;
      byte[] b = new byte[4194304];
      while((x = in.read(b)) > 0)
      {
          fos.write(b, 0, x);
      }
      fos.close();
  }
  catch(Exception e)
  {
     e.printStackTrace();
  }
//服务器
尝试
{
Socket clientSocket=新套接字(“某些IP”,12345);
OutputStream out=clientSocket.getOutputStream();
FileInputStream fis=新的FileInputStream(文件);
字节[]信息=新字节[64];
int len=file.getPath().length();
info=file.getPath().getBytes();
对于(inti=len;i<64;i++)信息[i]=0x00;
输出。写入(信息,0,64);
len=file.getName().length();
info=file.getName().getBytes();
对于(inti=len;i<64;i++)信息[i]=0x00;
输出。写入(信息,0,64);
int x;
字节[]b=新字节[4194304];
而((x=fis.read(b))>0)
{
写出(b,0,x);
}
out.close();
fis.close();
}
捕获(例外e)
{
e、 printStackTrace();
}
//客户
尝试
{
InputStream in=client.getInputStream();
FileOutputStream fos=新的FileOutputStream(“rec.txt”);
字节[]路径=新字节[64];
in.read(路径0,64);
字节[]名称=新字节[64];
in.read(名称,0,64);
int x=0;
字节[]b=新字节[4194304];
而((x=in.read(b))>0)
{
fos.写入(b,0,x);
}
fos.close();
}
捕获(例外e)
{
e、 printStackTrace();
}

您需要先发送文件路径,然后发送文件名,使用64字节的缓冲区,一旦获得路径和名称,就可以读取文件内容

例如:

   // Server
   try
   {
       Socket clientSocket=new Socket("Some IP",12345);
       OutputStream out=clientSocket.getOutputStream();
       FileInputStream fis=new FileInputStream(file);
       byte[] info = new byte[64];
       int len = file.getPath().length();
       info = file.getPath().getBytes();
       for (int i=len; i < 64; i++) info[i]=0x00;
       out.write(info, 0, 64);
       len = file.getName().length();
       info = file.getName().getBytes();
       for (int i=len; i < 64; i++) info[i]=0x00;
       out.write(info, 0, 64);

       int x;
       byte[] b = new byte[4194304];
       while((x=fis.read(b)) > 0)
       {
         out.write(b, 0, x);
       }
      out.close();
      fis.close();
  }
  catch (Exception e)
  {
      e.printStackTrace();
  }

  // Client
  try
  {
      InputStream in = client.getInputStream();
      FileOutputStream fos = new FileOutputStream("rec.txt");
      byte[] path = new byte[64];
      in.read(path, 0, 64);
      byte[] name = new byte[64];
      in.read(name, 0, 64);

      int x=0;
      byte[] b = new byte[4194304];
      while((x = in.read(b)) > 0)
      {
          fos.write(b, 0, x);
      }
      fos.close();
  }
  catch(Exception e)
  {
     e.printStackTrace();
  }
//服务器
尝试
{
Socket clientSocket=新套接字(“某些IP”,12345);
OutputStream out=clientSocket.getOutputStream();
FileInputStream fis=新的FileInputStream(文件);
字节[]信息=新字节[64];
int len=file.getPath().length();
info=file.getPath().getBytes();
对于(inti=len;i<64;i++)信息[i]=0x00;
输出。写入(信息,0,64);
len=file.getName().length();
info=file.getName().getBytes();
对于(inti=len;i<64;i++)信息[i]=0x00;
输出。写入(信息,0,64);
int x;
字节[]b=新字节[4194304];
而((x=fis.read(b))>0)
{
写出(b,0,x);
}
out.close();
fis.close();
}
捕获(例外e)
{
e、 printStackTrace();
}
//客户
尝试
{
InputStream in=client.getInputStream();
FileOutputStream fos=新的FileOutputStream(“rec.txt”);
字节[]路径=新字节[64];
in.read(路径0,64);
字节[]名称=新字节[64];
in.read(名称,0,64);
int x=0;
字节[]b=新字节[4194304];
而((x=in.read(b))>0)
{
fos.写入(b,0,x);
}
fos.close();
}
捕获(例外e)
{
e、 printStackTrace();
}
Zip
1.文件
2.有关文件的信息
发送它)

Zip
1.文件
2.有关文件的信息

发送)

当然可以,但您需要为其创建更复杂的协议。或者,您可以使用许多已经做类似事情(如HTTP或FTP)的服务之一。谢谢,但我正在寻找一些本机支持,我知道所有这些选择,但它们是我最后的选择。没有此类本机支持。当然,除非您使用网络文件系统,但这里不是这样。当然这是可能的,但您需要为它创建一个更复杂的协议。或者,您可以使用许多已经做类似事情(如HTTP或FTP)的服务之一。谢谢,但我正在寻找一些本机支持,我知道所有这些选择,但它们是我最后的选择。没有此类本机支持。当然,除非您使用网络文件系统,但这里不是这种情况。这还要求您首先发送头/内容的长度。这还要求您首先发送头/内容的长度。