2011-10-08 119 views
3

我在java中使用服务器套接字概念来传输文件,如图像和视频。但是当我在客户端收到时,我正在自定义文件名。我可以得到该文件的原始名称吗?服务器套接字文件传输

例如:

如果从服务器端的文件传输“的abc.txt”,我需要此相同的名称在客户端被反射(不通过单独的名称)。

在服务器端:

public class FileServer { 
    public static void main (String [] args) throws Exception { 
    // create socket 
    ServerSocket servsock = new ServerSocket(13267); 
    while (true) { 
     System.out.println("Waiting..."); 

     Socket sock = servsock.accept(); 
     System.out.println("Accepted connection : " + sock); 
     OutputStream os = sock.getOutputStream(); 
    new FileServer().send(os); 
     sock.close(); 
     } 
    } 

    public void send(OutputStream os) throws Exception{ 
     // sendfile 
     File myFile = new File ("C:\\User\\Documents\\abc.png"); 
     byte [] mybytearray = new byte [(int)myFile.length()+1]; 
     FileInputStream fis = new FileInputStream(myFile); 
     BufferedInputStream bis = new BufferedInputStream(fis); 
     bis.read(mybytearray,0,mybytearray.length); 
     System.out.println("Sending..."); 
     os.write(mybytearray,0,mybytearray.length); 
     os.flush(); 
    } 
} 

在客户端:

public class FileClient{ 
    public static void main (String [] args) throws Exception { 


    long start = System.currentTimeMillis(); 


    // localhost for testing 
    Socket sock = new Socket("127.0.0.1",13267); 
    System.out.println("Connecting..."); 
    InputStream is = sock.getInputStream(); 
    // receive file 
    new FileClient().receiveFile(is); 
     long end = System.currentTimeMillis(); 
    System.out.println(end-start); 

    sock.close(); 
    } 

    public void receiveFile(InputStream is) throws Exception{ 
     int filesize=6022386; 
     int bytesRead; 
     int current = 0; 
     byte [] mybytearray = new byte [filesize]; 

     FileOutputStream fos = new FileOutputStream("def"); 
     BufferedOutputStream bos = new BufferedOutputStream(fos); 
     bytesRead = is.read(mybytearray,0,mybytearray.length); 
     current = bytesRead; 


     do { 
      bytesRead = 
       is.read(mybytearray, current, (mybytearray.length-current)); 
      if(bytesRead >= 0) current += bytesRead; 
     } while(bytesRead > -1); 

     bos.write(mybytearray, 0 , current); 
     bos.flush(); 
     bos.close(); 
    } 
} 

回答

2

是的,只是实际的文件内容日前将元数据(在你的情况myFile.getName()),使客户端和服务器读取和发射出的元数据。使用已建立的协议是一个不错的主意,例如HTTP和它的Content-Disposition标题。

+0

但事情是,即时通讯发送这样的文件数组到客户端。我不能将文件的名称追加到文件的字节数组中。如果是,请告诉我如何去做。 – Arun

+0

@Arun当然你可以,你只需要设计一个协议。你使用的是什么序列化方法/协议?在问题中包含实际发送或接收文件的代码(<15行)将非常有帮助。 – phihag

+0

我用完整的源代码编辑了我的问题。这实际上是从此问题中发布的链接中获取的。请帮助我。这么晚才回复很抱歉。 – Arun

4

对于接收器知道文件名,或者:

一)它必须承担它知道的名字,因为这是自找的,

二)服务器首先发送名称流的一部分。

如果发明一种方式,没有真正发送发送信息,让我知道,我们可以成为亿万富翁。我们可以称之为'电脑心灵感应'。

+0

我发送文件作为字节数组到客户端。请注意一件事...它会包含文件的名称还是不包含? – Arun

+0

如果您发送名称,它将包含名称,否则该文件不太可能包含自己的名称。 –

1

有一个更简单的方法来做到这一点。只需在DataInputStream中包装输入流并使用像.writeUTF(myFile.getName())这样的方法。同样,您可以通过在DataInputStream上应用.readUTF来读取接收方的文件名。下面是一个示例代码: 发信人:

FileInputStream fis = new FileInputStream(myFile); 
    BufferedInputStream bis = new BufferedInputStream(fis); 
    DataInputStream dis = new DataInputStream(bis); 
    OutputStream os; 
    try { 
     os = socket.getOutputStream(); 
     DataOutputStream dos = new DataOutputStream(os); 
     dos.writeUTF(myFile.getName()); 
.............////// catch exceptions 

接收机:

InputStream in; 
    try { 
     bufferSize=socket.getReceiveBufferSize(); 
     in=socket.getInputStream(); 
     DataInputStream clientData = new DataInputStream(in); 
     String fileName = clientData.readUTF(); 
     System.out.println(fileName); 
................../////// catch exceptions