webupload大文件分片上传

webupload大文件分片上传

1. 大文件分片上传的js代码

js设置

2.大文件上传的java后台代码

 public String fileUpload(@RequestParam("file") MultipartFile file, String chunks, String chunk,
                             String name, HttpServletRequest request, HttpServletResponse response, String fileType) throws IOException {
        log.info("FileManageController.fileUpload request={}", request);
        InputStream proIn = getClass().getClassLoader().getResourceAsStream("uploadConfig.properties");
        Properties properties = new Properties();
        properties.load(proIn);
        String fileSystemPath = properties.getProperty("fileManagerSystemPath");//系统根路径
        String tempFileSystemPath = properties.getProperty("uploadFileSystemPath");//系统根路径
        String fileName = file.getOriginalFilename();
        String savePath = fileSystemPath + "/" + fileType;
        JSONObject json = new JSONObject();
        File retfile = new File(savePath, fileName);
        //为了创建存放的文件夹
        if (!retfile.exists()) {
            retfile.mkdirs();
        }
        json.put("filePath", retfile.getPath());
        try {
            if (null != file) {
                //判断上传的文件是否被分片(小于5M的不会分片)不分片的上传
                if (null == chunks && null == chunk) {
                    File targetFile = new File(savePath, fileName);
                    if (!targetFile.exists()) {
                        targetFile.mkdirs();
                    }
                    file.transferTo(targetFile);
                    targetFile.createNewFile();
                    json.put("msg", "success");
                    return json.toJSONString();
                }
                //分片处理
//                String tempFileDir = fileSystemPath + File.separator+ System.currentTimeMillis() + File.separator + name;
                String tempFileDir = tempFileSystemPath + File.separator + name;
                File parentFileDir = new File(tempFileDir);
                if (!parentFileDir.exists()) {
                    parentFileDir.mkdirs();
                }
                File f = new File(tempFileDir + File.separator + name + "_" + chunk + ".part");
                file.transferTo(f);
                f.createNewFile();
                // 是否全部上传完成
                // 所有分片都存在才说明整个文件上传完成
                boolean uploadDone = true;
                for (int i = 0; i < Integer.parseInt(chunks); i++) {
                    File partFile = new File(tempFileDir, name + "_" + i + ".part");
                    if (!partFile.exists()) {
                        uploadDone = false;
                        json.put("msg", "success");
                        return json.toJSONString();
                    }
                }
                // 所有分片文件都上传完成
                // 将所有分片文件合并到一个文件中
                if (uploadDone) {
                    synchronized (this) {
                        File destTempFile = new File(savePath, name);
                        for (int i = 0; i < Integer.parseInt(chunks); i++) {
                            File partFile = new File(tempFileDir, name + "_" + i + ".part");
                            if (retfile.exists() && i == 0) {
                                retfile.delete();
                                retfile.createNewFile();
                            }
                            FileOutputStream destTempfos = new FileOutputStream(destTempFile, true);
                            FileUtils.copyFile(partFile, destTempfos);
                            destTempfos.close();
                        }
                        FileUtils.deleteDirectory(parentFileDir);
                    }
                }    
                json.put("msg", "success");
                log.info("FileManageController.fileUpload response={}", json.toJSONString());
                return json.toJSONString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("FileManageController.fileUpload error={}", e.getMessage());
        }
        json.put("msg", "success");
        return json.toJSONString();
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值