{"id":70460,"date":"2026-02-01T23:18:03","date_gmt":"2026-02-01T15:18:03","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/70460.html"},"modified":"2026-02-01T23:18:03","modified_gmt":"2026-02-01T15:18:03","slug":"javaweb%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%88day11%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/70460.html","title":{"rendered":"JavaWeb\u5b66\u4e60\u7b14\u8bb0\uff08Day11\uff09"},"content":{"rendered":"<h4>\u4e00\u3001\u65b0\u589e\u5458\u5de5<\/h4>\n<h5>1. \u4ee3\u7801\u5b9e\u73b0<\/h5>\n<p>(1)\u00a0EmpController<\/p>\n<p>&#064;Slf4j<br \/>\n&#064;RestController<br \/>\n&#064;RequestMapping(&#034;\/emps&#034;)<br \/>\npublic class EmpController {<\/p>\n<p>    &#064;Autowired<br \/>\n    private EmpService empService;<\/p>\n<p>    \/\/\u65b0\u589e<br \/>\n    &#064;PostMapping<br \/>\n    public Result save(&#064;RequestBody Emp emp){<br \/>\n        \/\/\u8bb0\u5f55\u65e5\u5fd7<br \/>\n        log.info(&#034;\u65b0\u589e\u5458\u5de5, emp:{}&#034;,emp);<br \/>\n        \/\/\u8c03\u7528\u4e1a\u52a1\u5c42\u65b0\u589e\u529f\u80fd<br \/>\n        empService.save(emp);<br \/>\n        \/\/\u54cd\u5e94<br \/>\n        return Result.success();<br \/>\n    }<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n} <\/p>\n<p>(2)\u00a0EmpService<\/p>\n<p>public interface EmpService {<\/p>\n<p>    \/**<br \/>\n     * \u4fdd\u5b58\u5458\u5de5\u4fe1\u606f<br \/>\n     * &#064;param emp<br \/>\n     *\/<br \/>\n    void save(Emp emp);<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n}<\/p>\n<p>(3)\u00a0EmpServiceImpl<\/p>\n<p>&#064;Slf4j<br \/>\n&#064;Service<br \/>\npublic class EmpServiceImpl implements EmpService {<br \/>\n    &#064;Autowired<br \/>\n    private EmpMapper empMapper;<\/p>\n<p>    &#064;Override<br \/>\n    public void save(Emp emp) {<br \/>\n        \/\/\u8865\u5168\u6570\u636e<br \/>\n        emp.setCreateTime(LocalDateTime.now());<br \/>\n        emp.setUpdateTime(LocalDateTime.now());<br \/>\n        \/\/\u8c03\u7528\u6dfb\u52a0\u65b9\u6cd5<br \/>\n        empMapper.insert(emp);<br \/>\n    }<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n} <\/p>\n<p>(4)\u00a0EmpMapper<\/p>\n<p>&#064;Mapper<br \/>\npublic interface EmpMapper {<br \/>\n    \/\/\u65b0\u589e\u5458\u5de5<br \/>\n    &#064;Insert(&#034;insert into emp (username, name, gender, image, job, entrydate, dept_id, create_time, update_time) &#034; &#043;<br \/>\n            &#034;values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime});&#034;)<br \/>\n    void insert(Emp emp);<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n}<\/p>\n<h4>\u4e8c\u3001\u6587\u4ef6\u4e0a\u4f20<\/h4>\n<h5>1. \u524d\u7aef\u7a0b\u5e8f<\/h5>\n<h5>(1) \u4ee3\u7801<\/h5>\n<p>&lt;form action&#061;&#034;\/upload&#034; method&#061;&#034;post&#034; enctype&#061;&#034;multipart\/form-data&#034;&gt;<br \/>\n\u59d3\u540d: &lt;input type&#061;&#034;text&#034; name&#061;&#034;username&#034;&gt;&lt;br&gt;<br \/>\n    \u5e74\u9f84: &lt;input type&#061;&#034;text&#034; name&#061;&#034;age&#034;&gt;&lt;br&gt;<br \/>\n    \u5934\u50cf: &lt;input type&#061;&#034;file&#034; name&#061;&#034;image&#034;&gt;&lt;br&gt;<br \/>\n    &lt;input type&#061;&#034;submit&#034; value&#061;&#034;\u63d0\u4ea4&#034;&gt;<br \/>\n&lt;\/form&gt; <\/p>\n<p>\u4e0a\u4f20\u6587\u4ef6\u7684\u539f\u59cbform\u8868\u5355&#xff0c;\u8981\u6c42\u8868\u5355\u5fc5\u987b\u5177\u5907\u4ee5\u4e0b\u4e09\u70b9&#xff08;\u4e0a\u4f20\u6587\u4ef6\u9875\u9762\u4e09\u8981\u7d20&#xff09;&#xff1a;<\/p>\n<p>\u2460 \u8868\u5355\u5fc5\u987b\u6709file\u57df&#xff0c;\u7528\u4e8e\u9009\u62e9\u8981\u4e0a\u4f20\u7684\u6587\u4ef6<\/p>\n<p>&lt;input type&#061;&#034;file&#034; name&#061;&#034;image&#034;\/&gt; <\/p>\n<p>\u2461\u00a0\u8868\u5355\u63d0\u4ea4\u65b9\u5f0f\u5fc5\u987b\u4e3a\u00a0POST<\/p>\n<p>\u901a\u5e38\u4e0a\u4f20\u7684\u6587\u4ef6\u4f1a\u6bd4\u8f83\u5927&#xff0c;\u6240\u4ee5\u9700\u8981\u4f7f\u7528 POST \u63d0\u4ea4\u65b9\u5f0f<\/p>\n<p>\u2462\u00a0\u8868\u5355\u7684\u7f16\u7801\u7c7b\u578benctype\u5fc5\u987b\u8981\u8bbe\u7f6e\u4e3a&#xff1a;multipart\/form-data<\/p>\n<p>\u666e\u901a\u9ed8\u8ba4\u7684\u7f16\u7801\u683c\u5f0f\u662f\u4e0d\u9002\u5408\u4f20\u8f93\u5927\u578b\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u7684&#xff0c;\u6240\u4ee5\u5728\u6587\u4ef6\u4e0a\u4f20\u65f6&#xff0c;\u8868\u5355\u7684\u7f16\u7801\u683c\u5f0f\u5fc5\u987b\u8bbe\u7f6e\u4e3amultipart\/form-data<\/p>\n<h5>2. \u540e\u7aef\u7a0b\u5e8f<\/h5>\n<h5>(1) \u601d\u8def\u5206\u6790<\/h5>\n<p>\u2460\u00a0\u5728\u670d\u52a1\u7aef\u5b9a\u4e49\u8fd9\u4e48\u4e00\u4e2acontroller&#xff0c;\u7528\u6765\u8fdb\u884c\u6587\u4ef6\u4e0a\u4f20&#xff0c;\u7136\u540e\u5728controller\u5f53\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u6765\u5904\u7406\/upload \u8bf7\u6c42<\/p>\n<p>\u2461\u00a0\u5728\u5b9a\u4e49\u7684\u65b9\u6cd5\u4e2d\u63a5\u6536\u63d0\u4ea4\u8fc7\u6765\u7684\u6570\u636e &#xff08;\u65b9\u6cd5\u4e2d\u7684\u5f62\u53c2\u540d\u548c\u8bf7\u6c42\u53c2\u6570\u7684\u540d\u5b57\u4fdd\u6301\u4e00\u81f4&#xff09;<\/p>\n<ul>\n<li>\n<p>\u7528\u6237\u540d&#xff1a;String name<\/p>\n<\/li>\n<li>\n<p>\u5e74\u9f84&#xff1a; Integer age<\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6&#xff1a; MultipartFile image<\/p>\n<\/li>\n<\/ul>\n<p>Spring\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2aAPI&#xff1a;MultipartFile&#xff0c;\u4f7f\u7528\u8fd9\u4e2aAPI\u5c31\u53ef\u4ee5\u6765\u63a5\u6536\u5230\u4e0a\u4f20\u7684\u6587\u4ef6<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"779\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201151800-697f6ea86fd77.png\" width=\"1687\" \/><\/p>\n<h5>(2) \u4ee3\u7801\u5b9e\u73b0<\/h5>\n<p>&#064;Slf4j<br \/>\n&#064;RestController<br \/>\npublic class UploadController {<\/p>\n<p>    &#064;PostMapping(&#034;\/upload&#034;)<br \/>\n    public Result upload(String username, Integer age, MultipartFile image)  {<br \/>\n        log.info(&#034;\u6587\u4ef6\u4e0a\u4f20&#xff1a;{},{},{}&#034;,username,age,image);<br \/>\n        return Result.success();<br \/>\n    }<\/p>\n<p>} <\/p>\n<h5>3. \u672c\u5730\u5b58\u50a8<\/h5>\n<h5>(1) \u601d\u8def\u5206\u6790<\/h5>\n<p>\u2460 \u5728\u670d\u52a1\u5668\u672c\u5730\u78c1\u76d8\u4e0a\u521b\u5efaimages\u76ee\u5f55&#xff0c;\u7528\u6765\u5b58\u50a8\u4e0a\u4f20\u7684\u6587\u4ef6&#xff08;\u4f8b&#xff1a;E\u76d8\u521b\u5efaimages\u76ee\u5f55&#xff09;<\/p>\n<p>\u2461 \u4f7f\u7528MultipartFile\u7c7b\u63d0\u4f9b\u7684API\u65b9\u6cd5&#xff0c;\u628a\u4e34\u65f6\u6587\u4ef6\u8f6c\u5b58\u5230\u672c\u5730\u78c1\u76d8\u76ee\u5f55\u4e0b<\/p>\n<ul>\n<li>\n<p>String getOriginalFilename(); \/\/\u83b7\u53d6\u539f\u59cb\u6587\u4ef6\u540d<\/p>\n<\/li>\n<li>\n<p>void transferTo(File dest); \/\/\u5c06\u63a5\u6536\u7684\u6587\u4ef6\u8f6c\u5b58\u5230\u78c1\u76d8\u6587\u4ef6\u4e2d<\/p>\n<\/li>\n<li>\n<p>long getSize(); \/\/\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f&#xff0c;\u5355\u4f4d&#xff1a;\u5b57\u8282<\/p>\n<\/li>\n<li>\n<p>byte[] getBytes(); \/\/\u83b7\u53d6\u6587\u4ef6\u5185\u5bb9\u7684\u5b57\u8282\u6570\u7ec4<\/p>\n<\/li>\n<li>\n<p>InputStream getInputStream(); \/\/\u83b7\u53d6\u63a5\u6536\u5230\u7684\u6587\u4ef6\u5185\u5bb9\u7684\u8f93\u5165\u6d41<\/p>\n<\/li>\n<\/ul>\n<h5>(2) \u4ee3\u7801\u5b9e\u73b0<\/h5>\n<p>&#064;Slf4j<br \/>\n&#064;RestController<br \/>\npublic class UploadController {<\/p>\n<p>    &#064;PostMapping(&#034;\/upload&#034;)<br \/>\n    public Result upload(String username, Integer age, MultipartFile image) throws IOException {<br \/>\n        log.info(&#034;\u6587\u4ef6\u4e0a\u4f20&#xff1a;{},{},{}&#034;,username,age,image);<\/p>\n<p>        \/\/\u83b7\u53d6\u539f\u59cb\u6587\u4ef6\u540d<br \/>\n        String originalFilename &#061; image.getOriginalFilename();<\/p>\n<p>        \/\/\u6784\u5efa\u65b0\u7684\u6587\u4ef6\u540d<br \/>\n        String extname &#061; originalFilename.substring(originalFilename.lastIndexOf(&#034;.&#034;));\/\/\u6587\u4ef6\u6269\u5c55\u540d<br \/>\n        String newFileName &#061; UUID.randomUUID().toString()&#043;extname;\/\/\u968f\u673a\u540d&#043;\u6587\u4ef6\u6269\u5c55\u540d<\/p>\n<p>        \/\/\u5c06\u6587\u4ef6\u5b58\u50a8\u5728\u670d\u52a1\u5668\u7684\u78c1\u76d8\u76ee\u5f55<br \/>\n        image.transferTo(new File(&#034;E:\/images\/&#034;&#043;newFileName));<\/p>\n<p>        return Result.success();<br \/>\n    }<\/p>\n<p>} <\/p>\n<p>\u6838\u5fc3\u903b\u8f91&#xff1a;<\/p>\n<p>\u2460\u00a0originalFilename.lastIndexOf(&#034;.&#034;)&#xff1a;\u627e\u5230\u539f\u59cb\u6587\u4ef6\u540d\u4e2d\u6700\u540e\u4e00\u4e2a.\u7684\u7d22\u5f15\u4f4d\u7f6e<\/p>\n<ul>\n<li>\u6587\u4ef6\u540d\u53ef\u80fd\u5305\u542b\u591a\u4e2a.&#xff08;\u6bd4\u5982doc.123.pdf&#xff09;&#xff0c;\u53ea\u6709\u53d6\u6700\u540e\u4e00\u4e2a.&#xff0c;\u624d\u80fd\u6b63\u786e\u62ff\u5230\u6587\u4ef6\u7684\u771f\u5b9e\u6269\u5c55\u540d&#xff08;.pdf&#xff09;&#xff0c;\u5982\u679c\u53d6\u7b2c\u4e00\u4e2a.\u4f1a\u5f97\u5230\u9519\u8bef\u7684.123\u3002<\/li>\n<li>substring(\u7d22\u5f15\u4f4d\u7f6e)&#xff1a;\u4ece\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u5f00\u59cb&#xff0c;\u622a\u53d6\u5b57\u7b26\u4e32\u5230\u672b\u5c3e&#xff0c;\u6700\u7ec8\u5f97\u5230\u5e26.\u7684\u6587\u4ef6\u6269\u5c55\u540d\u3002<\/li>\n<\/ul>\n<p>\u2461\u00a0String newFileName &#061; UUID.randomUUID().toString()&#043;extname;<\/p>\n<ul>\n<li>UUID&#xff1a;Java \u63d0\u4f9b\u7684\u901a\u7528\u552f\u4e00\u8bc6\u522b\u7801\u5de5\u5177\u7c7b&#xff0c;\u80fd\u751f\u6210\u5168\u7403\u552f\u4e00\u7684 32 \u4f4d &#043; \u6a2a\u7ebf\u7684\u5b57\u7b26\u4e32&#xff08;\u6bd4\u5982f2a7c90a-8e05-4f3a-91b6-7d28901e2345&#xff09;&#xff0c;\u65e0\u8bba\u591a\u5c11\u6b21\u751f\u6210&#xff0c;\u90fd\u4e0d\u4f1a\u51fa\u73b0\u91cd\u590d&#xff1b;<\/li>\n<li>UUID.randomUUID()&#xff1a;\u751f\u6210\u4e00\u4e2a UUID \u968f\u673a\u5bf9\u8c61&#xff1b;<\/li>\n<\/ul>\n<p>(3)\u00a0\u6211\u4eec\u518d\u6b21\u4e0a\u4f20\u4e00\u4e2a\u8f83\u5927\u7684\u6587\u4ef6(\u8d85\u51fa1M)\u65f6\u53d1\u73b0&#xff0c;\u540e\u7aef\u7a0b\u5e8f\u62a5\u9519&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"186\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201151801-697f6ea907b83.png\" width=\"1235\" \/><\/p>\n<p>\u62a5\u9519\u539f\u56e0&#xff1a;\u5728SpringBoot\u4e2d&#xff0c;\u6587\u4ef6\u4e0a\u4f20\u65f6\u9ed8\u8ba4\u5355\u4e2a\u6587\u4ef6\u6700\u5927\u5927\u5c0f\u4e3a1M<\/p>\n<p>\u90a3\u4e48\u5982\u679c\u9700\u8981\u4e0a\u4f20\u5927\u6587\u4ef6&#xff0c;\u53ef\u4ee5\u5728application.properties\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e<\/p>\n<p>#\u914d\u7f6e\u5355\u4e2a\u6587\u4ef6\u6700\u5927\u4e0a\u4f20\u5927\u5c0f<br \/>\nspring.servlet.multipart.max-file-size&#061;10MB<\/p>\n<p>#\u914d\u7f6e\u5355\u4e2a\u8bf7\u6c42\u6700\u5927\u4e0a\u4f20\u5927\u5c0f(\u4e00\u6b21\u8bf7\u6c42\u53ef\u4ee5\u4e0a\u4f20\u591a\u4e2a\u6587\u4ef6)<br \/>\nspring.servlet.multipart.max-request-size&#061;100MB <\/p>\n<h5>4. \u963f\u91cc\u4e91OSS<\/h5>\n<h5>(1)\u00a0\u53c2\u7167\u5b98\u65b9\u63d0\u4f9b\u7684SDK&#xff0c;\u6539\u9020\u4e00\u4e0b&#xff0c;\u5373\u53ef\u5b9e\u73b0\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd&#xff1a;<\/h5>\n<p>import com.aliyun.oss.ClientException;<br \/>\nimport com.aliyun.oss.OSS;<br \/>\nimport com.aliyun.oss.OSSClientBuilder;<br \/>\nimport com.aliyun.oss.OSSException;<br \/>\nimport com.aliyun.oss.model.PutObjectRequest;<br \/>\nimport com.aliyun.oss.model.PutObjectResult;<\/p>\n<p>import java.io.FileInputStream;<br \/>\nimport java.io.InputStream;<\/p>\n<p>public class AliOssTest {<br \/>\n    public static void main(String[] args) throws Exception {<br \/>\n        \/\/ Endpoint\u4ee5\u534e\u4e1c1&#xff08;\u676d\u5dde&#xff09;\u4e3a\u4f8b&#xff0c;\u5176\u5b83Region\u8bf7\u6309\u5b9e\u9645\u60c5\u51b5\u586b\u5199\u3002<br \/>\n        String endpoint &#061; &#034;oss-cn-shanghai.aliyuncs.com&#034;;<\/p>\n<p>        \/\/ \u963f\u91cc\u4e91\u8d26\u53f7AccessKey\u62e5\u6709\u6240\u6709API\u7684\u8bbf\u95ee\u6743\u9650&#xff0c;\u98ce\u9669\u5f88\u9ad8\u3002\u5f3a\u70c8\u5efa\u8bae\u60a8\u521b\u5efa\u5e76\u4f7f\u7528RAM\u7528\u6237\u8fdb\u884cAPI\u8bbf\u95ee\u6216\u65e5\u5e38\u8fd0\u7ef4&#xff0c;\u8bf7\u767b\u5f55RAM\u63a7\u5236\u53f0\u521b\u5efaRAM\u7528\u6237\u3002<br \/>\n        String accessKeyId &#061; &#034;LTAI5t9MZK8iq5T2Av5GLDxX&#034;;<br \/>\n        String accessKeySecret &#061; &#034;C0IrHzKZGKqU8S7YQcevcotD3Zd5Tc&#034;;<\/p>\n<p>        \/\/ \u586b\u5199Bucket\u540d\u79f0&#xff0c;\u4f8b\u5982examplebucket\u3002<br \/>\n        String bucketName &#061; &#034;web-framework01&#034;;<br \/>\n        \/\/ \u586b\u5199Object\u5b8c\u6574\u8def\u5f84&#xff0c;\u5b8c\u6574\u8def\u5f84\u4e2d\u4e0d\u80fd\u5305\u542bBucket\u540d\u79f0&#xff0c;\u4f8b\u5982exampledir\/exampleobject.txt\u3002<br \/>\n        String objectName &#061; &#034;1.jpg&#034;;<br \/>\n        \/\/ \u586b\u5199\u672c\u5730\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84&#xff0c;\u4f8b\u5982D:\\\\\\\\localpath\\\\\\\\examplefile.txt\u3002<br \/>\n        \/\/ \u5982\u679c\u672a\u6307\u5b9a\u672c\u5730\u8def\u5f84&#xff0c;\u5219\u9ed8\u8ba4\u4ece\u793a\u4f8b\u7a0b\u5e8f\u6240\u5c5e\u9879\u76ee\u5bf9\u5e94\u672c\u5730\u8def\u5f84\u4e2d\u4e0a\u4f20\u6587\u4ef6\u6d41\u3002<br \/>\n        String filePath&#061; &#034;C:\\\\\\\\Users\\\\\\\\Administrator\\\\\\\\Pictures\\\\\\\\1.jpg&#034;;<\/p>\n<p>        \/\/ \u521b\u5efaOSSClient\u5b9e\u4f8b\u3002<br \/>\n        OSS ossClient &#061; new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);<\/p>\n<p>        try {<br \/>\n            InputStream inputStream &#061; new FileInputStream(filePath);<br \/>\n            \/\/ \u521b\u5efaPutObjectRequest\u5bf9\u8c61\u3002<br \/>\n            PutObjectRequest putObjectRequest &#061; new PutObjectRequest(bucketName, objectName, inputStream);<br \/>\n            \/\/ \u8bbe\u7f6e\u8be5\u5c5e\u6027\u53ef\u4ee5\u8fd4\u56deresponse\u3002\u5982\u679c\u4e0d\u8bbe\u7f6e&#xff0c;\u5219\u8fd4\u56de\u7684response\u4e3a\u7a7a\u3002<br \/>\n            putObjectRequest.setProcess(&#034;true&#034;);<br \/>\n            \/\/ \u521b\u5efaPutObject\u8bf7\u6c42\u3002<br \/>\n            PutObjectResult result &#061; ossClient.putObject(putObjectRequest);<br \/>\n            \/\/ \u5982\u679c\u4e0a\u4f20\u6210\u529f&#xff0c;\u5219\u8fd4\u56de200\u3002<br \/>\n            System.out.println(result.getResponse().getStatusCode());<br \/>\n        } catch (OSSException oe) {<br \/>\n            System.out.println(&#034;Caught an OSSException, which means your request made it to OSS, &#034;<br \/>\n                    &#043; &#034;but was rejected with an error response for some reason.&#034;);<br \/>\n            System.out.println(&#034;Error Message:&#034; &#043; oe.getErrorMessage());<br \/>\n            System.out.println(&#034;Error Code:&#034; &#043; oe.getErrorCode());<br \/>\n            System.out.println(&#034;Request ID:&#034; &#043; oe.getRequestId());<br \/>\n            System.out.println(&#034;Host ID:&#034; &#043; oe.getHostId());<br \/>\n        } catch (ClientException ce) {<br \/>\n            System.out.println(&#034;Caught an ClientException, which means the client encountered &#034;<br \/>\n                    &#043; &#034;a serious internal problem while trying to communicate with OSS, &#034;<br \/>\n                    &#043; &#034;such as not being able to access the network.&#034;);<br \/>\n            System.out.println(&#034;Error Message:&#034; &#043; ce.getMessage());<br \/>\n        } finally {<br \/>\n            if (ossClient !&#061; null) {<br \/>\n                ossClient.shutdown();<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d&#xff0c;\u9700\u8981\u66ff\u6362\u7684\u5185\u5bb9\u4e3a&#xff1a;<\/p>\n<ul>\n<li>\n<p>accessKeyId&#xff1a;\u963f\u91cc\u4e91\u8d26\u53f7AccessKey<\/p>\n<\/li>\n<li>\n<p>accessKeySecret&#xff1a;\u963f\u91cc\u4e91\u8d26\u53f7AccessKey\u5bf9\u5e94\u7684\u79d8\u94a5<\/p>\n<\/li>\n<li>\n<p>bucketName&#xff1a;Bucket\u540d\u79f0<\/p>\n<\/li>\n<li>\n<p>objectName&#xff1a;\u5bf9\u8c61\u540d\u79f0&#xff0c;\u5728Bucket\u4e2d\u5b58\u50a8\u7684\u5bf9\u8c61\u7684\u540d\u79f0<\/p>\n<\/li>\n<li>\n<p>filePath&#xff1a;\u6587\u4ef6\u8def\u5f84<\/p>\n<\/li>\n<\/ul>\n<h5>(2) \u96c6\u6210<\/h5>\n<h5>\u2460 \u4ee3\u7801\u5b9e\u73b0<\/h5>\n<p>UploadController&#xff1a;<\/p>\n<p>import com.itheima.pojo.Result;<br \/>\nimport com.itheima.utils.AliOSSUtils;<br \/>\nimport lombok.extern.slf4j.Slf4j;<br \/>\nimport org.springframework.beans.factory.annotation.Autowired;<br \/>\nimport org.springframework.web.bind.annotation.PostMapping;<br \/>\nimport org.springframework.web.bind.annotation.RestController;<br \/>\nimport org.springframework.web.multipart.MultipartFile;<br \/>\nimport java.io.IOException;<\/p>\n<p>&#064;Slf4j<br \/>\n&#064;RestController<br \/>\npublic class UploadController {<\/p>\n<p>    &#064;Autowired<br \/>\n    private AliOSSUtils aliOSSUtils;<\/p>\n<p>    &#064;PostMapping(&#034;\/upload&#034;)<br \/>\n    public Result upload(MultipartFile image) throws IOException {<br \/>\n        \/\/\u8c03\u7528\u963f\u91cc\u4e91OSS\u5de5\u5177\u7c7b&#xff0c;\u5c06\u4e0a\u4f20\u4e0a\u6765\u7684\u6587\u4ef6\u5b58\u5165\u963f\u91cc\u4e91<br \/>\n        String url &#061; aliOSSUtils.upload(image);<br \/>\n        \/\/\u5c06\u56fe\u7247\u4e0a\u4f20\u5b8c\u6210\u540e\u7684url\u8fd4\u56de&#xff0c;\u7528\u4e8e\u6d4f\u89c8\u5668\u56de\u663e\u5c55\u793a<br \/>\n        return Result.success(url);<br \/>\n    }<\/p>\n<p>} <\/p>\n<h4>\u4e09\u3001\u4fee\u6539\u5458\u5de5<\/h4>\n<h5>1.\u00a0 \u67e5\u8be2\u56de\u663e<\/h5>\n<h5 style=\"background-color:transparent\">(1) \u4ee3\u7801\u5b9e\u73b0<\/h5>\n<p>\u2460\u00a0EmpMapper<\/p>\n<p>&#064;Mapper<br \/>\npublic interface EmpMapper {<\/p>\n<p>    \/\/\u6839\u636eID\u67e5\u8be2\u5458\u5de5\u4fe1\u606f<br \/>\n    &#064;Select(&#034;select id, username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time &#034; &#043;<br \/>\n            &#034;from emp &#034; &#043;<br \/>\n            &#034;where id &#061; #{id}&#034;)<br \/>\n    public Emp findById(Integer id);<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n} <\/p>\n<p>\u2461\u00a0EmpService<\/p>\n<p>public interface EmpService {<\/p>\n<p>    \/**<br \/>\n     * \u6839\u636eID\u67e5\u8be2\u5458\u5de5<br \/>\n     * &#064;param id<br \/>\n     * &#064;return<br \/>\n     *\/<br \/>\n    public Emp getById(Integer id);<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n} <\/p>\n<p>\u2462\u00a0EmpServiceImpl<\/p>\n<p>&#064;Slf4j<br \/>\n&#064;Service<br \/>\npublic class EmpServiceImpl implements EmpService {<br \/>\n    &#064;Autowired<br \/>\n    private EmpMapper empMapper;<\/p>\n<p>    &#064;Override<br \/>\n    public Emp getById(Integer id) {<br \/>\n        return empMapper.findById(id);<br \/>\n    }<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n} <\/p>\n<p>\u7531\u4e8e empMapper \u91cc\u7684 findById() \u662f\u6709\u8fd4\u56de\u503c\u7684&#xff0c;\u6240\u4ee5\u8981\u52a0\u4e2a return\u00a0\u00a0<\/p>\n<p>\u2463\u00a0EmpController<\/p>\n<p>&#064;Slf4j<br \/>\n&#064;RestController<br \/>\n&#064;RequestMapping(&#034;\/emps&#034;)<br \/>\npublic class EmpController {<\/p>\n<p>    &#064;Autowired<br \/>\n    private EmpService empService;<\/p>\n<p>    \/\/\u6839\u636eid\u67e5\u8be2<br \/>\n    &#064;GetMapping(&#034;\/{id}&#034;)<br \/>\n    public Result getById(&#064;PathVariable Integer id){<br \/>\n        Emp emp &#061; empService.getById(id);<br \/>\n        return Result.success(emp);<br \/>\n    }<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n} <\/p>\n<h5>2. \u4fee\u6539\u5458\u5de5<\/h5>\n<h5>(1) \u4ee3\u7801\u5b9e\u73b0<\/h5>\n<p>\u2460\u00a0EmpMapper<\/p>\n<p>&#064;Mapper<br \/>\npublic interface EmpMapper {<br \/>\n    \/\/\u4fee\u6539\u5458\u5de5\u4fe1\u606f<br \/>\n    public void update(Emp emp);<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n} <\/p>\n<p>\u2461\u00a0EmpMapper.xml<\/p>\n<p>&lt;?xml version&#061;&#034;1.0&#034; encoding&#061;&#034;UTF-8&#034; ?&gt;<br \/>\n&lt;!DOCTYPE mapper<br \/>\n        PUBLIC &#034;-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN&#034;<br \/>\n        &#034;http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd&#034;&gt;<br \/>\n&lt;mapper namespace&#061;&#034;com.itheima.mapper.EmpMapper&#034;&gt;<\/p>\n<p>    &lt;!&#8211;\u66f4\u65b0\u5458\u5de5\u4fe1\u606f&#8211;&gt;<br \/>\n    &lt;update id&#061;&#034;update&#034;&gt;<br \/>\n        update emp<br \/>\n        &lt;set&gt;<br \/>\n            &lt;if test&#061;&#034;username !&#061; null and username !&#061; &#039;&#039;&#034;&gt;<br \/>\n                username &#061; #{username},<br \/>\n            &lt;\/if&gt;<br \/>\n            &lt;if test&#061;&#034;password !&#061; null and password !&#061; &#039;&#039;&#034;&gt;<br \/>\n                password &#061; #{password},<br \/>\n            &lt;\/if&gt;<br \/>\n            &lt;if test&#061;&#034;name !&#061; null and name !&#061; &#039;&#039;&#034;&gt;<br \/>\n                name &#061; #{name},<br \/>\n            &lt;\/if&gt;<br \/>\n            &lt;if test&#061;&#034;gender !&#061; null&#034;&gt;<br \/>\n                gender &#061; #{gender},<br \/>\n            &lt;\/if&gt;<br \/>\n            &lt;if test&#061;&#034;image !&#061; null and image !&#061; &#039;&#039;&#034;&gt;<br \/>\n                image &#061; #{image},<br \/>\n            &lt;\/if&gt;<br \/>\n            &lt;if test&#061;&#034;job !&#061; null&#034;&gt;<br \/>\n                job &#061; #{job},<br \/>\n            &lt;\/if&gt;<br \/>\n            &lt;if test&#061;&#034;entrydate !&#061; null&#034;&gt;<br \/>\n                entrydate &#061; #{entrydate},<br \/>\n            &lt;\/if&gt;<br \/>\n            &lt;if test&#061;&#034;deptId !&#061; null&#034;&gt;<br \/>\n                dept_id &#061; #{deptId},<br \/>\n            &lt;\/if&gt;<br \/>\n            &lt;if test&#061;&#034;updateTime !&#061; null&#034;&gt;<br \/>\n                update_time &#061; #{updateTime}<br \/>\n            &lt;\/if&gt;<br \/>\n        &lt;\/set&gt;<br \/>\n        where id &#061; #{id}<br \/>\n    &lt;\/update&gt;<\/p>\n<p>    &lt;!&#8211; \u7701\u7565&#8230; &#8211;&gt;<\/p>\n<p>&lt;\/mapper&gt; <\/p>\n<p>\u2462\u00a0EmpService<\/p>\n<p>public interface EmpService {<br \/>\n    \/**<br \/>\n     * \u66f4\u65b0\u5458\u5de5<br \/>\n     * &#064;param emp<br \/>\n     *\/<br \/>\n    public void update(Emp emp);<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n} <\/p>\n<p>\u2463\u00a0EmpServiceImpl<\/p>\n<p>&#064;Slf4j<br \/>\n&#064;Service<br \/>\npublic class EmpServiceImpl implements EmpService {<br \/>\n    &#064;Autowired<br \/>\n    private EmpMapper empMapper;<\/p>\n<p>    &#064;Override<br \/>\n    public void update(Emp emp) {<br \/>\n        emp.setUpdateTime(LocalDateTime.now()); \/\/\u66f4\u65b0\u4fee\u6539\u65f6\u95f4\u4e3a\u5f53\u524d\u65f6\u95f4<\/p>\n<p>        empMapper.update(emp);<br \/>\n    }<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n} <\/p>\n<p>\u2464\u00a0EmpController<\/p>\n<p>&#064;Slf4j<br \/>\n&#064;RestController<br \/>\n&#064;RequestMapping(&#034;\/emps&#034;)<br \/>\npublic class EmpController {<\/p>\n<p>    &#064;Autowired<br \/>\n    private EmpService empService;<\/p>\n<p>    \/\/\u4fee\u6539\u5458\u5de5<br \/>\n    &#064;PutMapping<br \/>\n    public Result update(&#064;RequestBody Emp emp){<br \/>\n        empService.update(emp);<br \/>\n        return Result.success();<br \/>\n    }<\/p>\n<p>    \/\/\u7701\u7565&#8230;<br \/>\n} <\/p>\n<h4>\u56db\u3001\u914d\u7f6e\u6587\u4ef6<\/h4>\n<h5>1. \u53c2\u6570\u914d\u7f6e\u5316<\/h5>\n<h5>(1) \u5b58\u5728\u7684\u95ee\u9898&#xff1a;<\/h5>\n<p>\u5982\u679c\u6211\u4eec\u5f00\u53d1\u7684\u662f\u4e00\u4e2a\u771f\u5b9e\u7684\u4f01\u4e1a\u7ea7\u9879\u76ee&#xff0c; Java\u7c7b\u53ef\u80fd\u4f1a\u6709\u5f88\u591a&#xff0c;\u5982\u679c\u5c06\u8fd9\u4e9b\u53c2\u6570\u5206\u6563\u7684\u5b9a\u4e49\u5728\u5404\u4e2aJava\u7c7b\u5f53\u4e2d&#xff0c;\u6211\u4eec\u8981\u4fee\u6539\u4e00\u4e2a\u53c2\u6570\u503c&#xff0c;\u6211\u4eec\u5c31\u9700\u8981\u5728\u4f17\u591a\u7684Java\u4ee3\u7801\u5f53\u4e2d\u6765\u5b9a\u4f4d\u5230\u5bf9\u5e94\u7684\u4f4d\u7f6e&#xff0c;\u518d\u6765\u4fee\u6539\u53c2\u6570&#xff0c;\u4fee\u6539\u5b8c\u6bd5\u4e4b\u540e\u518d\u91cd\u65b0\u7f16\u8bd1\u518d\u8fd0\u884c\u3002&#xff08;\u53c2\u6570\u914d\u7f6e\u8fc7\u4e8e\u5206\u6563&#xff0c;\u662f\u4e0d\u65b9\u4fbf\u96c6\u4e2d\u7684\u7ba1\u7406\u548c\u7ef4\u62a4&#xff09;<\/p>\n<h5>(2) \u89e3\u51b3\u65b9\u6848&#xff1a;<\/h5>\n<p>\u2460\u00a0\u6211\u4eec\u53ef\u4ee5\u5c06\u53c2\u6570\u914d\u7f6e\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u3002\u5982\u4e0b&#xff1a;<\/p>\n<p>#\u81ea\u5b9a\u4e49\u7684\u963f\u91cc\u4e91OSS\u914d\u7f6e\u4fe1\u606f<br \/>\naliyun.oss.endpoint&#061;https:\/\/oss-cn-hangzhou.aliyuncs.com<br \/>\naliyun.oss.accessKeyId&#061;LTAI4GCH1vX6DKqJWxd6nEuW<br \/>\naliyun.oss.accessKeySecret&#061;yBshYweHOpqDuhCArrVHwIiBKpyqSL<br \/>\naliyun.oss.bucketName&#061;web-tlias <\/p>\n<p>\u5728\u5c06\u963f\u91cc\u4e91OSS\u914d\u7f6e\u53c2\u6570\u4ea4\u7ed9properties\u914d\u7f6e\u6587\u4ef6\u6765\u7ba1\u7406\u4e4b\u540e&#xff0c;\u6211\u4eec\u7684AliOSSUtils\u5de5\u5177\u7c7b\u5c31\u53d8\u4e3a\u4ee5\u4e0b\u5f62\u5f0f&#xff1a;<\/p>\n<p>&#064;Component<br \/>\npublic class AliOSSUtils {<br \/>\n    \/*\u4ee5\u4e0b4\u4e2a\u53c2\u6570\u6ca1\u6709\u6307\u5b9a\u503c&#xff08;\u9ed8\u8ba4\u503c&#xff1a;null&#xff09;*\/<br \/>\n    private String endpoint;<br \/>\n    private String accessKeyId;<br \/>\n    private String accessKeySecret;<br \/>\n    private String bucketName;<\/p>\n<p>    \/\/\u7701\u7565\u5176\u4ed6\u4ee3\u7801&#8230;<br \/>\n} <\/p>\n<p>\u2461\u00a0\u56e0\u4e3aapplication.properties\u662fspringboot\u9879\u76ee\u9ed8\u8ba4\u7684\u914d\u7f6e\u6587\u4ef6&#xff0c;\u6240\u4ee5springboot\u7a0b\u5e8f\u5728\u542f\u52a8\u65f6\u4f1a\u9ed8\u8ba4\u8bfb\u53d6application.properties\u914d\u7f6e\u6587\u4ef6&#xff0c;\u800c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u73b0\u6210\u7684\u6ce8\u89e3&#xff1a;&#064;Value&#xff0c;\u83b7\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p>&#064;Value \u6ce8\u89e3\u901a\u5e38\u7528\u4e8e\u5916\u90e8\u914d\u7f6e\u7684\u5c5e\u6027\u6ce8\u5165&#xff0c;\u5177\u4f53\u7528\u6cd5\u4e3a&#xff1a; &#064;Value(&#034;${\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684key}&#034;)<\/p>\n<p>&#064;Component<br \/>\npublic class AliOSSUtils {<\/p>\n<p>    &#064;Value(&#034;${aliyun.oss.endpoint}&#034;)<br \/>\n    private String endpoint;<\/p>\n<p>    &#064;Value(&#034;${aliyun.oss.accessKeyId}&#034;)<br \/>\n    private String accessKeyId;<\/p>\n<p>    &#064;Value(&#034;${aliyun.oss.accessKeySecret}&#034;)<br \/>\n    private String accessKeySecret;<\/p>\n<p>    &#064;Value(&#034;${aliyun.oss.bucketName}&#034;)<br \/>\n    private String bucketName;<\/p>\n<p> \/\/\u7701\u7565\u5176\u4ed6\u4ee3\u7801&#8230;<br \/>\n }    <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"547\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201151801-697f6ea92b77a.png\" width=\"1176\" \/><\/p>\n<h5>2. yml\u914d\u7f6e\u6587\u4ef6<\/h5>\n<h5>(1) \u914d\u7f6e\u683c\u5f0f<\/h5>\n<p>application.yml<\/p>\n<p>server:<br \/>\n  port: 8080<br \/>\n  address: 127.0.0.1 <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"510\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201151801-697f6ea968a99.png\" width=\"1209\" \/><\/p>\n<h5>(2) \u57fa\u672c\u8bed\u6cd5<\/h5>\n<ul>\n<li>\n<p>\u5927\u5c0f\u5199\u654f\u611f<\/p>\n<\/li>\n<li>\n<p>\u6570\u503c\u524d\u8fb9\u5fc5\u987b\u6709\u7a7a\u683c&#xff0c;\u4f5c\u4e3a\u5206\u9694\u7b26<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u7f29\u8fdb\u8868\u793a\u5c42\u7ea7\u5173\u7cfb&#xff0c;\u7f29\u8fdb\u65f6&#xff0c;\u4e0d\u5141\u8bb8\u4f7f\u7528Tab\u952e&#xff0c;\u53ea\u80fd\u7528\u7a7a\u683c&#xff08;idea\u4e2d\u4f1a\u81ea\u52a8\u5c06Tab\u8f6c\u6362\u4e3a\u7a7a\u683c&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u7f29\u8fdb\u7684\u7a7a\u683c\u6570\u76ee\u4e0d\u91cd\u8981&#xff0c;\u53ea\u8981\u76f8\u540c\u5c42\u7ea7\u7684\u5143\u7d20\u5de6\u4fa7\u5bf9\u9f50\u5373\u53ef<\/p>\n<\/li>\n<li>\n<p>#\u8868\u793a\u6ce8\u91ca&#xff0c;\u4ece\u8fd9\u4e2a\u5b57\u7b26\u4e00\u76f4\u5230\u884c\u5c3e&#xff0c;\u90fd\u4f1a\u88ab\u89e3\u6790\u5668\u5ffd\u7565<\/p>\n<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"150\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201151801-697f6ea9a5519.png\" width=\"1205\" \/><\/p>\n<h5>(3) yml\u7684\u6570\u636e\u683c\u5f0f<\/h5>\n<p>\u2460\u00a0\u5bf9\u8c61\/Map\u96c6\u5408<\/p>\n<p>user:<br \/>\n  name: zhangsan<br \/>\n  age: 18<br \/>\n  password: 123456 <\/p>\n<p>\u2461\u00a0\u6570\u7ec4\/List\/Set\u96c6\u5408<\/p>\n<p>hobby:<br \/>\n  &#8211; java<br \/>\n  &#8211; game<br \/>\n  &#8211; sport <\/p>\n<h5>(4)\u00a0\u65b0\u5efa\u7684application.yml\u6587\u4ef6&#xff1a;<\/h5>\n<p>spring:<br \/>\n  datasource:<br \/>\n    driver-class-name: com.mysql.cj.jdbc.Driver<br \/>\n    url: jdbc:mysql:\/\/localhost:3306\/tlias<br \/>\n    username: root<br \/>\n    password: 1234<br \/>\n  servlet:<br \/>\n    multipart:<br \/>\n      max-file-size: 10MB<br \/>\n      max-request-size: 100MB<\/p>\n<p>mybatis:<br \/>\n  configuration:<br \/>\n    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl<br \/>\n    map-underscore-to-camel-case: true<\/p>\n<p>aliyun:<br \/>\n  oss:<br \/>\n    endpoint: https:\/\/oss-cn-hangzhou.aliyuncs.com<br \/>\n    accessKeyId: LTAI4GCH1vX6DKqJWxd6nEuW<br \/>\n    accessKeySecret: yBshYweHOpqDuhCArrVHwIiBKpyqSL<br \/>\n    bucketName: web-397 <\/p>\n<h5>3.\u00a0&#064;ConfigurationProperties<\/h5>\n<h5>(1) \u5b58\u5728\u7684\u95ee\u9898&#xff1a;<\/h5>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"455\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201151801-697f6ea9beb3b.png\" width=\"1152\" \/><\/p>\n<p>\u6211\u4eec\u5728application.yml\u4e2d\u914d\u7f6e\u4e86\u963f\u91cc\u4e91OSS\u7684\u56db\u9879\u53c2\u6570\u4e4b\u540e&#xff0c;\u5982\u679cjava\u7a0b\u5e8f\u4e2d\u9700\u8981\u8fd9\u56db\u9879\u53c2\u6570\u6570\u636e&#xff0c;\u6211\u4eec\u76f4\u63a5\u901a\u8fc7&#064;Value\u6ce8\u89e3\u6765\u8fdb\u884c\u6ce8\u5165\u3002\u8fd9\u79cd\u65b9\u5f0f\u672c\u8eab\u6ca1\u6709\u4ec0\u4e48\u95ee\u9898&#xff0c;\u4f46\u662f\u5982\u679c\u8bf4\u9700\u8981\u6ce8\u5165\u7684\u5c5e\u6027\u8f83\u591a(\u4f8b&#xff1a;\u9700\u898120\u591a\u4e2a\u53c2\u6570\u6570\u636e)&#xff0c;\u6211\u4eec\u5199\u8d77\u6765\u5c31\u4f1a\u6bd4\u8f83\u7e41\u7410\u3002<\/p>\n<p>\u90a3\u4e48\u6709\u6ca1\u6709\u4e00\u79cd\u65b9\u5f0f\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e9b\u914d\u7f6e\u53c2\u6570\u7684\u6ce8\u5165\u5462&#xff1f;\u7b54\u6848\u662f\u80af\u5b9a\u6709&#xff0c;\u5728Spring\u4e2d\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5316\u65b9\u5f0f&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u5c06\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u9879\u7684\u503c\u81ea\u52a8\u7684\u6ce8\u5165\u5230\u5bf9\u8c61\u7684\u5c5e\u6027\u4e2d\u3002<\/p>\n<h5>(2) \u89e3\u51b3\u65b9\u6848&#xff1a;<\/h5>\n<p>\u2460\u00a0\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5b9e\u73b0\u7c7b&#xff0c;\u4e14\u5b9e\u4f53\u7c7b\u4e2d\u7684\u5c5e\u6027\u540d\u548c\u914d\u7f6e\u6587\u4ef6\u5f53\u4e2dkey\u7684\u540d\u5b57\u5fc5\u987b\u8981\u4e00\u81f4<\/p>\n<p>\u2461\u00a0\u9700\u8981\u5c06\u5b9e\u4f53\u7c7b\u4ea4\u7ed9Spring\u7684IOC\u5bb9\u5668\u7ba1\u7406&#xff0c;\u6210\u4e3aIOC\u5bb9\u5668\u5f53\u4e2d\u7684bean\u5bf9\u8c61<\/p>\n<p>\u2462\u00a0\u5728\u5b9e\u4f53\u7c7b\u4e0a\u6dfb\u52a0&#064;ConfigurationProperties\u6ce8\u89e3&#xff0c;\u5e76\u901a\u8fc7perfix\u5c5e\u6027\u6765\u6307\u5b9a\u914d\u7f6e\u53c2\u6570\u9879\u7684\u524d\u7f00<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"338\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201151802-697f6eaa0bd2e.png\" width=\"1485\" \/><\/p>\n<h5>(3) \u4ee3\u7801\u5b9e\u73b0<\/h5>\n<p>\u2460\u00a0\u5b9e\u4f53\u7c7b&#xff1a;AliOSSProperties<\/p>\n<p>import lombok.Data;<br \/>\nimport org.springframework.boot.context.properties.ConfigurationProperties;<br \/>\nimport org.springframework.stereotype.Component;<\/p>\n<p>\/*\u963f\u91cc\u4e91OSS\u76f8\u5173\u914d\u7f6e*\/<br \/>\n&#064;Data<br \/>\n&#064;Component<br \/>\n&#064;ConfigurationProperties(prefix &#061; &#034;aliyun.oss&#034;)<br \/>\npublic class AliOSSProperties {<br \/>\n    \/\/\u533a\u57df<br \/>\n    private String endpoint;<br \/>\n    \/\/\u8eab\u4efdID<br \/>\n    private String accessKeyId ;<br \/>\n    \/\/\u8eab\u4efd\u5bc6\u94a5<br \/>\n    private String accessKeySecret ;<br \/>\n    \/\/\u5b58\u50a8\u7a7a\u95f4<br \/>\n    private String bucketName;<br \/>\n} <\/p>\n<p>\u2461\u00a0AliOSSUtils\u5de5\u5177\u7c7b&#xff1a; <\/p>\n<p>import com.aliyun.oss.OSS;<br \/>\nimport com.aliyun.oss.OSSClientBuilder;<br \/>\nimport org.springframework.beans.factory.annotation.Autowired;<br \/>\nimport org.springframework.stereotype.Component;<br \/>\nimport org.springframework.web.multipart.MultipartFile;<br \/>\nimport java.io.IOException;<br \/>\nimport java.io.InputStream;<br \/>\nimport java.util.UUID;<\/p>\n<p>&#064;Component \/\/\u5f53\u524d\u7c7b\u5bf9\u8c61\u7531Spring\u521b\u5efa\u548c\u7ba1\u7406<br \/>\npublic class AliOSSUtils {<\/p>\n<p>    \/\/\u6ce8\u5165\u914d\u7f6e\u53c2\u6570\u5b9e\u4f53\u7c7b\u5bf9\u8c61<br \/>\n    &#064;Autowired<br \/>\n    private AliOSSProperties aliOSSProperties;<\/p>\n<p>    \/**<br \/>\n     * \u5b9e\u73b0\u4e0a\u4f20\u56fe\u7247\u5230OSS<br \/>\n     *\/<br \/>\n    public String upload(MultipartFile multipartFile) throws IOException {<br \/>\n        \/\/ \u83b7\u53d6\u4e0a\u4f20\u7684\u6587\u4ef6\u7684\u8f93\u5165\u6d41<br \/>\n        InputStream inputStream &#061; multipartFile.getInputStream();<\/p>\n<p>        \/\/ \u907f\u514d\u6587\u4ef6\u8986\u76d6<br \/>\n        String originalFilename &#061; multipartFile.getOriginalFilename();<br \/>\n        String fileName &#061; UUID.randomUUID().toString() &#043; originalFilename.substring(originalFilename.lastIndexOf(&#034;.&#034;));<\/p>\n<p>        \/\/\u4e0a\u4f20\u6587\u4ef6\u5230 OSS<br \/>\n        OSS ossClient &#061; new OSSClientBuilder().build(aliOSSProperties.getEndpoint(),<br \/>\n                aliOSSProperties.getAccessKeyId(), aliOSSProperties.getAccessKeySecret());<br \/>\n        ossClient.putObject(aliOSSProperties.getBucketName(), fileName, inputStream);<\/p>\n<p>        \/\/\u6587\u4ef6\u8bbf\u95ee\u8def\u5f84<br \/>\n        String url &#061;aliOSSProperties.getEndpoint().split(&#034;\/\/&#034;)[0] &#043; &#034;\/\/&#034; &#043; aliOSSProperties.getBucketName() &#043; &#034;.&#034; &#043; aliOSSProperties.getEndpoint().split(&#034;\/\/&#034;)[1] &#043; &#034;\/&#034; &#043; fileName;<\/p>\n<p>        \/\/ \u5173\u95edossClient<br \/>\n        ossClient.shutdown();<br \/>\n        return url;\/\/ \u628a\u4e0a\u4f20\u5230oss\u7684\u8def\u5f84\u8fd4\u56de<br \/>\n    }<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u65b0\u589e\u5458\u5de5<br \/>\n1. \u4ee3\u7801\u5b9e\u73b0<br \/>\n(1)\u00a0EmpController<br \/>\nSlf4j<br \/>\nRestController<br \/>\nRequestMapping(\\&#8221;\/emps\\&#8221;)<br \/>\npublic class EmpController {Autowiredprivate EmpService empService;\/\/\u65b0\u589ePostMappingpublic Result save(RequestBody Emp emp){\/\/\u8bb0\u5f55\u65e5\u5fd7log.info(\\&#8221;\u65b0\u589e\u5458\u5de5, emp:{}\\&#8221;,emp);\/\/<\/p>\n","protected":false},"author":2,"featured_media":70453,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1056,371,2754,305],"topic":[],"class_list":["post-70460","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-javaweb","tag-371","tag-2754","tag-305"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaWeb\u5b66\u4e60\u7b14\u8bb0\uff08Day11\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/70460.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaWeb\u5b66\u4e60\u7b14\u8bb0\uff08Day11\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u65b0\u589e\u5458\u5de5 1. \u4ee3\u7801\u5b9e\u73b0 (1)\u00a0EmpController Slf4j RestController RequestMapping(&quot;\/emps&quot;) public class EmpController {Autowiredprivate EmpService empService;\/\/\u65b0\u589ePostMappingpublic Result save(RequestBody Emp emp){\/\/\u8bb0\u5f55\u65e5\u5fd7log.info(&quot;\u65b0\u589e\u5458\u5de5, emp:{}&quot;,emp);\/\/\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/70460.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-01T15:18:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201151800-697f6ea86fd77.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/70460.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/70460.html\",\"name\":\"JavaWeb\u5b66\u4e60\u7b14\u8bb0\uff08Day11\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-01T15:18:03+00:00\",\"dateModified\":\"2026-02-01T15:18:03+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/70460.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/70460.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/70460.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaWeb\u5b66\u4e60\u7b14\u8bb0\uff08Day11\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaWeb\u5b66\u4e60\u7b14\u8bb0\uff08Day11\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/70460.html","og_locale":"zh_CN","og_type":"article","og_title":"JavaWeb\u5b66\u4e60\u7b14\u8bb0\uff08Day11\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u65b0\u589e\u5458\u5de5 1. \u4ee3\u7801\u5b9e\u73b0 (1)\u00a0EmpController Slf4j RestController RequestMapping(\"\/emps\") public class EmpController {Autowiredprivate EmpService empService;\/\/\u65b0\u589ePostMappingpublic Result save(RequestBody Emp emp){\/\/\u8bb0\u5f55\u65e5\u5fd7log.info(\"\u65b0\u589e\u5458\u5de5, emp:{}\",emp);\/\/","og_url":"https:\/\/www.wsisp.com\/helps\/70460.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-01T15:18:03+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260201151800-697f6ea86fd77.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/70460.html","url":"https:\/\/www.wsisp.com\/helps\/70460.html","name":"JavaWeb\u5b66\u4e60\u7b14\u8bb0\uff08Day11\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-01T15:18:03+00:00","dateModified":"2026-02-01T15:18:03+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/70460.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/70460.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/70460.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"JavaWeb\u5b66\u4e60\u7b14\u8bb0\uff08Day11\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/70460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=70460"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/70460\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/70453"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=70460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=70460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=70460"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=70460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}