标签:
plupload批量上传分片功能,
对于文件比较大的情况下,plupload支持分片上传,后台代码如下:
/** * * 方法:upLoadSpecialProgramPictrue * 方法说明:本地节目导入 * @return * @author wangHao * @throws Exception * @date 2015年6月9日 */ @RequestMapping("/localUpLoadProgram") @ResponseBody public void localUpLoadProgram( @RequestParam("file") CommonsMultipartFile[] file, HttpServletResponse response,HttpServletRequest request) throws Exception { boolean isMultipart = ServletFileUpload.isMultipartContent(request); if(isMultipart){ String fileName = ""; Integer chunk = 0, chunks = 0; //检查文件目录,不存在则创建 String relativePath = "/plupload/files/"; String realPath = ConfigUtil.class.getResource("/").getPath(); File folder = new File(realPath + relativePath); if (!folder.exists()) { folder.mkdirs(); } DiskFileItemFactory diskFactory = new DiskFileItemFactory(); // threshold 极限、临界值,即硬盘缓存 1M diskFactory.setSizeThreshold(4 * 1024); ServletFileUpload upload = new ServletFileUpload(diskFactory); // 设置允许上传的最大文件大小(单位MB) upload.setSizeMax(1024 * 1048576); upload.setHeaderEncoding("UTF-8"); List<FileItem> fileList = new ArrayList<FileItem>(); fileList.add(file[0].getFileItem()); Iterator<FileItem> it = fileList.iterator(); FileItem item = it.next(); String name = item.getFieldName(); InputStream input = item.getInputStream(); fileName = item.getName(); chunk = Integer.parseInt(request.getParameter("chunk")); chunks = Integer.parseInt(request.getParameter("chunks")); // 处理上传文件内容 if (!item.isFormField()) { //目标文件 File destFile = new File(folder, fileName); //文件已存在删除旧文件(上传了同名的文件) if (chunk == 0 && destFile.exists()) { destFile.delete(); destFile = new File(folder, fileName); } //合成文件 appendFile(input, destFile); if (chunk == chunks - 1) { System.out.println("上传完成"); }else { System.out.println("还剩["+(chunks-1-chunk)+"]个块文件"); } } } } private void appendFile(InputStream in, File destFile) { OutputStream out = null; try { // plupload 配置了chunk的时候新上传的文件append到文件末尾 if (destFile.exists()) { out = new BufferedOutputStream(new FileOutputStream(destFile, true), BUFFER_SIZE); } else { out = new BufferedOutputStream(new FileOutputStream(destFile),BUFFER_SIZE); } in = new BufferedInputStream(in, BUFFER_SIZE); int len = 0; byte[] buffer = new byte[BUFFER_SIZE]; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } } catch (Exception e) { } finally { try { if (null != in) { in.close(); } if(null != out){ out.close(); } } catch (IOException e) { } } }
标签:
原文地址:http://www.cnblogs.com/holdon521/p/4625053.html