码迷,mamicode.com
首页 > Web开发 > 详细

大文件分块上传

时间:2018-12-31 23:45:33      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:spl   on()   content   flush   return   nbu   ica   nts   layer   

1.服务端SpringBoot

@WebServlet(name = "vectorUpload", urlPatterns = "/gis/vectorUpload", initParams = {
        @WebInitParam(name = "upload_path", value = "D:\\dataTest") })
public class VectorUploadServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
     
    private String fileUploadPath;

    @Override
    public void init(ServletConfig config) {
        fileUploadPath = config.getInitParameter("upload_path");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if (request.getParameter("getfile") != null && !request.getParameter("getfile").isEmpty()) {
            File file = new File(fileUploadPath, request.getParameter("getfile"));
            if (file.exists()) {
                int bytes = 0;
                ServletOutputStream op = response.getOutputStream();
                response.setContentType(getMimeType(file));
                response.setContentLength((int) file.length());
                response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

                byte[] bbuf = new byte[1024];
                DataInputStream in = new DataInputStream(new FileInputStream(file));

                while ((in != null) && ((bytes = in.read(bbuf)) != -1)) {
                    op.write(bbuf, 0, bytes);
                }

                in.close();
                op.flush();
                op.close();
            }
        } else if (request.getParameter("delfile") != null && !request.getParameter("delfile").isEmpty()) {
            File file = new File(fileUploadPath, request.getParameter("delfile"));
            if (file.exists()) {
                file.delete(); 
            }
        } else if (request.getParameter("getthumb") != null && !request.getParameter("getthumb").isEmpty()) {
            File file = new File(fileUploadPath, request.getParameter("getthumb"));
            if (file.exists()) {
                String mimetype = getMimeType(file);
                if (mimetype.endsWith("png") || mimetype.endsWith("jpeg") || mimetype.endsWith("gif")) {
                    BufferedImage im = ImageIO.read(file);
                    if (im != null) {
                        BufferedImage thumb = Scalr.resize(im, 75);
                        ByteArrayOutputStream os = new ByteArrayOutputStream();
                        if (mimetype.endsWith("png")) {
                            ImageIO.write(thumb, "PNG", os);
                            response.setContentType("image/png");
                        } else if (mimetype.endsWith("jpeg")) {
                            ImageIO.write(thumb, "jpg", os);
                            response.setContentType("image/jpeg");
                        } else {
                            ImageIO.write(thumb, "GIF", os);
                            response.setContentType("image/gif");
                        }
                        ServletOutputStream servletOutputStream = response.getOutputStream();
                        response.setContentLength(os.size());
                        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
                        os.writeTo(servletOutputStream);
                        servletOutputStream.flush();
                        servletOutputStream.close();
                    }
                }
            } 
        } else {
            PrintWriter writer = response.getWriter();
            writer.write("call POST with multipart form data");
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //
        if (!ServletFileUpload.isMultipartContent(request)) {
            throw new IllegalArgumentException(
                    "Request is not multipart, please ‘multipart/form-data‘ enctype for your form.");
        }
        //
        PrintWriter printWriter = response.getWriter();
        response.setContentType("application/json");
        List<UploadResult> uploadResultList = new LinkedList<UploadResult>();
        //        
        String contentRange = request.getHeader("content-range");
        String contentDisposition = request.getHeader("content-disposition");
        
        try {
            ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
            List<FileItem> fileItemList = servletFileUpload.parseRequest(request);
            if (StringUtils.isNotEmpty(contentRange) && StringUtils.isNotEmpty(contentDisposition)) {
                
                String fileName = contentDisposition.split("\"")[1];
                long blockIndex = Long.parseLong(request.getHeader("block-index"));
                String[] blockDone = request.getHeader("block-done").split(",");
                long blockTotal = Long.parseLong(request.getHeader("block-total"));

                //
                for (FileItem fileItem : fileItemList) {
                    if (!fileItem.isFormField()) {
                        File file = new File(fileUploadPath, fileItem.getName() + ".tmp." + blockIndex);
                        fileItem.write(file);
                        UploadResult uploadResult = new UploadResult();
                        uploadResult.setName(fileItem.getName() + ".tmp." + blockIndex);
                        uploadResult.setSize(fileItem.getSize());
                        uploadResult.setUrl("upload?getfile=" + fileItem.getName());
                        uploadResult.setThumbnailUrl("upload?getthumb=" + fileItem.getName());
                        uploadResult.setDeleteUrl("upload?delfile=" + fileItem.getName());                        
                        uploadResultList.add(uploadResult);
                    } else {
                        System.out.println("一般表单-->     " + fileItem.getFieldName() + "," + fileItem.getString());
                    }
                }
                //
                boolean canMerge = canUnionFiles(blockDone, blockTotal, blockIndex);
                System.out.println("canUnion-->     " + canMerge);
                if (canMerge) {
                    String[] filePaths = new String[(int) blockTotal];
                    for (int i = 0; i < blockTotal; i++) {
                        filePaths[i] = fileUploadPath + File.separator + fileName + ".tmp." + (i + 1);
                    }
                    String resultPath = fileUploadPath + File.separator + fileName;
                    boolean success = FileUtil.mergeFiles(filePaths, resultPath, true);
                    
                    if(resultPath.endsWith(".tar.gz")) {                        
                        FileUtil.unTarGZip(resultPath, fileUploadPath);
                    }
                    else if(resultPath.endsWith(".zip")) {
                        FileUtil.unZip(resultPath, fileUploadPath);
                    }                    
                    System.out.println("success:   " + success);
                }
                //

            } else {
                //
                for (FileItem fileItem : fileItemList) {
                    if (!fileItem.isFormField()) {
                        File file = new File(fileUploadPath, fileItem.getName());
                        fileItem.write(file);
                        UploadResult uploadResult = new UploadResult();
                        uploadResult.setName(fileItem.getName());
                        uploadResult.setSize(fileItem.getSize());
                        uploadResult.setUrl("upload?getfile=" + fileItem.getName());
                        uploadResult.setThumbnailUrl("upload?getthumb=" + fileItem.getName());
                        uploadResult.setDeleteUrl("upload?delfile=" + fileItem.getName());                        
                        uploadResultList.add(uploadResult);
                    } else {
                        System.out.println("一般表单-->     " + fileItem.getFieldName() + "," + fileItem.getString());
                    }
                }
            }
        } catch (FileUploadException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {            
            ObjectMapper mapper = new ObjectMapper();
            String jsonStr = mapper.writeValueAsString(uploadResultList);
            printWriter.write(jsonStr);
            printWriter.close();
        }
    }

    //
    private boolean canUnionFiles(String[] blockDone, long blockTotal, long blockIndex) {
        long totalAll = 0;
        long currentAll = blockIndex;
        for (int i = 0; i < blockTotal; i++) {
            if (i < blockDone.length) {
                String value = StringUtils.isEmpty(blockDone[i]) ? "0" : blockDone[i];                
                currentAll += Integer.parseInt(value);
            }
            totalAll += i + 1;
        }
        return totalAll == currentAll;
    }

}


spring.servlet.multipart.maxFileSize=2048MB
spring.servlet.multipart.maxRequestSize=2048MB
upload.filepath=D:\\dataTest


@SpringBootApplication
@ServletComponentScan
@EnableCaching
@MapperScan("com.smartmap.platform.gis.**.dao")
public class Application extends SpringBootServletInitializer {
 
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        
        //
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {        
        return builder.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}


2.客户端

var blockDone = [];
//
$(‘#dynamic-layer-fileupload‘).fileupload({
  dataType: ‘json‘,
  url: ‘http://127.0.0.1:8080/springtest/gis/vectorUpload‘,  
  paramName: ‘files‘,
  maxChunkSize: 1024 * 1024 * 20,
  //limitConcurrentUploads: 5,
  singleFileUploads: true,
  formAcceptCharset: ‘utf-8‘,
  formData: [{
      name: ‘a‘,
      value: 1
    },
    {
      name: ‘b‘,
      value: 2
    }
  ],
  chunkbeforesend: function (e, data) {
    console.log("-------------------------- chunkbeforesend");
  },
  chunksend: function (e, data) {
    console.log("-------------------------- chunksend");
    //
    var range = data.headers[‘Content-Range‘].split(" ")[1];
    var startEndMax = range.replace("/", "-").split("-");
    var start = parseInt(startEndMax[0]);
    var end = parseInt(startEndMax[1]) + 1;
    var max = parseInt(startEndMax[2]);
    var total = Math.floor(max / data.maxChunkSize) + ((max % data.maxChunkSize) == 0 ? 0 : 1);
    var index = Math.floor(end / data.maxChunkSize) + ((end % data.maxChunkSize) == 0 ? 0 : 1);
    //
    data.headers[‘Block-Index‘] = index;
    data.headers[‘Block-Total‘] = total;
    data.headers[‘Block-Done‘] = blockDone.join(‘,‘);
    //
    data.formData = [{
        name: ‘Block-Index‘,
        value: index
      },
      {
        name: ‘Block-Total‘,
        value: total
      },
      {
        name: ‘Block-Done‘,
        value: blockDone.join(‘,‘)
      }
    ];
    //
    return true;
  },
  chunkdone: function (e, data) {
    console.log("-------------------------- chunkdone");
    var range = data.headers[‘Content-Range‘].split(" ")[1];
    var startEndMax = range.replace("/", "-").split("-");
    var end = parseInt(startEndMax[1]) + 1;
    var index = Math.floor(end / data.maxChunkSize) + ((end % data.maxChunkSize) == 0 ? 0 : 1);            
    blockDone[index - 1] = index;
  },
  add: function (e, data) {
    blockDone = [];
    $(‘.dynamic-layer-file-name‘).text(data.files[0].name).attr(‘title‘, data.files[0].name);
    //data.context = $(‘#dynamic-layer-progress .dynamic-layer-bar‘).text(‘Uploading...‘);
    data.submit();
  },
  progressall: function (e, data) {
    var progress = parseInt(data.loaded / data.total * 100, 10);
    $(‘#dynamic-layer-progress .dynamic-layer-bar‘).css(‘width‘, progress + ‘%‘);
    $(‘#dynamic-layer-progress .dynamic-layer-value‘).text(progress + ‘%‘);
  },
  done: function (e, data) {
    //data.context.text(‘Upload finished.‘);
    console.log("------------done--------------");
    //console.log(e);
    console.log(data);
    console.log("------------done--------------");
  }
});


<div class="dynamic-layer-form-line" aria-label="文件选择">
    <label class="form-label">文件选择</label>
    <div class="form-item">
      <span class="esri-widget--button fileinput-button">
        <span>上传</span>
        <input id="dynamic-layer-fileupload" type="file" multiple>
      </span>
      <div class="dynamic-layer-file-name"></div>
    </div>
</div>
<div class="dynamic-layer-form-line" aria-label="上传进度">
    <div id="dynamic-layer-progress">
      <div class="dynamic-layer-bar" style="width: 0%;"></div>
      <div class="dynamic-layer-value"></div>
    </div>
</div>

大文件分块上传

标签:spl   on()   content   flush   return   nbu   ica   nts   layer   

原文地址:https://www.cnblogs.com/gispathfinder/p/10203564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!