idea实现文件上传

App启动类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

controller类

package com.example.demo.Controller;

import com.example.demo.entity.Resp;
import com.example.demo.pojo.Yh;
import com.example.demo.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


@RestController
@CrossOrigin
public class Contoller {
    private List<Yh> list = new ArrayList<>();


    @Autowired
    private ILoginService iLoginService;

    @RequestMapping(value = "/yh",method = RequestMethod.GET)
    public List<Yh>find(){
        list.add(new Yh("1",
                "ERROR 1146 (42S02): Table 'dewu.dewu' doesn't exist"));
        list.add(new Yh("2","https://cdn.poizon.com/du_app/2020/image/32237308_byte820205byte_96b764f74b4212ee9c3058c8288ad7bf_iOS_w2252h3000.jpg?x-oss-process=image/format,webp"));
        list.add(new Yh("3","dfghjk"));
        list.add(new Yh("11","dfghjk"));
        list.add(new Yh("5","dfghjk"));
        list.add(new Yh("8","dfghjk"));

        return list;
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    private Resp<String> upload(@RequestParam("file")MultipartFile file){
        return iLoginService.upload(file);
    }

}

自己定义功能类

package com.example.demo.entity;

public class  Resp<E> {
    private  String code;
    private String message;
    private  E body;

     Resp(String code,String message,E body){
       this.code = code;
       this.message = message;
       this.body = body;
   }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public E getBody() {
        return body;
    }

    public void setBody(E body) {
        this.body = body;
    }
    public static <E> Resp<E> success(E body){
       return  new Resp("200","",body);
    }
    public static <E> Resp<E> fail(String code,String message){
        return  new Resp(code,message,(Object)null);
    }
}

yh接口实现类

package com.example.demo.pojo;

import java.io.Serializable;

public class Yh  implements Serializable {
    private String id;
    private String lianjie;

    public Yh() {
    }

    public Yh(String id, String lianjie) {
        this.id = id;
        this.lianjie = lianjie;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLianjie() {
        return lianjie;
    }

    public void setLianjie(String lianjie) {
        this.lianjie = lianjie;
    }

    @Override
    public String toString() {
        return "Yh{" +
                "id='" + id + '\'' +
                ", lianjie='" + lianjie + '\'' +
                '}';
    }
}

创建文件上传接口

package com.example.demo.service;

import com.example.demo.entity.Resp;
import org.springframework.web.multipart.MultipartFile;

public interface ILoginService {
    Resp<String> upload (MultipartFile file);

}

实现文件上传接口

package com.example.demo.service.impI;

import com.example.demo.entity.Resp;
import com.example.demo.service.ILoginService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
@Service
public class LoginService implements ILoginService {

    @Override
    public Resp<String>upload(MultipartFile file){
        if(file.isEmpty()){
            return Resp.fail("400","文件为空!");
        }
        String OriginalFilename = file.getOriginalFilename();
        String fileName = System.currentTimeMillis()+"."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
        String filPath = "E:\\Ceshi\\";
        File dest = new File(filPath+fileName);
        if(!dest.getParentFile().exists())
            dest.getParentFile().mkdirs();
        try {
            file.transferTo(dest);
        }catch (Exception e){
            e.printStackTrace();
            return  Resp.fail("500","文件上传失败!");
        }
        return  Resp.success(fileName);
    }
}

mvn

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

resources文件夹下 application.properties设置


#// 设置单个文件大小
spring.servlet.multipart.max-file-size= 50MB
#// 设置单次请求文件的总大小
spring.servlet.multipart.max-request-size= 50MB
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容