• 首页
  • 邻居
  • 关于
  • 归档
  • 搜索
  • 夜间模式
    ©2020-2026  我的学习笔记 Theme by OneBlog

    我的学习笔记博客

    搜索
    标签
    # 随笔 # Java # 教程 # openwrt # Mysql # SQL # 爬虫 # post # Js调优 # MAVEN
  • 首页>
  • Java>
  • 正文
  • java分片上传和下载文件1

    2023年04月24日 1.4 k 阅读 0 评论 3828 字

    上传

    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);
            }
    本文著作权归作者 [ admin ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    取消回复

    发表留言
    回复

    首页邻居关于归档
    Copyright©2020-2026  All Rights Reserved.  Load:0.016 s
    京ICP备18019712号
    Theme by OneBlog V3.6.5
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。