发送然后在同一个套接字中接收文件? 【JAVA]

时间:2014-10-17 00:12:22

标签: java sockets

我的客户端服务器配置需要执行以下操作:

  1. 客户端将视频发送到服务器。
  2. 服务器接收视频并进行转换。
  3. 服务器将转换后的视频发送给客户端。
  4. 客户端收到转换后的视频。
  5. 我知道如何传输文件并进行转换,因此前两步已完成。但后来我需要发回视频。天真的方法,这将使服务器看起来像这样:

    //Receive file
    File copy = new File("copypath");
    byte[] bytearray = new byte[10000000];
    InputStream is = socket.getInputStream();
    FileOutputStream fos = new FileOutputStream(copy);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    int bytesRead = is.read(bytearray, 0, bytearray.length);
    int currentTot = bytesRead;
    do {
        bytesRead = is.read(bytearray, currentTot,
                (bytearray.length - currentTot));
        if (bytesRead >= 0)
            currentTot += bytesRead;
    } while (bytesRead > -1);
    bos.write(bytearray, 0, currentTot);
    
    //Convert file
    File converted = convert(copy);
    
    //Send file back:
    bytearray = new byte[(int) converted.length()];
    FileInputStream fin = new FileInputStream(converted);
    BufferedInputStream bin = new BufferedInputStream(fin);
    bin.read(bytearray, 0, bytearray.length);   
    OutputStream os = socket.getOutputStream();
    os.write(bytearray, 0, bytearray.length);
    

    客户端代码为:

    OutputStream os = socket.getOutputStream();
    //System.out.println("Sending file...");
    os.write(bytearray, 0, bytearray.length);
    os.flush();
    
    //  System.out.println("File transfer complete");
    
    //Y recibe
    File copy = new File(convertedName);
    bytearray = new byte[10000000];
    InputStream is = socket.getInputStream();
    FileOutputStream fos = new FileOutputStream(copy);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    int bytesRead = is.read(bytearray,0,bytearray.length);
    int currentTot = bytesRead;
    do{
    bytesRead =
    is.read(bytearray,currentTot,(bytearray.length-currentTot));
    if(bytesRead >= 0)currentTot+=bytesRead;
    } while(bytesRead > -1);
    bos.write(bytearray,0,currentTot);
    

    ......没有用。关于如何进行的任何提示?

0 个答案:

没有答案