【spring boot 十七】文件上传
使用SpringBoot进行文件上传的方法和SpringMVC差不多。
话不多说,直接上代码
FileUploadController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.spring.boot.web; import com.spring.boot.dto.BaseResult; import com.spring.boot.file.FileUploadService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; /** * 文件上传到nginx * Created by XJH on 2017/11/8. */ @CrossOrigin @RestController @RequestMapping("/file") public class FileUploadController { private final FileUploadService fileUploadService; @Autowired public FileUploadController(FileUploadService fileUploadService) { this.fileUploadService = fileUploadService; } /** * 上传图片 * * @param file * @return */ @PostMapping(value = "/upload", produces = {"application/json;charset=UTF-8"}) public BaseResult upload(@RequestParam("uploadFile") MultipartFile file) throws IOException { return new BaseResult<>(true, fileUploadService.upload(file)); } } |
FileUploadService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.spring.boot.file; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; /** * Created by XJH on 2017/11/8. */ @Service public interface FileUploadService { /** * 上传图片 * * @param file 图片 * @return 返回图片路径 */ String upload(MultipartFile file) throws IOException; } |
FileUploadServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package com.spring.boot.file; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.servlet.MultipartConfigElement; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; /** * FileUploadServiceImpl * Created by XJH on 2017/11/8. */ @Service public class FileUploadServiceImpl implements FileUploadService { @Value("${img.urlPrefix}") private String urlPrefix; private final MultipartConfigElement multipartConfigElement; @Autowired public FileUploadServiceImpl(MultipartConfigElement multipartConfigElement) { this.multipartConfigElement = multipartConfigElement; } /** * 上传图片 * * @param file 图片 * @return 返回图片url */ @Override public String upload(MultipartFile file) throws IOException { String filePath = multipartConfigElement.getLocation() + file.getOriginalFilename(); if (!file.isEmpty()) { File dir = new File(multipartConfigElement.getLocation()); if (!dir.exists()) { //文件夹不存在,创建文件夹 dir.mkdirs(); } // 还有关于文件格式限制、文件大小限制,详见:中配置。 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(filePath))); out.write(file.getBytes()); out.flush(); out.close(); return urlPrefix + file.getOriginalFilename(); } else { throw new RuntimeException("图片为空"); } } } |
在application.properties配置文件中:
1 2 3 4 5 |
#上传文件 spring.http.multipart.max-file-size=1024KB spring.http.multipart.max-request-size=102400KB spring.http.multipart.location=/usr/local/nginx/html/img/upload/ img.urlPrefix=https://www.27wy.cn/img/upload/ |
看前缀就知道是干什么了
spring.http.multipart.max-file-size:顾名思义是最大文件的大小
spring.http.multipart.max-request-size:总文件的大小
spring.http.multipart.location:文件存储的位置
img.urlPrefix:返回图片url的前缀
是不是很简单?
就是这么简单!
现在的我不配喊累,因为我一无所有。
发表评论