{"id":10020394,"date":"2024-11-13T14:21:23","date_gmt":"2024-11-13T06:21:23","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020394"},"modified":"2024-11-13T14:21:23","modified_gmt":"2024-11-13T06:21:23","slug":"springboot-%e5%ae%9e%e6%88%98%ef%bc%9a%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e4%b9%8b%e7%a7%92%e4%bc%a0%e3%80%81%e6%96%ad%e7%82%b9%e7%bb%ad%e4%bc%a0%e3%80%81%e5%88%86%e7%89%87%e4%b8%8a%e4%bc%a0","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020394","title":{"rendered":"SpringBoot \u5b9e\u6218\uff1a\u6587\u4ef6\u4e0a\u4f20\u4e4b\u79d2\u4f20\u3001\u65ad\u70b9\u7eed\u4f20\u3001\u5206\u7247\u4e0a\u4f20"},"content":{"rendered":"<p>\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u51e0\u4e4e\u662f\u6bcf\u4e2a Web \u5e94\u7528\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u65e0\u8bba\u662f\u4e2a\u4eba\u535a\u5ba2\u4e2d\u7684\u56fe\u7247\u4e0a\u4f20\uff0c\u8fd8\u662f\u4f01\u4e1a\u7ea7\u5e94\u7528\u4e2d\u7684\u6587\u6863\u7ba1\u7406\uff0c\u6587\u4ef6\u4e0a\u4f20\u90fd\u626e\u6f14\u7740\u81f3\u5173\u91cd\u8981\u7684\u89d2\u8272\u3002\u4eca\u5929\uff0c\u677e\u54e5\u548c\u5927\u5bb6\u6765\u804a\u804a\u6587\u4ef6\u4e0a\u4f20\u4e2d\u7684\u51e0\u4e2a\u9ad8\u7ea7\u73a9\u6cd5\u2014\u2014\u79d2\u4f20\u3001\u65ad\u70b9\u7eed\u4f20\u548c\u5206\u7247\u4e0a\u4f20\u3002<\/p>\n<h2>\u4e00 \u6587\u4ef6\u4e0a\u4f20\u7684\u5e38\u89c1\u573a\u666f<\/h2>\n<p>\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u6587\u4ef6\u4e0a\u4f20\u7684\u573a\u666f\u591a\u79cd\u591a\u6837\u3002\u6bd4\u5982\uff0c\u5728\u7ebf\u6559\u80b2\u5e73\u53f0\u4e0a\u7684\u89c6\u9891\u8d44\u6e90\u4e0a\u4f20\uff0c\u793e\u4ea4\u5e73\u53f0\u4e0a\u7684\u56fe\u7247\u5206\u4eab\uff0c\u4ee5\u53ca\u4f01\u4e1a\u5185\u90e8\u7684\u77e5\u8bc6\u6587\u6863\u7ba1\u7406\u7b49\u3002\u8fd9\u4e9b\u573a\u666f\u5bf9\u6587\u4ef6\u4e0a\u4f20\u7684\u8981\u6c42\u4e5f\u5404\u4e0d\u76f8\u540c\uff0c\u6709\u7684\u8ffd\u6c42\u901f\u5ea6\uff0c\u6709\u7684\u6ce8\u91cd\u7a33\u5b9a\u6027\uff0c\u8fd8\u6709\u7684\u9700\u8981\u8003\u8651\u6587\u4ef6\u5927\u5c0f\u548c\u5b89\u5168\u6027\u3002\u56e0\u6b64\uff0c\u9488\u5bf9\u4e0d\u540c\u9700\u6c42\uff0c\u6211\u4eec\u6709\u4e86\u79d2\u4f20\u3001\u65ad\u70b9\u7eed\u4f20\u548c\u5206\u7247\u4e0a\u4f20\u7b49\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<h2>\u4e8c \u79d2\u4f20\u3001\u65ad\u70b9\u4e0a\u4f20\u4e0e\u5206\u7247\u4e0a\u4f20<\/h2>\n<h3>\u79d2\u4f20<\/h3>\n<p>\u79d2\u4f20\uff0c\u987e\u540d\u601d\u4e49\uff0c\u5c31\u662f\u51e0\u4e4e\u77ac\u95f4\u5b8c\u6210\u6587\u4ef6\u4e0a\u4f20\u7684\u8fc7\u7a0b\u3002\u5176\u5b9e\u73b0\u539f\u7406\u662f\u901a\u8fc7\u8ba1\u7b97\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\uff08\u5982 MD5 \u6216 SHA-1\uff09\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7b26\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u3002\u5982\u679c\u670d\u52a1\u5668\u4e0a\u5df2\u7ecf\u5b58\u5728\u76f8\u540c\u7684\u6587\u4ef6\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u6210\u529f\u4fe1\u606f\uff0c\u907f\u514d\u4e86\u91cd\u590d\u4e0a\u4f20\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u8282\u7701\u4e86\u5e26\u5bbd\uff0c\u4e5f\u5927\u5927\u63d0\u9ad8\u4e86\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<h3>\u65ad\u70b9\u7eed\u4f20<\/h3>\n<p>\u65ad\u70b9\u7eed\u4f20\u662f\u6307\u5728\u7f51\u7edc\u4e0d\u7a33\u5b9a\u6216\u8005\u7528\u6237\u4e3b\u52a8\u4e2d\u65ad\u4e0a\u4f20\u540e\uff0c\u80fd\u591f\u4ece\u4e0a\u6b21\u4e2d\u65ad\u7684\u5730\u65b9\u7ee7\u7eed\u4e0a\u4f20\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u65b0\u5f00\u59cb\u6574\u4e2a\u8fc7\u7a0b\u3002\u8fd9\u5bf9\u4e8e\u5927\u6587\u4ef6\u4e0a\u4f20\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u56e0\u7f51\u7edc\u95ee\u9898\u5bfc\u81f4\u7684\u4e0a\u4f20\u5931\u8d25\uff0c\u540c\u65f6\u4e5f\u80fd\u8282\u7ea6\u7528\u6237\u7684\u6d41\u91cf\u548c\u65f6\u95f4\u3002<\/p>\n<h3>\u5206\u7247\u4e0a\u4f20<\/h3>\n<p>\u5206\u7247\u4e0a\u4f20\u5219\u662f\u5c06\u4e00\u4e2a\u5927\u6587\u4ef6\u5206\u5272\u6210\u591a\u4e2a\u5c0f\u5757\u5206\u522b\u4e0a\u4f20\uff0c\u6700\u540e\u518d\u7531\u670d\u52a1\u5668\u5408\u5e76\u6210\u5b8c\u6574\u7684\u6587\u4ef6\u3002\u8fd9\u79cd\u505a\u6cd5\u7684\u597d\u5904\u662f\u53ef\u4ee5\u5e76\u884c\u5904\u7406\u591a\u4e2a\u5c0f\u6587\u4ef6\uff0c\u63d0\u9ad8\u4e0a\u4f20\u6548\u7387\uff1b\u540c\u65f6\uff0c\u5982\u679c\u67d0\u4e00\u90e8\u5206\u4e0a\u4f20\u5931\u8d25\uff0c\u53ea\u9700\u8981\u91cd\u4f20\u8fd9\u4e00\u90e8\u5206\uff0c\u4e0d\u5f71\u54cd\u5176\u4ed6\u90e8\u5206\u3002<\/p>\n<h2>\u4e09 \u79d2\u4f20\u5b9e\u6218<\/h2>\n<h3>\u540e\u7aef\u5b9e\u73b0<\/h3>\n<p>\u5728 SpringBoot \u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>MessageDigest<\/code> \u7c7b\u6765\u8ba1\u7b97\u6587\u4ef6\u7684 MD5 \u503c\uff0c\u7136\u540e\u68c0\u67e5\u6570\u636e\u5e93\u4e2d\u662f\u5426\u5b58\u5728\u8be5\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"java\">@RestController\n@RequestMapping(&quot;\/file&quot;)\npublic class FileController {\n    @Autowired\n    FileService fileService;\n\n    @PostMapping(&quot;\/upload1&quot;)\n    public ResponseEntity&lt;String&gt; secondUpload(@RequestParam(value = &quot;file&quot;,required = false) MultipartFile file,@RequestParam(required = false,value = &quot;md5&quot;) String md5) {\n        try {\n            \/\/ \u68c0\u67e5\u6570\u636e\u5e93\u4e2d\u662f\u5426\u5df2\u5b58\u5728\u8be5\u6587\u4ef6\n            if (fileService.existsByMd5(md5)) {\n                return ResponseEntity.ok(&quot;\u6587\u4ef6\u5df2\u5b58\u5728&quot;);\n            }\n            \/\/ \u4fdd\u5b58\u6587\u4ef6\u5230\u670d\u52a1\u5668\n            file.transferTo(new File(&quot;\/path\/to\/save\/&quot; + file.getOriginalFilename()));\n            \/\/ \u4fdd\u5b58\u6587\u4ef6\u4fe1\u606f\u5230\u6570\u636e\u5e93\n            fileService.save(new FileInfo(file.getOriginalFilename(), DigestUtils.md5DigestAsHex(file.getInputStream())));\n            return ResponseEntity.ok(&quot;\u4e0a\u4f20\u6210\u529f&quot;);\n        } catch (Exception e) {\n            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(&quot;\u4e0a\u4f20\u5931\u8d25&quot;);\n        }\n    }\n}<\/code><\/pre>\n<h3>\u524d\u7aef\u8c03\u7528<\/h3>\n<p>\u524d\u7aef\u53ef\u4ee5\u901a\u8fc7 JavaScript \u7684 FileReader API \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u901a\u8fc7 spark-md5 \u8ba1\u7b97 MD5 \u503c\uff0c\u7136\u540e\u53d1\u9001\u7ed9\u540e\u7aef\u8fdb\u884c\u6821\u9a8c\u3002<\/p>\n<pre><code class=\"html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;\u79d2\u4f20&lt;\/title&gt;\n    &lt;script src=&quot;spark-md5.js&quot;&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;input type=&quot;file&quot; id=&quot;fileInput&quot; \/&gt;\n&lt;button onclick=&quot;startUpload()&quot;&gt;\u5f00\u59cb\u4e0a\u4f20&lt;\/button&gt;\n&lt;hr&gt;\n&lt;script&gt;\n    async function startUpload() {\n        const fileInput = document.getElementById('fileInput');\n        const file = fileInput.files[0];\n        if (!file) {\n            alert(&quot;\u8bf7\u9009\u62e9\u6587\u4ef6&quot;);\n            return;\n        }\n\n        const md5 = await calculateMd5(file);\n        const formData = new FormData();\n        formData.append('md5', md5);\n\n        const response = await fetch('\/file\/upload1', {\n            method: 'POST',\n            body: formData\n        });\n\n        const result = await response.text();\n        if (response.ok) {\n            if (result != &quot;\u6587\u4ef6\u5df2\u5b58\u5728&quot;) {\n                \/\/ \u5f00\u59cb\u4e0a\u4f20\u6587\u4ef6\n            }\n        } else {\n            console.error(&quot;\u4e0a\u4f20\u5931\u8d25: &quot; + result);\n        }\n    }\n\n    function calculateMd5(file) {\n        return new Promise((resolve, reject) =&gt; {\n            const reader = new FileReader();\n            reader.onloadend = () =&gt; {\n                const spark = new SparkMD5.ArrayBuffer();\n                spark.append(reader.result);\n                resolve(spark.end());\n            };\n            reader.onerror = () =&gt; reject(reader.error);\n            reader.readAsArrayBuffer(file);\n        });\n    }\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>\u524d\u7aef\u5206\u4e3a\u4e24\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u8ba1\u7b97\u6587\u4ef6\u7684 MD5 \u503c\uff0c\u8ba1\u7b97\u4e4b\u540e\u53d1\u9001\u7ed9\u670d\u52a1\u7aef\u786e\u5b9a\u6587\u4ef6\u662f\u5426\u5b58\u5728\u3002<\/li>\n<li>\u5982\u679c\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u4e0d\u9700\u8981\u7ee7\u7eed\u4e0a\u4f20\u6587\u4ef6\uff1b\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u5f00\u59cb\u4e0a\u4f20\u6587\u4ef6\uff0c\u4e0a\u4f20\u6587\u4ef6\u548c MD5 \u6821\u9a8c\u8bf7\u6c42\u7c7b\u4f3c\uff0c\u4e0a\u9762\u7684\u6848\u4f8b\u4ee3\u7801\u4e2d\u6211\u5c31\u6ca1\u6709\u91cd\u590d\u6f14\u793a\u4e86\uff0c\u677e\u54e5\u5728\u4e66\u91cc\u548c\u4e4b\u524d\u7684\u8bfe\u7a0b\u91cc\u90fd\u591a\u6b21\u8bb2\u8fc7\u6587\u4ef6\u4e0a\u4f20\uff0c\u8fd9\u91cc\u4e0d\u518d\u5570\u55e6\u3002<\/li>\n<\/ol>\n<h2>\u56db \u5206\u7247\u4e0a\u4f20\u5b9e\u6218<\/h2>\n<p>\u5206\u7247\u4e0a\u4f20\u5173\u952e\u662f\u5728\u524d\u7aef\u5bf9\u6587\u4ef6\u5207\u7247\uff0c\u6bd4\u5982\u4e00\u4e2a 10MB \u7684\u6587\u4ef6\u5207\u4e3a 10 \u4efd\uff0c\u6bcf\u4efd 1MB\u3002\u6bcf\u6b21\u4e0a\u4f20\u7684\u65f6\u5019\uff0c\u9700\u8981\u591a\u4e00\u4e2a\u53c2\u6570\u8bb0\u5f55\u5f53\u524d\u4e0a\u4f20\u7684\u6587\u4ef6\u5207\u7247\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<\/p>\n<p>\u6bd4\u5982\u4e00\u4e2a 10MB \u7684\u6587\u4ef6\uff0c\u5207\u4e3a 10 \u4efd\uff0c\u6bcf\u4efd 1MB\uff0c\u90a3\u4e48\uff1a<\/p>\n<ul>\n<li>\u7b2c 0 \u7247\uff0c\u4ece 0 \u5f00\u59cb\uff0c\u4e00\u5171\u662f <code>1024*1024<\/code> \u4e2a\u5b57\u8282\u3002<\/li>\n<li>\u7b2c 1 \u7247\uff0c\u4ece <code>1024*1024<\/code> \u5f00\u59cb\uff0c\u4e00\u5171\u662f <code>1024*1024<\/code> \u4e2a\u5b57\u8282\u3002<\/li>\n<li>\u7b2c 2 \u7247&#8230;<\/li>\n<\/ul>\n<p>\u628a\u8fd9\u4e2a\u641e\u61c2\uff0c\u540e\u9762\u7684\u4ee3\u7801\u5c31\u597d\u7406\u89e3\u4e86\u3002<\/p>\n<h3>\u540e\u7aef\u5b9e\u73b0<\/h3>\n<pre><code class=\"java\">private static final String UPLOAD_DIR = System.getProperty(&quot;user.home&quot;) + &quot;\/uploads\/&quot;;\n\/**\n * \u4e0a\u4f20\u6587\u4ef6\u5230\u6307\u5b9a\u4f4d\u7f6e\n *\n * @param file \u4e0a\u4f20\u7684\u6587\u4ef6\n * @param start \u6587\u4ef6\u5f00\u59cb\u4e0a\u4f20\u7684\u4f4d\u7f6e\n * @return ResponseEntity&lt;String&gt; \u4e0a\u4f20\u7ed3\u679c\n *\/\n@PostMapping(&quot;\/upload2&quot;)\npublic ResponseEntity&lt;String&gt; resumeUpload(@RequestParam(&quot;file&quot;) MultipartFile file, @RequestParam(&quot;start&quot;) long start,@RequestParam(&quot;fileName&quot;) String fileName) {\n    try {\n        File directory = new File(UPLOAD_DIR);\n        if (!directory.exists()) {\n            directory.mkdirs();\n        }\n        File targetFile = new File(UPLOAD_DIR + fileName);\n        RandomAccessFile randomAccessFile = new RandomAccessFile(targetFile, &quot;rw&quot;);\n        FileChannel channel = randomAccessFile.getChannel();\n        channel.position(start);\n        channel.transferFrom(file.getResource().readableChannel(), start, file.getSize());\n        channel.close();\n        randomAccessFile.close();\n        return ResponseEntity.ok(&quot;\u4e0a\u4f20\u6210\u529f&quot;);\n    } catch (Exception e) {\n        System.out.println(&quot;\u4e0a\u4f20\u5931\u8d25: &quot;+e.getMessage());\n        return ResponseEntity.status(500).body(&quot;\u4e0a\u4f20\u5931\u8d25&quot;);\n    }\n}<\/code><\/pre>\n<p>\u540e\u7aef\u6bcf\u6b21\u5904\u7406\u7684\u65f6\u5019\uff0c\u9700\u8981\u5148\u8bbe\u7f6e\u6587\u4ef6\u7684\u8d77\u59cb\u4f4d\u7f6e\u3002<\/p>\n<h3>\u524d\u7aef\u8c03\u7528<\/h3>\n<p>\u524d\u7aef\u9700\u8981\u5c06\u6587\u4ef6\u5207\u5206\u6210\u591a\u4e2a\u5c0f\u5757\uff0c\u7136\u540e\u4f9d\u6b21\u4e0a\u4f20\u3002<\/p>\n<pre><code class=\"html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;\u5206\u7247\u793a\u4f8b&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;input type=&quot;file&quot; id=&quot;fileInput&quot; \/&gt;\n    &lt;button onclick=&quot;startUpload()&quot;&gt;\u5f00\u59cb\u4e0a\u4f20&lt;\/button&gt;\n\n    &lt;script&gt;\n        async function startUpload() {\n            const fileInput = document.getElementById('fileInput');\n            const file = fileInput.files[0];\n            if (!file) {\n                alert(&quot;\u8bf7\u9009\u62e9\u6587\u4ef6&quot;);\n                return;\n            }\n\n            const filename = file.name;\n            let start = 0;\n\n            uploadFile(file, start);\n        }\n\n        async function uploadFile(file, start) {\n            const chunkSize = 1024 * 1024; \/\/ \u6bcf\u4e2a\u5206\u72471MB\n            const total = Math.ceil(file.size \/ chunkSize);\n\n            for (let i = 0; i &lt; total; i++) {\n                const chunkStart = start + i * chunkSize;\n                const chunkEnd = Math.min(chunkStart + chunkSize, file.size);\n                const chunk = file.slice(chunkStart, chunkEnd);\n\n                const formData = new FormData();\n                formData.append('file', chunk);\n                formData.append('start', chunkStart);\n                formData.append('fileName', file.name);\n\n                const response = await fetch('\/file\/upload2', {\n                    method: 'POST',\n                    body: formData\n                });\n\n                const result = await response.text();\n                if (response.ok) {\n                    console.log(`\u5206\u7247 ${i + 1}\/${total} \u4e0a\u4f20\u6210\u529f`);\n                } else {\n                    console.error(`\u5206\u7247 ${i + 1}\/${total} \u4e0a\u4f20\u5931\u8d25: ${result}`);\n                    break;\n                }\n            }\n        }\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<h2>\u4e94 \u65ad\u70b9\u7eed\u4f20\u5b9e\u6218<\/h2>\n<p>\u65ad\u70b9\u7eed\u4f20\u7684\u6280\u672f\u539f\u7406\u7c7b\u4f3c\u4e8e\u5206\u7247\u4e0a\u4f20\u3002<\/p>\n<p>\u5f53\u6587\u4ef6\u5df2\u7ecf\u4e0a\u4f20\u4e86\u4e00\u90e8\u5206\u4e4b\u540e\uff0c\u65ad\u4e86\u9700\u8981\u91cd\u65b0\u5f00\u59cb\u4e0a\u4f20\u3002<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u7684\u601d\u8def\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<ol>\n<li>\u524d\u7aef\u5148\u53d1\u9001\u4e00\u4e2a\u8bf7\u6c42\uff0c\u68c0\u67e5\u8981\u4e0a\u4f20\u7684\u6587\u4ef6\u5728\u670d\u52a1\u7aef\u662f\u5426\u5df2\u7ecf\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\uff0c\u76ee\u524d\u5927\u5c0f\u662f\u591a\u5c11\u3002<\/li>\n<li>\u524d\u7aef\u6839\u636e\u5df2\u7ecf\u5b58\u5728\u7684\u5927\u5c0f\uff0c\u7ee7\u7eed\u4e0a\u4f20\u6587\u4ef6\u5373\u53ef\u3002<\/li>\n<\/ol>\n<h3>\u540e\u7aef\u6848\u4f8b<\/h3>\n<p>\u5148\u6765\u770b\u540e\u7aef\u68c0\u67e5\u7684\u63a5\u53e3\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@GetMapping(&quot;\/check&quot;)\npublic ResponseEntity&lt;Long&gt; checkFile(@RequestParam(&quot;filename&quot;) String filename) {\n    File file = new File(UPLOAD_DIR + filename);\n    if (file.exists()) {\n        return ResponseEntity.ok(file.length());\n    } else {\n        return ResponseEntity.ok(0L);\n    }\n}<\/code><\/pre>\n<p>\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u5df2\u7ecf\u5b58\u5728\u7684\u6587\u4ef6\u5927\u5c0f\u3002<\/p>\n<p>\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de 0\uff0c\u8868\u793a\u524d\u7aef\u4ece\u5934\u5f00\u59cb\u4e0a\u4f20\u8be5\u6587\u4ef6\u3002<\/p>\n<h3>\u524d\u7aef\u8c03\u7528<\/h3>\n<pre><code class=\"html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;\u65ad\u70b9\u7eed\u4f20\u793a\u4f8b&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;input type=&quot;file&quot; id=&quot;fileInput&quot;\/&gt;\n&lt;button onclick=&quot;startUpload()&quot;&gt;\u5f00\u59cb\u4e0a\u4f20&lt;\/button&gt;\n\n&lt;script&gt;\n    async function startUpload() {\n        const fileInput = document.getElementById('fileInput');\n        const file = fileInput.files[0];\n        if (!file) {\n            alert(&quot;\u8bf7\u9009\u62e9\u6587\u4ef6&quot;);\n            return;\n        }\n\n        const filename = file.name;\n        let start = await checkFile(filename);\n\n        uploadFile(file, start);\n    }\n\n    async function checkFile(filename) {\n        const response = await fetch(`\/file\/check?filename=${filename}`);\n        const start = await response.json();\n        return start;\n    }\n\n    async function uploadFile(file, start) {\n        const chunkSize = 1024 * 1024; \/\/ \u6bcf\u4e2a\u5206\u72471MB\n        const total = Math.ceil((file.size - start) \/ chunkSize);\n\n        for (let i = 0; i &lt; total; i++) {\n            const chunkStart = start + i * chunkSize;\n            const chunkEnd = Math.min(chunkStart + chunkSize, file.size);\n            const chunk = file.slice(chunkStart, chunkEnd);\n\n            const formData = new FormData();\n            formData.append('file', chunk);\n            formData.append('start', chunkStart);\n            formData.append('fileName', file.name);\n\n            const response = await fetch('\/file\/upload2', {\n                method: 'POST',\n                body: formData\n            });\n\n            const result = await response.text();\n            if (response.ok) {\n                console.log(`\u5206\u7247 ${i + 1}\/${total} \u4e0a\u4f20\u6210\u529f`);\n            } else {\n                console.error(`\u5206\u7247 ${i + 1}\/${total} \u4e0a\u4f20\u5931\u8d25: ${result}`);\n                break;\n            }\n        }\n    }\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6848\u4f8b\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u65ad\u70b9\u7eed\u4f20+\u5206\u7247\u4e0a\u4f20\u7684\u6848\u4f8b\uff0c\u76f8\u5173\u77e5\u8bc6\u70b9\u5e76\u4e0d\u96be\uff0c\u5c0f\u4f19\u4f34\u4eec\u53ef\u4ee5\u81ea\u884c\u4f53\u4f1a\u4e0b\u3002<\/p>\n<h2>\u516d \u603b\u7ed3<\/h2>\n<p>\u597d\u4e86\uff0c\u4ee5\u4e0a\u5c31\u662f\u5173\u4e8e\u6587\u4ef6\u4e0a\u4f20\u4e2d\u79d2\u4f20\u3001\u65ad\u70b9\u7eed\u4f20\u548c\u5206\u7247\u4e0a\u4f20\u7684\u5b9e\u6218\u5206\u4eab\u3002\u901a\u8fc7\u8fd9\u4e9b\u6280\u672f\u7684\u5e94\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u5347\u6587\u4ef6\u4e0a\u4f20\u7684\u6548\u7387\u548c\u7a33\u5b9a\u6027\uff0c\u6539\u5584\u7528\u6237\u4f53\u9a8c\u3002\u5e0c\u671b\u5404\u4f4d\u5c0f\u4f19\u4f34\u5728\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\u4e5f\u80fd\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<p>\u672c\u6587\u5b8c\u6574\u6848\u4f8b\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=dZ9%2BwPwi6b4Cpf8YJ4mTwg%3D%3D.AFB1fQyuEhG1QE7uRgnsyNmnSwyP5t1h2AzctEmej9CaLeRYPQ9RFkBvumLY5AyI9pMCU9x0KBQyJmi%2BP%2FPVfUhbfSufyPr36yBgMAGAgu8%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https:\/\/github.com\/lenve\/springboot3-samples\/tree\/main\/file_upload<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000045466286\">https:\/\/segmentfault.com\/a\/1190000045466286<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u51e0\u4e4e\u662f\u6bcf\u4e2a Web \u5e94\u7528\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u65e0\u8bba\u662f\u4e2a\u4eba\u535a\u5ba2\u4e2d\u7684\u56fe\u7247\u4e0a\u4f20\uff0c\u8fd8\u662f\u4f01\u4e1a\u7ea7\u5e94\u7528\u4e2d\u7684\u6587\u6863\u7ba1\u7406\uff0c [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020394","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020394"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020394\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}