java分片上传和下载文件1

warning: 这篇文章距离上次修改已过416天,其中的内容可能已经有所变动。

上传

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);
        }
版权声明 ▶ 本网站名称:我的学习笔记
▶ 本文链接:https://ooolo.net/article/175.html
▶ 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行核实删除。
▶ 转载本站文章需要遵守:商业转载请联系站长,非商业转载请注明出处!!!

none
最后修改于:2023年04月24日 18:42

添加新评论

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png