前言:
之前一直都是用的別人封裝好的文件上傳方法,這次想自己寫(xiě)一個(gè)特別簡(jiǎn)單的,文件上傳方法,非常適合新手觀(guān)看…
正文:
首先需要Springboot需要有Web依賴(lài),就是下面這個(gè)依賴(lài)
<!--Web依賴(lài)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
依賴(lài)導(dǎo)完了,下面就直接是代碼,大家看一下
package com.xssq.controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.*; /** * 文件上傳Controller * 前端控制層 */ @RestController @RequestMapping("/upload") public class UploadController { @PostMapping("/uploadFile") public void upload(MultipartFile file) throws IOException { /*創(chuàng)建一個(gè)文件對(duì)象*/ File file1 = new File("C:\\xssq\\", file.getOriginalFilename()); /* 設(shè)置創(chuàng)建文件的時(shí)候,會(huì)生成不存在的目錄*/ file1.mkdirs(); /*保存文件*/ file.transferTo(file1); } }
后記:
到這里文件上傳的解釋都在代碼里面,下面如果報(bào)文件過(guò)大的報(bào)錯(cuò)還需要配置一點(diǎn)上傳文件的大小,在下面的application.yml文件配置中
server: # 端口配置 port: 8989 spring: servlet: multipart: enabled: true # 文件上傳大小限制 max-file-size: 50MB # 請(qǐng)求大小限制 max-request-size: 50MB
到此文件上傳 就結(jié)束了
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-647273.html
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-647273.html
到了這里,關(guān)于SpringBoot如何使用MultipartFile進(jìn)行文件上傳保存到服務(wù)器本地的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!