java springboot 切割分片上传大文件

公司要做一个上传文件的功能,浏览器上传超过500m的文件会崩溃

需要切割分片上传

前端用的百度的 webuploader插件

官网 http://fex.baidu.com/webuploader/

用的springboot

打开后页面为这样

java springboot 切割分片上传大文件_第1张图片

上传文件,我上传的事一个338m的文件,切割为每个32m的文件

上传后,切割为11份,

java springboot 切割分片上传大文件_第2张图片

上传成功后,合并11文件为一个文件,并将11个文件删除

源码地址:

csdn https://download.csdn.net/download/qq_34874784/10743990

码云:https://gitee.com/c1henye/springboot-study

 

项目结构为

java springboot 切割分片上传大文件_第3张图片

jar 只新增了commons-fileupload这一个jar

pom.xml



    4.0.0

    com.example
    springboot-bigfile
    0.0.1-SNAPSHOT
    jar

    springboot-bigfile
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    
    
        
            maven-ali
            http://maven.aliyun.com/nexus/content/groups/public/
        
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            commons-fileupload
            commons-fileupload
            1.3.3
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



springboot, 默认限制了上传文件最大为1m,需修改,我修改为了50m

FileConfig

package com.example.springbootbigfile;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;

@Configuration
public class FileConfig {

   /**
    * 文件上传配置
    * 
    * @return MultipartConfigElement
    */
   @Bean
   public MultipartConfigElement multipartConfigElement(
         @Value("${multipart.maxFileSize}") String maxFileSize,
         @Value("${multipart.maxRequestSize}") String maxRequestSize) {
      MultipartConfigFactory factory = new MultipartConfigFactory();
      // 单个文件最大
      factory.setMaxFileSize(maxFileSize);
      // 设置总上传数据总大小
      factory.setMaxRequestSize(maxRequestSize);
      return factory.createMultipartConfig();
   }

}

application.properties

#文件上传路径
uploadFolder=D:/data/appfiles/

#单个文件最大
multipart.maxFileSize=3000MB
#设置总上传数据总大小
multipart.maxRequestSize=10000MB

server.port=8088
JsonResult
package com.example.springbootbigfile;

/**
 * 描述: json格式数据返回对象,使用CustomJsonResultSerializer 来序列化
 * @author : lijiazhi
 */
public class JsonResult {
  
    private String code;
    private String msg;
    private T data;

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "code=" + code + " message=" + msg + " data=" + data;
    }

    public static  JsonResult fail() {
       JsonResult ret = new JsonResult();
       ret.setCode("201");
       ret.setMsg("失败");
        return ret;
    }
    
    public static   JsonResult fail(T data) {
          JsonResult ret = JsonResult.fail();
          ret.setData(data);
        return ret;
    }
    
    public static   JsonResult failMessage(String msg) {
          JsonResult ret = JsonResult.fail();
          ret.setMsg(msg);
        return ret;
    }
    public static   JsonResult successMessage(String msg) {
          JsonResult ret = JsonResult.success();
          ret.setMsg(msg);
       return ret;
    }

    public static  JsonResult success() {
       JsonResult ret = new JsonResult();
       ret.setCode("200");
       ret.setMsg("");
        return ret;
    }

    public static  JsonResult success(T data) {
          JsonResult ret = JsonResult.success();
          ret.setData(data);
        return ret;
    }
    
    public static  JsonResult  http404(T data) {
          JsonResult ret = new JsonResult();
          ret.setCode("404");
          ret.setMsg("");
          ret.setData(data);
        return ret;
    }
    
    public static  JsonResult http403(T data) {
          JsonResult ret = new JsonResult();
          ret.setCode("403");
          ret.setMsg("");
          ret.setData(data);
        return ret;
    }

}

UploadController

package com.example.springbootbigfile;

import java.io.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


@Controller
public class UploadController{
   
   @Value("${uploadFolder}")
   private String filePath;

   private  final String MODEL="upload";
   /**
    * 上传文件
    * @param request
    * @param response
    * @param guid
    * @param chunk
    * @param file
    * @param chunks
    */
   @RequestMapping( MODEL+ "/upload")
   public void bigFile(HttpServletRequest request, HttpServletResponse response,String guid,Integer chunk, MultipartFile file,Integer chunks){
      try {  
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
            if (isMultipart) {  
                // 临时目录用来存放所有分片文件  
                String tempFileDir = filePath + guid;  
                File parentFileDir = new File(tempFileDir);  
                if (!parentFileDir.exists()) {  
                    parentFileDir.mkdirs();  
                }  
                // 分片处理时,前台会多次调用上传接口,每次都会上传文件的一部分到后台  
                File tempPartFile = new File(parentFileDir, guid + "_" + chunk + ".part");  
                FileUtils.copyInputStreamToFile(file.getInputStream(), tempPartFile);  
            }  
        } catch (Exception e) {  
           e.printStackTrace();
        }  
   }
   
   /**
    * 合并文件
    * @param guid
    * @param fileName
    * @throws Exception
    */
   @RequestMapping( MODEL+"/merge")
   @ResponseBody
   public JsonResult mergeFile(String guid,String fileName){
       // 得到 destTempFile 就是最终的文件  
      try {
         File parentFileDir = new File(filePath + guid);
         if(parentFileDir.isDirectory()){
            File destTempFile = new File(filePath + "/merge", fileName);
            if(!destTempFile.exists()){
               //先得到文件的上级目录,并创建上级目录,在创建文件,
               destTempFile.getParentFile().mkdir();
               try {
                  //创建文件
                  destTempFile.createNewFile(); //上级目录没有创建,这里会报错
               } catch (IOException e) {
                  e.printStackTrace();
               }
            }
            System.out.println(parentFileDir.listFiles().length);
              for (int i = 0; i < parentFileDir.listFiles().length; i++) {  
                  File partFile = new File(parentFileDir, guid + "_" + i + ".part");
                  FileOutputStream destTempfos = new FileOutputStream(destTempFile, true);
                  //遍历"所有分片文件"到"最终文件"中  
                  FileUtils.copyFile(partFile, destTempfos);  
                  destTempfos.close();  
              }  
              // 删除临时目录中的分片文件  
              FileUtils.deleteDirectory(parentFileDir);
              return JsonResult.success();
         }
      } catch (Exception e) {
         e.printStackTrace();
         return JsonResult.fail();
      }
      return null;
   }

}

 

html




   
   
   
   


   
选择文件

你可能感兴趣的:(java)