SpringBoot如何实现文件上传呢?

戚薇 SpringBoot 发布时间:2023-06-15 22:03:27 阅读数:14008 1
下文笔者讲述SpringBoot实现文件上传的方法及示例分享,如下所示

SpringBoot实现文件上传的实现思路

1.引入相关依赖
2.controller接收传入的文件对象
例:SpirigBoot项目接收文件

引入依赖

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>commons-fileupload</groupId>
		<artifactId>commons-fileupload</artifactId>
		<version>1.4</version>
	</dependency>
</dependencies>

SpringBoot主类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class UploadTestApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(UploadTestApplication.class, args);
    }
 
}

实体类

import lombok.AllArgsConstructor;
import lombok.Data;
 
@Data
@AllArgsConstructor
public class Resp<E> {
    private String code;
    private String message;
    private E 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);
    }
}

上传接口

import com.java265.uploadtest.entity.Resp; import org.springframework.web.multipart.MultipartFile; public interface UploadService { public Resp<String> upload(MultipartFile file); }

接口实现类

import com.java265.uploadtest.entity.Resp;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
 
@Service
public class UploadServiceImpl implements UploadService{
    @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 filePath="D:\\uploadtest\\";
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) dest.getParentFile().mkdirs();
        try {
            file.transferTo(dest);
        } catch (Exception e) {
            e.printStackTrace();
            Resp.fail("500",originalFilename+"上传失败!");
        }
        return Resp.success(fileName);
    }
}

Controllerc控制层

import com.java265.uploadtest.entity.Resp;
import com.java265.uploadtest.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
@RestController
public class UpLoadController {
    @Autowired
    private UploadService uploadService;

    @RequestMapping(value = "/upload.do",method = RequestMethod.POST)
    public Resp<String> upload(@RequestParam("file") MultipartFile file){
        return uploadService.upload(file);
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringBoot/202306/6800.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者