前言:
之前一直都是用的别人封装好的文件上传方法,这次想自己写一个特别简单的,文件上传方法,非常适合新手观看…
正文:
首先需要Springboot需要有Web依赖,就是下面这个依赖
<dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-webartifactId>dependency>
依赖导完了,下面就直接是代码,大家看一下
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.*;
@RestController
@RequestMapping("/upload")
public class UploadController {@PostMapping("/uploadFile")public void upload(MultipartFile file) throws IOException {File file1 = new File("C:\\xssq\\", file.getOriginalFilename());file1.mkdirs();file.transferTo(file1);}
}
后记:
到这里文件上传的解释都在代码里面,下面如果报文件过大的报错还需要配置一点上传文件的大小,在下面的application.yml文件配置中
server:
port: 8989spring:servlet:multipart:enabled: truemax-file-size: 50MBmax-request-size: 50MB
到此文件上传 就结束了
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!