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

我应该为“ .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:

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:

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:

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值