上传
File file = new File("D:\\redis.zip");
Long totalSize = file.length();
System.out.println("文件总大小:" + totalSize);
String deleteUrl = "http://8888/SEG/v1/nfs/" + file.getName() + "?op=DELETE";
HttpResponse httpResponse = HttpRequest.delete(deleteUrl)
.header("token", token)
.execute();
Boolean flag = JSONUtil.parseObj(httpResponse.body()).getBool("boolean");
System.out.println(flag ? "删除成功" : "删除失败");
int offset = 0; // 分片偏移量
int chunkSize = 1024 * 1024 * 1; // 每个分片的大小
BufferedInputStream bis = null;
<!--more-->
try {
bis = new BufferedInputStream(new FileInputStream(file));
//拆分成每个为几kb大小的文件
byte[] bytes = new byte[chunkSize];
int length;
// 子文件下标
String uploadUrl = null;
while ((length = bis.read(bytes)) > -1) {
uploadUrl = "http://8888/SEG/v1/nfs/" + file.getName() + "?op=CREATE&offset=" + offset;
httpResponse = null;
if (length < chunkSize) {
byte[] readSize = new byte[length];
System.arraycopy(bytes, 0, readSize, 0, readSize.length);
httpResponse = HttpRequest.put(uploadUrl)
.header("token", token)
.body(readSize)
.execute();
} else {
httpResponse = HttpRequest.put(uploadUrl)
.header("token", token)
.body(bytes)
.execute();
}
String responseBody = httpResponse.body();
offset += length;
System.out.println("Uploaded " + offset + "/" + file.length() + " bytes " + "/" + uploadUrl);
}
String body = bigfiledownloadGETFILESTATUS(file.getName());
JSONObject jsonObject = JSONUtil.parseObj(body);
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(bis);
}
下载:
System.out.println("文件总大小:" + "1854464");
String body = bigfiledownloadGETFILESTATUS("redis.zip");
JSONObject jsonObject = JSONUtil.parseObj(body);
Long totalSize = jsonObject.getJSONObject("FileStatus").getLong("size");
int offset = 0; // 分片偏移量
int chunkSize = 1024 * 1024 * 1; // 每个分片的大小
File file2 = new File("D:\\redis-2.zip");
file2.delete();
RandomAccessFile raf = new RandomAccessFile(file2, "rw");
try {
while (offset < totalSize) {
raf.seek(offset); // 将偏移量设置到指定位置
byte[] bytes = null;
String downloadUrl = null;
int length = Math.min(chunkSize, (int) (totalSize - offset));
downloadUrl = "http://8888/SEG/v1/nfs/" + "redis.zip" + "?op=OPEN&offset=" + offset + "&length="
+ length;
HttpResponse httpResponse = HttpRequest.get(downloadUrl)
.header("token", token)
.execute();
bytes = httpResponse.bodyBytes();
raf.write(bytes); // 将数据写入文件
// 处理当前分片的数据,此处仅示例输出到控制台
offset += length;
System.out.println("Download " + offset + "/" + totalSize + " bytes " + "/" + downloadUrl);
}
} finally {
IOUtils.closeQuietly(raf);
}