簡體   English   中英

服務器套接字文件傳輸

[英]Server socket file transfer

我在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();
  }
}

要讓接收者知道文件名,請:

a)它必須假設它知道名字,因為它要求它,

b)服務器首先將該名稱作為流的一部分發送。

如果您發明了一種在不實際發送信息的情況下發送信息的方法,請告訴我們,我們可以成為億萬富翁。 我們可以稱之為“計算機心靈感應”。

是的,只需在實際文件內容之前傳輸元數據(在您的情況下為myFile.getName() ),並使客戶端和服務器讀取並發出該元數據。 使用已建立的協議是個好主意,例如HTTP及其Content-Disposition標頭。

有一種更簡單的方法可以做到這一點。 只需將輸入流包裝在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM