前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java分片上传和下载文件1

java分片上传和下载文件1

作者头像
用户9131103
发布2023-07-17 19:53:21
3350
发布2023-07-17 19:53:21
举报
文章被收录于专栏:工作经验

上传

代码语言:javascript
复制
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);
        }

下载:

代码语言:javascript
复制
  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);
        }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023年04月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档