当前位置: 首页 > wzjs >正文

p2p网站建设方案书二级域名出租

p2p网站建设方案书,二级域名出租,wordpress分类目录添加报错_标签不能添加,外贸soho做网站分片上传和直接上传是两种常见的文件上传方式。分片上传将文件分成多个小块,每次上传一个小块,可以并行处理多个分片,适用于大文件上传,减少了单个请求的大小,能有效避免因网络波动或上传中断导致的失败,并…

分片上传和直接上传是两种常见的文件上传方式。分片上传将文件分成多个小块,每次上传一个小块,可以并行处理多个分片,适用于大文件上传,减少了单个请求的大小,能有效避免因网络波动或上传中断导致的失败,并支持断点续传。相比之下,直接上传是将文件作为一个整体上传,通常适用于较小的文件,简单快捷,但对于大文件来说,容易受到网络环境的影响,上传中断时需要重新上传整个文件。因此,分片上传在大文件上传中具有更高的稳定性和可靠性。

FileUploadController

package per.mjn.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import per.mjn.service.FileUploadService;import java.io.File;
import java.io.IOException;
import java.util.List;@RestController
@RequestMapping("/upload")
public class FileUploadController {private static final String UPLOAD_DIR = "F:/uploads/";@Autowiredprivate FileUploadService fileUploadService;// 启动文件分片上传@PostMapping("/start")public ResponseEntity<String> startUpload(@RequestParam("file") MultipartFile file,@RequestParam("totalParts") int totalParts,@RequestParam("partIndex") int partIndex) {try {String fileName = file.getOriginalFilename();fileUploadService.saveChunk(file, partIndex);  // 存储单个分片return ResponseEntity.ok("Chunk " + partIndex + " uploaded successfully.");} catch (IOException e) {return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error uploading chunk: " + e.getMessage());}}// 多线程并行上传所有分片@PostMapping("/uploadAll")public ResponseEntity<String> uploadAllChunks(@RequestParam("file") List<MultipartFile> files,@RequestParam("fileName") String fileName) {try {fileUploadService.uploadChunksInParallel(files, fileName);  // 调用并行上传方法return ResponseEntity.ok("All chunks uploaded successfully.");} catch (Exception e) {return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error uploading chunks: " + e.getMessage());}}// 合并文件分片@PostMapping("/merge")public ResponseEntity<String> mergeChunks(@RequestParam("fileName") String fileName,@RequestParam("totalParts") int totalParts) {try {System.out.println(fileName);System.out.println(totalParts);fileUploadService.mergeChunks(fileName, totalParts);return ResponseEntity.ok("File uploaded and merged successfully.");} catch (IOException e) {return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error merging chunks: " + e.getMessage());}}// 直接上传整个文件@PostMapping("/file")public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {try {String fileName = file.getOriginalFilename();File targetFile = new File(UPLOAD_DIR + fileName);File dir = new File(UPLOAD_DIR);if (!dir.exists()) {dir.mkdirs();}file.transferTo(targetFile);return ResponseEntity.ok("File uploaded successfully: " + fileName);} catch (IOException e) {return ResponseEntity.status(500).body("Error uploading file: " + e.getMessage());}}
}

FileUploadService

package per.mjn.service;import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.util.List;public interface FileUploadService {public void saveChunk(MultipartFile chunk, int partIndex) throws IOException;public void uploadChunksInParallel(List<MultipartFile> chunks, String fileName);public void mergeChunks(String fileName, int totalParts) throws IOException;
}

FileUploadServiceImpl

package per.mjn.service.impl;import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import per.mjn.service.FileUploadService;import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;@Service
public class FileUploadServiceImpl implements FileUploadService {private static final String UPLOAD_DIR = "F:/uploads/";private final ExecutorService executorService;public FileUploadServiceImpl() {// 使用一个线程池来并发处理上传this.executorService = Executors.newFixedThreadPool(4); // 4个线程用于并行上传}// 保存文件分片public void saveChunk(MultipartFile chunk, int partIndex) throws IOException {File dir = new File(UPLOAD_DIR);if (!dir.exists()) {dir.mkdirs();}File chunkFile = new File(UPLOAD_DIR + chunk.getOriginalFilename() + ".part" + partIndex);chunk.transferTo(chunkFile);}// 处理所有分片的上传public void uploadChunksInParallel(List<MultipartFile> chunks, String fileName) {List<Callable<Void>> tasks = new ArrayList<>();for (int i = 0; i < chunks.size(); i++) {final int index = i;final MultipartFile chunk = chunks.get(i);tasks.add(() -> {try {saveChunk(chunk, index);System.out.println("Uploaded chunk " + index + " of " + fileName);} catch (IOException e) {e.printStackTrace();}return null;});}try {// 执行所有上传任务executorService.invokeAll(tasks);} catch (InterruptedException e) {e.printStackTrace();}}// 合并文件分片public void mergeChunks(String fileName, int totalParts) throws IOException {File mergedFile = new File(UPLOAD_DIR + fileName);try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(mergedFile))) {for (int i = 0; i < totalParts; i++) {File chunkFile = new File(UPLOAD_DIR + fileName + ".part" + i);try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(chunkFile))) {byte[] buffer = new byte[8192];int bytesRead;while ((bytesRead = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}}chunkFile.delete(); // 删除临时分片文件}}}
}

前端测试界面

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>File Upload</title>
</head>
<body><h1>File Upload</h1><input type="file" id="fileInput"><button onclick="startUpload()">Upload File</button><button onclick="directUpload()">Upload File Directly</button><script>const CHUNK_SIZE = 5 * 1024 * 1024; // 5MB per chunkconst fileInput = document.querySelector('#fileInput');let totalChunks;function startUpload() {const file = fileInput.files[0];totalChunks = Math.ceil(file.size / CHUNK_SIZE);let currentChunk = 0;let files = [];while (currentChunk < totalChunks) {const chunk = file.slice(currentChunk * CHUNK_SIZE, (currentChunk + 1) * CHUNK_SIZE);files.push(chunk);currentChunk++;}// 并行上传所有分片uploadAllChunks(files, file.name);}function uploadAllChunks(chunks, fileName) {const promises = chunks.map((chunk, index) => {const formData = new FormData();formData.append('file', chunk, fileName);formData.append('partIndex', index);formData.append('totalParts', totalChunks);formData.append('fileName', fileName);return fetch('http://172.20.10.2:8080/upload/start', {method: 'POST',body: formData}).then(response => response.text()).then(data => console.log(`Chunk ${index} uploaded successfully.`)).catch(error => console.error(`Error uploading chunk ${index}`, error));});// 等待所有分片上传完成Promise.all(promises).then(() => {console.log('All chunks uploaded, now merging.');mergeChunks(fileName);}).catch(error => console.error('Error during uploading chunks', error));}function mergeChunks(fileName) {fetch(`http://172.20.10.2:8080/upload/merge?fileName=${fileName}&totalParts=${totalChunks}`, {method: 'POST'}).then(response => response.text()).then(data => console.log('File uploaded and merged successfully.')).catch(error => console.error('Error merging chunks', error));}// 直接上传整个文件function directUpload() {const file = fileInput.files[0];if (!file) {alert('Please select a file to upload.');return;}const formData = new FormData();formData.append('file', file); // 将整个文件添加到 FormDatafetch('http://172.20.10.2:8080/upload/file', {method: 'POST',body: formData}).then(response => response.text()).then(data => console.log('File uploaded directly: ', data)).catch(error => console.error('Error uploading file directly', error));}</script>
</body>
</html>

测试分片上传与直接上传耗时

我们上传一个310MB的文件,分片上传每个分片在前端设置为10MB,在后端开5个线程并发执行上传操作。
直接上传没有分片大小也没有开多线程,下面是两种方式的测试结果。
在这里插入图片描述
分片上传,耗时2.419s
在这里插入图片描述

直接上传,耗时4.572s


文章转载自:

http://4pcnBhIR.kzhxy.cn
http://cxyNU6bD.kzhxy.cn
http://mbvT6egC.kzhxy.cn
http://GMN2XbJ8.kzhxy.cn
http://QQxN7E48.kzhxy.cn
http://BuGWYYwK.kzhxy.cn
http://unlsD3tC.kzhxy.cn
http://Ud1VyVU1.kzhxy.cn
http://zBH57VkH.kzhxy.cn
http://YaM4wkyY.kzhxy.cn
http://n8wvKdq8.kzhxy.cn
http://P3wiLq0P.kzhxy.cn
http://BKgxXm04.kzhxy.cn
http://o4KFxfs7.kzhxy.cn
http://m3tGoR6n.kzhxy.cn
http://kcjN9Dog.kzhxy.cn
http://WTaWUBfX.kzhxy.cn
http://iY1Z2nsj.kzhxy.cn
http://yglVYnzi.kzhxy.cn
http://QVRphnZs.kzhxy.cn
http://6XHKJ764.kzhxy.cn
http://Y8zQz4HK.kzhxy.cn
http://aZgologT.kzhxy.cn
http://TjsZkeBC.kzhxy.cn
http://H4P8t35u.kzhxy.cn
http://iZN7Bw66.kzhxy.cn
http://9SGQdVlZ.kzhxy.cn
http://7pswLaxV.kzhxy.cn
http://Y2cfws4M.kzhxy.cn
http://OBq4hUyi.kzhxy.cn
http://www.dtcms.com/wzjs/751490.html

相关文章:

  • 国外做水广告网站大全网站建设后百度找不到
  • 新乡网站建设哪家优惠购物网站开发英文文献
  • 防止服务器上的网站被进攻wordpress+调整+行距
  • 如何制作收费网站二个字最吉利最旺财的公司名
  • 刚上线的网站wordpress 正在发送请求
  • 长春建设平台网站的公司哪家好珠海网站建易搜互联
  • 亚马逊网站的建设目标wordpress落地页改造
  • 手机型网站扬州做网站需要多少钱
  • 百度网站建设优化在线生成多款表白网站是怎么做的
  • 免费网站建设程序下载济宁建网站公司
  • 牛仔网站的建设风格wordpress 设置语言
  • 网站下载免费新版杭州建设厅官方网站
  • 长沙网站制作培训基地哈尔滨网站制作哪里专业
  • 技术支持 东莞网站建设 轴承境外网站搭建
  • 做网站图标的软件个人网站设计报告书
  • 烟台优化网站公司南京网络推广外包
  • 做网站怎么提取视频无广告国产在线免费观看高甜电影推荐
  • 做室内装修的网站企业门户网站免费模板
  • 耐克官网网站设计大连云购物app下载安装到手机
  • 无锡朝阳网站建设深圳品牌做网站
  • 网站建设的例子南昌seo网站
  • 怎么成立个人网站WordPress邮箱内容修改
  • 二维码网站制作湖南还没有建网站的企业
  • 企业网站案例展示在哪个网站里下载的图片可以做展架
  • 网站优化体验报告wordpress能多人登录
  • 物流网站建设案例开发者模式有什么危害
  • 网站建设及推广文案网站论坛建设步骤
  • 江苏中淮建设集团有限公司网站住房和城乡建设管理局
  • 购销网站建设视频百度云asp室内装修装潢网站源码
  • 响应式做的比较好的网站2023小规模企业所得税税率是多少