服务器/客户端之间的文件传输 [英] File Transport between Server/Client

查看:250
本文介绍了服务器/客户端之间的文件传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该为.thrift文件定义什么样的服务,以便稍后将其用于我的程序?

What kind of Service should I define for ".thrift"-file to use it later for my Program?

此文件传输应位于客户端和服务器,它应该是部分。

This File Transport should be between the Client and the Server and it should be "partly".

StreamFileService.thrift:

StreamFileService.thrift:

struct FileChunk {
1: binary data
2: i64 remaining
}

service StreamFileService {    
FileChunk getBytes(1:string fileName, 2: i64 offset, 3: i32 size);    
}

StreamFileClient.java:

StreamFileClient.java:

public class StreamFileClient {
private int fileChunkSize = 16;
private String filePath;

public String getFilePath() {
    return filePath;
}

public void setFilePath(String filePath) {
    this.filePath = filePath;
}

private void invoke() {

    try {

        TTransport theClientTransport = new TFramedTransport(new TSocket(
                "127.0.0.1", 7911));
        TProtocol theProtocol = new TBinaryProtocol(theClientTransport);
        StreamFileService.Client theClient = new StreamFileService.Client(
                theProtocol);
        theClientTransport.open();

        filePath = "/home/output/output.pdf";
        File theFile2 = new File(filePath);
        theFile2.createNewFile();
        FileInputStream stream = new FileInputStream(theFile2);
        long currentPosition = 0;

        FileChannel theFileChannel = stream.getChannel();
        boolean again = true;

        do {
            FileChunk chunk2 = theClient.getBytes(filePath,
                    currentPosition, fileChunkSize);
            currentPosition += fileChunkSize;

            theFileChannel.write(chunk2.data);

            if (chunk2.remaining == 0)
                again = false;

        } while (again);
        stream.close();

    } catch (TTransportException e) {
        e.printStackTrace();
    } catch (TException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    StreamFileClient theClient = new StreamFileClient();

    theClient.invoke();

}

}

StreamFileServer.java:

StreamFileServer.java:

public class StreamFileServer {

private void start() {
    try {

        TNonblockingServerTransport theServerSocket = new TNonblockingServerSocket(
                7911);
        StreamFileService.Processor theProcessor = new StreamFileService.Processor(
                new StreamFileServiceImpl());
        TServer theServer = new TNonblockingServer(
                new TNonblockingServer.Args(theServerSocket)
                        .processor(theProcessor));
        System.out.println("Server starting on port 7911...");

        theServer.serve();

    } catch (TTransportException e) {
        e.printStackTrace();
    }

}

public static void main(String[] args) {
    StreamFileServer theFileServer = new StreamFileServer();
    theFileServer.start();
}

}

StreamFileServiceImpl:

StreamFileServiceImpl:

  public class StreamFileServiceImpl implements StreamFileService.Iface {

public FileChunk getBytes(String filePath, long offset, int size)
        throws TException {

    File theFile = new File("/home/input/kl_12.pdf");
    FileChunk chunk = new FileChunk();

    try {

        FileOutputStream stream = new FileOutputStream(theFile);

        MappedByteBuffer buffer = stream.getChannel().map(
                FileChannel.MapMode.READ_ONLY, offset, size);
        chunk.data = buffer;
        chunk.remaining = stream.getChannel().size() - offset - size;
        stream.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return chunk;
}

}

推荐答案

您的代码对我来说并不是那么糟糕(未经过测试),并且没有太大的变化。

Your code looks not so bad to me (not tested) and there is not much to change.

怎么样

typedef binary binar
service StreamFileService {    
   binar getBytes(1:string fileName, 2: i64 offset, 3: i32 size);    
   i64 getSize(1:string fileName)
}

我也会回来一个持有字节的结构,但这或多或少是我的个人观点。

I would also return a struct holding the bytes, but that's more or less my personal opinion.

struct FileChunk {
  1: binary data
  2: i64 remaining
}

service StreamFileService {    
   FileChunk getBytes(1:string fileName, 2: i64 offset, 3: i32 size);    
}

FileChunk struct如果有必要,可以很容易地扩展,例如为了返回额外的元数据,例如总大小(特别是如果大小随时间增长/缩小),剩余字节,关于数据格式的指示等。您不必这样做,因为如果以后需要这样做,您可以轻松扩展界面。味道很重要。

The FileChunk struct can be easily extended, if such becomes necessary, e.g. in order to return additional metadata, like total size (especially if the size grows/shrinks over time), remaining bytes, indications about the data format, or the like. You don't have to do that, as you can easily extend the interface if such becomes necessary later. Matter of taste.

这篇关于服务器/客户端之间的文件传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆