webupload大文件分片上传

来源:互联网 发布:流程优化六步法 编辑:程序博客网 时间:2024/05/17 00:17

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();    }