java:文件分片上传

news2024/10/6 16:20:14

代码下载地址:https://download.csdn.net/download/u013938578/87358484

1 文件上传、断点续传服务端

1.1 新建maven项目

文件结构如下:

1.2 引入百度开源上传组件webuploader

1.3 前端页面upload.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>webuploader</title>
</head>
<!--引入CSS-->
<link rel="stylesheet" type="text/css" href="webuploader.css">
<script src="jquery-1.11.1.js"></script>
<script src="webuploader.js"></script>
<style>
    #upload-container, #upload-list{width: 500px; margin: 0 auto; }
    #upload-container{cursor: pointer; border-radius: 15px; background: #EEEFFF; height: 200px;}
    #upload-list{height: 800px; border: 1px solid #EEE; border-radius: 5px; margin-top: 10px; padding: 10px 20px;}
    #upload-container>span{widows: 100%; text-align: center; color: gray; display: block; padding-top: 15%;}
    .upload-item{margin-top: 5px; padding-bottom: 5px; border-bottom: 1px dashed gray;}
    .percentage{height: 5px; background: green;}
    .btn-delete, .btn-retry{cursor: pointer; color: gray;}
    .btn-delete:hover{color: orange;}
    .btn-retry:hover{color: green;}
</style>
<!--引入JS-->
<body>
<div id="upload-container">
    <span>点击或将文件拖拽至此上传</span>
</div>
<div id="upload-list">
</div>
<button id="picker" style="display: none;">点击上传文件</button>
</body>

<script>
    $('#upload-container').click(function(event) {
        $("#picker").find('input').click();
    });
    var uploader = WebUploader.create({
        auto: true,// 选完文件后,是否自动上传。
        swf: 'Uploader.swf',// swf文件路径
        server: 'http://localhost:8080/upload',// 文件接收服务端。
        dnd: '#upload-container',
        pick: '#picker',// 内部根据当前运行是创建,可能是input元素,也可能是flash. 这里是div的id
        multiple: true, // 选择多个
        chunked: true,// 开启分片上传。
        threads: 20, // 上传并发数。允许同时最大上传进程数。
        method: 'POST', // 文件上传方式,POST或者GET。
        fileSizeLimit: 1024*1024*1024*10, //验证文件总大小是否超出限制, 超出则不允许加入队列。
        fileSingleSizeLimit: 1024*1024*1024, //验证单个文件大小是否超出限制, 超出则不允许加入队列。
        fileVal:'upload' // [默认值:'file'] 设置文件上传域的name。
    });

    uploader.on("beforeFileQueued", function(file) {
        console.log(file); // 获取文件的后缀
    });

    uploader.on('fileQueued', function(file) {
        // 选中文件时要做的事情,比如在页面中显示选中的文件并添加到文件列表,获取文件的大小,文件类型等
        console.log(file.ext); // 获取文件的后缀
        console.log(file.size);// 获取文件的大小
        console.log(file.name);
        var html = '<div class="upload-item"><span>文件名:'+file.name+'</span><span data-file_id="'+file.id+'" class="btn-delete">删除</span><span data-file_id="'+file.id+'" class="btn-retry">重试</span><div class="percentage '+file.id+'" style="width: 0%;"></div></div>';
        $('#upload-list').append(html);
        uploader.md5File( file )//大文件秒传

        // 及时显示进度
            .progress(function(percentage) {
                console.log('Percentage:', percentage);
            })

            // 完成
            .then(function(val) {
                console.log('md5 result:', val);
            });
    });

    uploader.on('uploadProgress', function(file, percentage) {
        console.log(percentage * 100 + '%');
        var width = $('.upload-item').width();
        $('.'+file.id).width(width*percentage);
    });

    uploader.on('uploadSuccess', function(file, response) {
        console.log(file.id+"传输成功");
    });

    uploader.on('uploadError', function(file) {
        console.log(file);
        console.log(file.id+'upload error')
    });

    $('#upload-list').on('click', '.upload-item .btn-delete', function() {
        // 从文件队列中删除某个文件id
        file_id = $(this).data('file_id');
        // uploader.removeFile(file_id); // 标记文件状态为已取消
        uploader.removeFile(file_id, true); // 从queue中删除
        console.log(uploader.getFiles());
    });

    $('#upload-list').on('click', '.btn-retry', function() {
        uploader.retry($(this).data('file_id'));
    });

    uploader.on('uploadComplete', function(file) {
        console.log(uploader.getFiles());
    });
</script>
</html>

1.4 上传代码

package org.example.controller;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@Controller
public class UploadController {
    
    private final static String utf8 ="utf-8";
    @RequestMapping("/upload")
    @ResponseBody
    public void upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //分片
        response.setCharacterEncoding(utf8);

        //当前分片数
        Integer schunk = null;

        //总分片数
        Integer schunks = null;

        //文件名字
        String name = null;

        //上传文件的位置
        String uploadPath = "F:\\fileItem";
        BufferedOutputStream os = null;

        try {
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //设置缓存大小
            factory.setSizeThreshold(1024);
            //临时目录
            factory.setRepository(new File(uploadPath));

            ServletFileUpload upload = new ServletFileUpload(factory);
            //设置单个文件的大小
            upload.setFileSizeMax(5l *1024l *1024l*1024l);
            //设置总体的大小
            upload.setSizeMax(10l *1024l *1024l*1024l);

            List<FileItem> items = upload.parseRequest(request);

            for(FileItem item : items){
                //判断是不是文件对象
                if(item.isFormField()){
                    if("chunk".equals(item.getFieldName())){
                        schunk = Integer.parseInt(item.getString(utf8));
                    }
                    if("chunks".equals(item.getFieldName())){
                        schunks = Integer.parseInt(item.getString(utf8));
                    }
                    if("name".equals(item.getFieldName())){
                        name = item.getString(utf8);
                    }
                }
            }

            for(FileItem item : items){
                if(!item.isFormField()){
                    //如果文件没有分片直接存储
                    String temFileName = name;
                    if(name != null){
                        //如果文件已经分片,则进行分片保存
                        if(schunk != null){
                            temFileName = schunk +"_"+name;
                        }
                        //查看分片文件是否存在,存在则不上传
                        File temFile = new File(uploadPath,temFileName);
                        if(!temFile.exists()){//断点续传
                            item.write(temFile);
                        }
                    }
                }
            }

            //文件合并
            if(schunk != null && schunk.intValue() == schunks.intValue()-1){
                File tempFile = new File(uploadPath,name);
                os = new BufferedOutputStream(new FileOutputStream(tempFile));

                for(int i=0 ;i<schunks;i++){
                    File file = new File(uploadPath,i+"_"+name);
                    while(!file.exists()){
                        Thread.sleep(100);
                    }
                    byte[] bytes = FileUtils.readFileToByteArray(file);
                    os.write(bytes);
                    os.flush();
                    file.delete();
                }
                os.flush();
            }
            response.getWriter().write("上传成功"+name);

        }finally {
            try{
                if(os != null){
                    os.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }

    }
}

1.5 测试

上传文件

查看上传文件的目录,发现文件被分片上传

上传结束以后,会对文件进行合并: 

2 文件下载服务端

代码如下:

package org.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@Controller
public class DownLoadController {

    private final static String utf8 ="utf-8";
    @RequestMapping("/download")
    public void downLoadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //文件路径可以通过前台传过来,着了为了方便,直接写死
        File file = new File("F:\\fileItem\\性能调优专题-Mysql索引优化与底层数据结构深入剖析-0312.mp4");
        response.setCharacterEncoding(utf8);
        InputStream is = null;
        OutputStream os = null;
        try{
            //分片下载   http  Range bytes=100-1000   bytes=100-
            long fSize = file.length();
            response.setContentType("application/x-download");
            String fileName = URLEncoder.encode(file.getName(),utf8);
            response.addHeader("Content-Disposition","attachment;filename=" + fileName);
            response.setHeader("Accept-Range","bytes");

            response.setHeader("fSize",String.valueOf(fSize));
            response.setHeader("fName",fileName);
            //文件的起始位置,文件大小,读取的大小
            long pos = 0,last = fSize-1,sum = 0;
            //判断是否有分片信息
            if(null != request.getHeader("Range")){
                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);

                String numRange = request.getHeader("Range").replaceAll("bytes=","");
                //Range格式为100-1000
                String[] strRange = numRange.split("-");
                //判断文件大小
                if(strRange.length == 2){
                    pos = Long.parseLong(strRange[0].trim());
                    last = Long.parseLong(strRange[1].trim());
                    //判断结束字节是否超出文件大小
                    if(last > fSize-1){
                        last = fSize-1;
                    }
                }else{
                    pos = Long.parseLong(numRange.replaceAll("-","").trim());
                }
            }
            long rangeLenght = last - pos +1;
            String contentRange = new StringBuffer("bytes ").append(pos).append("-").append(last).append("/").append(fSize).toString();
            response.setHeader("Content-Range",contentRange);
            response.setHeader("Content-Lenght",String.valueOf(rangeLenght));

            os = new BufferedOutputStream(response.getOutputStream());
            is = new BufferedInputStream(new FileInputStream(file));
            is.skip(pos);
            byte[] buffer = new byte[1024];
            int lenght = 0;
            while(sum < rangeLenght){
                lenght = is.read(buffer,0,((rangeLenght-sum) <= buffer.length ? ((int)(rangeLenght-sum)) :  buffer.length));
                sum = sum+ lenght;
                os.write(buffer,0,lenght);
            }
            System.out.println("下载完成");
        }finally {
            if(is != null){
                is.close();
            }
            if(os != null){
                os.close();
            }
        }
    }

}

注意:

web端有沙箱问题,无法直接通过web端进行下载时的断点续传,续传需要通过客户端进行,下一章会进行讲解

3 文件下载客户端

新建FileInfo实体类

package org.example.client;

public class FileInfo{
    long fSize;
    String fName;

    public FileInfo(long fSize, String fName) {
        this.fSize = fSize;
        this.fName = fName;
    }
}

新建FileDownload类

package org.example.client;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;

public class FileDownload {

    private final static long PER_PAGE = 1024l *1024l * 50l;
    private final static String DOWNPATH = "E:\\fileItem";

    public static FileInfo download(long start,long end,long page,String fName) throws Exception {
        File file = new File(DOWNPATH,page+"-"+fName);
        if(file.exists()){
            return null;
        }
        HttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/download");
        httpGet.setHeader("Range","bytes="+start+"-"+end);

        HttpResponse response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();

        String fSize = response.getFirstHeader("fSize").getValue();
        fName = URLDecoder.decode(response.getFirstHeader("fName").getValue(),"utf-8");

        FileOutputStream fis = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int ch =0;
        while((ch = is.read(buffer)) != -1){
            fis.write(buffer,0,ch);
        }
        is.close();
        fis.flush();
        fis.close();

        if(end - Long.valueOf(fSize) >= 0){//最后一个分片
            mergeFile(fName,page);
        }
        return new FileInfo(Long.valueOf(fSize),fName);
    }

    private static void mergeFile(String fName, long page) throws Exception {
        File tempFile = new File(DOWNPATH,fName);
        BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(tempFile));

        for(int i=0 ;i<=page;i++){
            File file = new File(DOWNPATH,i+"-"+fName);
            while(!file.exists() || (i != page && file.length() < PER_PAGE)){
                Thread.sleep(100);
            }
            byte[] bytes = FileUtils.readFileToByteArray(file);
            os.write(bytes);
            os.flush();
            file.delete();
        }
        File file = new File(DOWNPATH,-1+"-null");
        file.delete();
        os.flush();
        os.close();
        //文件子节计算导致文件不完整
        //流未关闭
    }
}

新建download类集成Runnable

分片下载需要使用线程来进行,代码如下:

package org.example.client;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLDecoder;

public class Download implements Runnable {
    private final static long PER_PAGE = 1024l *1024l * 50l;
    private final static String DOWNPATH = "E:\\fileItem";
    long start;
    long end;
    long page;
    String fName;

    public Download(long start, long end, long page, String fName) {
        this.start = start;
        this.end = end;
        this.page = page;
        this.fName = fName;
    }

    public void run() {
        try {
            FileInfo info = FileDownload.download(start, end, page, fName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

编写下载客户端类

package org.example.client;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.net.URLDecoder;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
public class DownloadClient {

    private final static long PER_PAGE = 1024l *1024l * 50l;
    private final static String DOWNPATH = "E:\\fileItem";
    ExecutorService pool = Executors.newFixedThreadPool(10);

    @RequestMapping("/downloadFile")
    public String downloadFile() throws Exception {
        FileInfo fileInfo = FileDownload.download( 0, 10, -1, null);
        //总分片数量
        long pages = fileInfo.fSize / PER_PAGE;
        for(long i=0;i<=pages; i++){
            pool.submit(new Download(i*PER_PAGE,(i+1)*PER_PAGE-1,i,fileInfo.fName));
        }

        return "success";
    }

}

运行结果如下:

结果显示为分片下载

分片下载完成以后会进行分片合并

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/134282.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java个人家乡博客源码

概述 个人博客相册家乡主题&#xff0c;用户注册后可以发布关于家乡的特色文章介绍&#xff0c;可以发布照片&#xff0c;相册管理&#xff0c;留言&#xff0c;评论&#xff0c;回复&#xff0c;收藏&#xff0c;关注 演示视频 https://www.bilibili.com/video/BV1iy4y1x7w6…

SSM框架-注解开发

11 注解开发 11.1 注解开发定义Bean 代码 接口BookDao public interface BookDao {void save(); }实现类BookDaoImpl【更改之处】 在最上面Component(“bookDao”) Component("bookDao") //或者Repository("bookDao") public class BookDaoImpl impl…

【C++】-- 哈希(上万字详细配图配代码从执行一步步讲解)

目录 哈希 常见哈希函数 除留余数法 哈希冲突 哈希冲突解决 闭散列 a、线性探测 插入 查找 删除 线性探测的实现代码 b、二次探测 二次探测的实现 开散列 开散列实现 插入 查找 删除 析构函数 代码汇总 哈希 常见哈希函数 直接定址法 -- (常用)-- 不存在哈…

2022 年博客总结

时间过的飞快&#xff0c;孩子也快4岁了&#xff0c;1号带孩子去玩雪&#xff0c;发生了一件有趣的事&#xff0c;发个视频。 带孩子玩雪我拉着闺女&#xff0c;闺女拉着儿子&#xff0c;忽略了力的作用&#xff0c;我以为只有我在使劲&#xff0c;实际上闺女需要需要更大的力拉…

java8新特性——函数式编程

文章目录1.函数式编程思想1.1概念1.2函数式编程的思想2.Lambda表达式2.1概述2.2核心原则2.3基本格式2.4Lambda表达式练习2.5省略规则3.Stream流3.1概述3.2案例准备3.3Steam流操作案例3.3.1需求3.3.2实现3.4Stream常用操作3.4.1创建stream流方式3.4.2中间操作3.4.2.1filter3.4.2…

SpringMVC 底层机制的简易实现

SpringMVC 底层机制的简易实现项目基础配置 xml 文件开发指南开发步骤1.初始化数据2.中央控制器 - 分发请求3.开发者角度4.视图解析器开发总结项目基础 <dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId&g…

【Python基础】常用数据结构及处理

1. KeyValue dict.clear() 删除字典内所有元素dict.copy()返回一个字典的浅复制[dict.fromkeys(seq, val])创建一个新字典&#xff0c;以序列 seq 中元素做字典的键&#xff0c;val 为字典所有键对应的初始值 dict.get(key, defaultNone) 返回指定键的值&#xff0c;如果值不在…

prompt模型详解之文本生成

prompt在生成方面的应用从两个方面进行介绍&#xff1a; 评估手段 具体任务 评估手段 生成任务的评估手段主要分为四种类型&#xff1a; 1). 基于N-gram匹配 2). 基于编辑距离 3). 基于词向量 4). 基于可学习方式。 本小节主要介绍BARTSCORE&#xff0c;其使用prompt方…

Python杂题

目录 一、前言 二、例题1——修剪灌木 三、例题2—— 付账问题 四、例题3——最少砝码 五、例题四——矩形拼接 六、例题五——蜂巢 一、前言 竞赛题有很多不需要什么算法的题目&#xff0c;只要学过编程语言就能做&#xff0c;其考核思维、逻辑、编码能力。而这种题有“…

【算法题解】 8. K 个一组翻转链表

文章目录题目解题思路代码实现复杂度分析题目 给你链表的头节点 head &#xff0c;每 k 个节点一组进行翻转&#xff0c;请你返回修改后的链表。 k 是一个正整数&#xff0c;它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍&#xff0c;那么请将最后剩余的节点保持…

Python中编码(encode)解码(decode)讲解

嗨害大家好鸭&#xff01;我是小熊猫~ 这次也是给大家带来一点干货~ 所用素材:点击此处跳转文末名片获取 一、python3中str与unicode 在python3中&#xff0c;字符串有两种形式&#xff1a;str和bytes&#xff0c;两者区别如下&#xff1a; unicode string(str类型)&#xf…

【经验】关于区分cin、getline、cin.getline三种字符串输入的区别

cin 既可以输入char[]数组&#xff0c;也可以输入string类型&#xff0c;输入会被空格打断 cin对char进行输入 #include<bits/stdc.h> using namespace std; int main(){char ch[50];cin>>ch;cout<<strlen(ch)<<endl;for(int i0;i<strlen(ch);i){…

1.移动机器人发展现状

移动机器人主要应用场景&#xff1a; 场景1.仓储机器人(AGV自动导引运输车)&#xff1a;电商企业用户下单后机器人可以实现自动分拣和发货。需要多个传感器配合 2.自动驾驶领域(AMR自主移动机器人):车辆避让行人、导航等 热点研究领域&#xff1a; 环境感知和建模、人机交互…

2022简要总结和2023行动指南

在这辞旧迎接之际&#xff0c;心存感恩&#xff0c;放眼未来。 祝宝妈妈宝&#xff0c;幸福快乐&#xff1b; 祝国泰民安&#xff0c;政通人和。 祝百融云创系&#xff0c;生意兴隆&#xff1b; 祝公司老板们&#xff0c;大展宏图&#xff1b; 祝同事同行er&#xff0c;身…

Java算法_LeetCode:旋转数组

旋转数组 给你一个数组&#xff0c;将数组中的元素向右轮转 k 个位置&#xff0c;其中 k 是非负数。 示例 1: 输入: nums [1,2,3,4,5,6,7], k 3 输出: [5,6,7,1,2,3,4] 解释: 向右轮转 1 步: [7,1,2,3,4,5,6] 向右轮转 2 步: [6,7,1,2,3,4,5] 向右轮转 3 步: [5,6,7,1,2,3,…

Fastsapi的小疑问

1. Fastapi中的get和post区别是什么&#xff1f; 答&#xff1a;get参数传输暴露在外&#xff0c;post隐式传输 GET参数获取&#xff1a;获取一个URL后面带?param11&param22这种形式。 特点&#xff1a;URL上直接编辑传输&#xff0c;方便快捷&#xff0c;但是信息暴露在…

【nowcoder】笔试强训Day16

目录 一、选择题 二、编程题 2.1扑克牌大小 2.2完全数计算 一、选择题 1.在关系型是数据库中&#xff0c;有两个不同的事务同时操作数据库中同一表的同一行&#xff0c;不会引起冲突的是&#xff1a; A. 其中一个DELETE操作&#xff0c;一个是SELECT操作 B. 其中两个都是…

植物大战僵尸:代码实现无限阳光

通过逆向分析植物阳光数量的动态地址找到阳光的基址与偏移&#xff0c;从而实现每次启动游戏都能够使用基址加偏移的方式定位阳光数据&#xff0c;最后我们将通过使用C语言编写通用辅助实现简单的无限阳光辅助&#xff0c;在教程开始之前我们先来说一下为什么会有动态地址与基址…

光缆单盘检测与光缆线路测试需使用双窗口吗?

1 引言 光缆线路和宽带接入工程中&#xff0c;通常会涉及光缆单盘检测与光缆线路的测试工作&#xff0c;光缆线路测试包括&#xff1a;中继段测试、用户光缆测试等。这些测试条目&#xff0c;有的只需采用测试仪表的1个波长进行测试&#xff0c;即单窗口测试&#xff0c;有的则…

代码随想录算法训练营第2天 977. 有序数组的平方、209. 长度最小的子数组

代码随想录算法训练营第2天| 977. 有序数组的平方、209. 长度最小的子数组 有序数组的平方 力扣题目链接(opens new window) 给你一个按 非递减顺序 排序的整数数组 nums&#xff0c;返回 每个数字的平方 组成的新数组&#xff0c;要求也按 非递减顺序 排序。 数组其实是有…