程序示例
private Boolean uploadFile(File uploadFile, String uploadUrl, List<Object> fileList) {final long CHUNK_SIZE = 500L * 1024 * 1024; // 500MBlong fileSize = uploadFile.length();int totalChunk = (int) Math.ceil((double) fileSize / CHUNK_SIZE);double totalSizeInMB = fileSize / 1024.0 / 1024.0;String md5 = DigestUtils.md5Hex(uploadFile.getName());System.out.printf("开始上传附件:%s,大小:%.2f MB,总分片:%d%n",uploadFile.getName(), totalSizeInMB, totalChunk);long startTime = System.currentTimeMillis(); // ⏱ 开始时间try (RandomAccessFile raf = new RandomAccessFile(uploadFile, "r")) {for (int i = 0; i < totalChunk; i++) {int fileChunk = i + 1;long currentChunkSize = Math.min(CHUNK_SIZE, fileSize - i * CHUNK_SIZE);byte[] chunkData = new byte[(int) currentChunkSize];raf.seek(i * CHUNK_SIZE);raf.readFully(chunkData);try (CloseableHttpClient client = HttpClients.createDefault()) {HttpPost post = new HttpPost(uploadUrl);HttpEntity multipart = MultipartEntityBuilder.create().setCharset(StandardCharsets.UTF_8).setMode(HttpMultipartMode.RFC6532).addTextBody("fileName", uploadFile.getName().replaceFirst("\\.[^.]+$", ""), ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8)).addTextBody("fileForm", uploadFile.getName().contains(".") ? uploadFile.getName().substring(uploadFile.getName().lastIndexOf('.') + 1) : "", ContentType.TEXT_PLAIN).addTextBody("fileChunk", String.valueOf(fileChunk), ContentType.TEXT_PLAIN).addTextBody("totalChunk", String.valueOf(totalChunk), ContentType.TEXT_PLAIN).addTextBody("md5", md5, ContentType.TEXT_PLAIN).addBinaryBody("file", chunkData, ContentType.DEFAULT_BINARY, uploadFile.getName()).build();post.setEntity(multipart);try (CloseableHttpResponse response = client.execute(post)) {int status = response.getStatusLine().getStatusCode();if (status == 200) {System.out.printf("分片 %d/%d 上传成功,大小:%.2f MB%n",fileChunk, totalChunk, currentChunkSize / 1024.0 / 1024.0);if (fileChunk == totalChunk) {String json = EntityUtils.toString(response.getEntity(), "UTF-8");ObjectMapper mapper = new ObjectMapper();@SuppressWarnings("unchecked")Map<String, Object> result = mapper.readValue(json, Map.class);@SuppressWarnings("unchecked")List<Map<String, Object>> dataArray = (List<Map<String, Object>>) result.get("data");for (Map<String, Object> data : dataArray) {JSONObject file = new JSONObject();file.put("fileName", data.get("fileName"));file.put("fileSize", data.get("fileSize"));file.put("fileExtension", data.get("fileExtension"));file.put("fileFullName", data.get("fileFullName"));file.put("id", data.get("id"));file.put("fileSizeText", data.get("fileSizeText"));file.put("url", data.get("url"));file.put("md5", data.get("md5"));file.put("shadowLinkCode",data.get("shadowLinkCode"));fileList.add(file);}}} else {System.err.printf("分片上传失败:%d/%d,HTTP Status = %d%n", fileChunk, totalChunk, status);return false;}}}}long endTime = System.currentTimeMillis(); // ⏱ 结束时间double durationSeconds = (endTime - startTime) / 1000.0;double speed = totalSizeInMB / durationSeconds;System.out.printf("附件上传完成:%s,总耗时:%.2f 秒,平均速度:%.2f MB/s%n",uploadFile.getName(), durationSeconds, speed);return true;} catch (IOException e) {e.printStackTrace();return false;}
}