java - 服务器/客户端之间的文件传输

标签 java thrift

我应该为“.thrift”文件定义什么样的服务以便以后在我的程序中使用它?

这个文件传输应该在客户端和服务器之间,并且应该是“部分”。

StreamFileService.thrift:

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

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

流文件客户端.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();

}

流文件服务器.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();
}

流文件服务实现:

  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;
}

最佳答案

您的代码在我看来并没有那么糟糕(未经测试)并且没有太多需要更改的地方。

怎么样

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

我还会返回一个保存字节的结构,但这或多或少是我个人的看法。

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

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

FileChunk 结构可以很容易地扩展,如果有必要的话,例如为了返回额外的元数据,比如总大小(特别是如果大小随时间增长/缩小)、剩余字节数、关于数据格式的指示等。您不必这样做,因为如果以后有必要,您可以轻松地扩展接口(interface)。品味问题。

关于java - 服务器/客户端之间的文件传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20960541/

相关文章:

java - java如何在Mac OS上检查“jar launcher.app”正在使用的版本

java - JCheckBox 启用/禁用 JRadioButton

c++ - Thrift 将可选转换为默认或必需

python - 如何处理 Thrift 客户端断开连接问题

c - Thrift:是否可以仅使用 C(GLib)Thrift 库进行序列化?

java - 如何防止二维数组上的字符串重叠?

java - 将带值的字符串与带占位符的字符串进行比较以从中查找值

java - 尝试声明 BottomNavigationView

java - 使用 Apache Thrift 和 TServlet 执行服务多路复用

list - Apache 节俭 : Returning a list/container