Java 文件分块上传服务器端源代码

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

// TODO 查看下这里是否有问题 // 上传文件非空验证 private void checkEmptyFile(File file) { if (file == null || file.getAbsolutePath() == null) { throw new BaseException(BaseException.ERROR_CODE_20020);// 上传文件不存在 } } /** * 写文件到本地文件夹 * * @throws IOException * 返回生成的文件名 */ private String copyFile(InputStream inputStream, String fileName) { OutputStream outputStream = null; String tempFileName = null; int pointPosition = fileName.lastIndexOf(”.”); if (pointPosition < 0) {// myvedio tempFileName = UUID.randomUUID()。toString();// 94d1d2e0-9aad-4dd8-a0f6-494b0099ff26 } else {// myvedio.flv tempFileName = UUID.randomUUID() + fileName.substring(pointPosition);// 94d1d2e0-9aad-4dd8-a0f6-494b0099ff26.flv } try { outputStream = new FileOutputStream(Global.UPLOAD_TEMP_DIR + tempFileName); int readBytes = 0; byte[] buffer = new byte[10000]; while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) { outputStream.write(buffer, 0, readBytes); } return tempFileName; } catch (IOException ioe) { // log.error(ioe.getMessage(), ioe); throw new BaseException(BaseException.ERROR_CODE_20020);// 上传文件不存在 } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { } } } } /** * 测试此服务是否可用 * * @param response * @return * @author zwq7978 */ @RequestMapping(”/core/v1/file/testServer”) @ResponseBody public Object testServer(HttpServletResponse response) { setHttpStatusOk(response); return Global.SUCCESS_RESPONSE; } public UploadService getUploadService() { return uploadService; } public void setUploadService(UploadService uploadService) { this.uploadService = uploadService; } public void setAuthService(AuthService authService) { this.authService = authService; } public AuthService getAuthService() { return authService; } } 比如要上传的文件是 test450k.mp4.对照《Java 文件分块上传客户端源代码》中分块上传服务器对分块文件参数定义的名字”file”,upload 方法里使用的是 MultipartFile 接收该对象。对于每次的 HTTP 请求,使用 copyFile 方法将文件流输出到服务器本地的一个临时文件夹里,比如作者的是 D:/defonds/syncPath/uploadTemp,该文件下会有 50127019-b63b-4a54-8f53-14efd1e58ada.mp4 临时文件生成用于保存上传文件流。 分块依次上传。当所有块都上传完毕之后,将这些临时文件都转移到服务器指定目录中,比如作者的这个目录是 D:/defonds/syncPath/file,在该文件夹下会有/1/temp_dir_5_1 目录生成,而 uploadTemp 的临时文件则被挨个转移到这个文件夹下,生成形如 5.part0001 的文件。以下是文件转移的源代码: [java] /** * 把所有块从临时文件目录移到指定本地目录或S2/S3 * * @param preUpload */ private void moveBlockFiles(BlockPreuploadFileInfo preUpload) { @SuppressWarnings(”unchecked”) String[] s3BlockUrl=new String[preUpload.getBlockNumber()]; String[] localBlockUrl=new String[preUpload.getBlockNumber()];//本地的块文件路径 以便以后删除 List<BlockUploadInfo> blocks = (List<BlockUploadInfo>) getBaseDao()。queryForList( “upload.getBlockUploadFileByUuid”, preUpload.getUuid()); String tempDirName = SyncUtil.getTempDirName(preUpload.getUuid(), preUpload.getUid()); String parentPath = Global.UPLOAD_ABSOLUTE_PAHT_ + Global.PATH_SEPARATIVE_SIGN + String.valueOf(preUpload.getUid()); String dirPath = parentPath + Global.PATH_SEPARATIVE_SIGN + tempDirName; new File(dirPath)。mkdirs();//创建存放块文件的文件夹 (本地) int j=0; for (BlockUploadInfo info : blocks) { try { String strBlockIndex = createStrBlockIndex(info.getBlockIndex()); String suffixPath = preUpload.getUuid() + “.part” + strBlockIndex; String tempFilePath = info.getTempFile(); File tempFile = new File(tempFilePath); File tmpFile = new File(dirPath + suffixPath); if (tmpFile.exists()) { FileUtils.deleteQuietly(tmpFile); } FileUtils.moveFile(tempFile, tmpFile); localBlockUrl[j]=dirPath + suffixPath; j++; info.setStatus(Global.MOVED_TO_NEWDIR); getBaseDao()。update(”upload.updateBlockUpload”, info); if (log.isInfoEnabled()) log.info(preUpload.getUuid() + ” ” + info.getBuId() + ” moveBlockFiles”); } catch (IOException e) { log.error(e.getMessage(), e); throw new BaseException(”file not found”); } } preUpload.setLocalBlockUrl(localBlockUrl); preUpload.setDirPath(dirPath); preUpload.setStatus(Global.MOVED_TO_NEWDIR); getBaseDao()。update(”upload.updatePreUploadInfo”, preUpload); } private String createStrBlockIndex(int blockIndex) { String strBlockIndex; if (blockIndex < 10) { strBlockIndex = “000” + blockIndex; } else if (10 <= blockIndex && blockIndex < 100) { strBlockIndex = “00” + blockIndex; } else if (100 <= blockIndex && blockIndex < 1000) { strBlockIndex = “0” + blockIndex; } else { strBlockIndex = “” + blockIndex; } return strBlockIndex; } 最后是文件的组装源代码: [java] /** * 组装文件 * */ private void assembleFileWithBlock(BlockPreuploadFileInfo preUpload) { String dirPath = preUpload.getDirPath(); // 开始在指定目录组装文件 String uploadedUrl = null; String[] separatedFiles; String[][] separatedFilesAndSize; int fileNum = 0; File file = new File(dirPath); separatedFiles = file.list(); separatedFilesAndSize = new String[separatedFiles.length][2]; Arrays.sort(separatedFiles); fileNum = separatedFiles.length; for (int i = 0; i < fileNum; i++) { separatedFilesAndSize[i][0] = separatedFiles[i]; String fileName = dirPath + separatedFiles[i]; File tmpFile = new File(fileName); long fileSize = tmpFile.length(); separatedFilesAndSize[i][1] = String.valueOf(fileSize); }

[1][2][3]

我就想是一只草原中被牧童遗忘的羊,

Java 文件分块上传服务器端源代码

相关文章:

你感兴趣的文章:

标签云: