Springboot实现文件上传下载的简单demo(可参考)

本文章详细介绍使用Springboot如何实现上传文件和下载文件接口

1. 新建一个Springboot项目

File -> New Project 根据下图指引做:

2. 点击next,选择Springboot版本和组件,版本可以跟我不同。

3. 点击 Finish完成项目创建,然后开始导入Pom依赖:

如果依赖下载的太慢的话,可以使用aliyun,方法是在你的 pom.xml里加入下面的代码:

 <repositories>
        <repository>
            <id>nexus-aliyun</id>
            <name>nexus-aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>aliyun nexus</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>


加完之后再刷新一下:

4. 导入依赖之后会出现Springboot的启动按钮:


直接点击绿色的三角启动运行项目。
运行效果:

看到这个 8080 表示你的项目启动好了。

5. 开始写文件上传接口

package com.example.demo;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

// 定义接口路径
@RestController
@RequestMapping("/file")
public class FileController {

    // 找一个计算机的磁盘位置,比如 D:\\, 我这里使用的是本项目的路径
    private static final String BASE_DIR = "D:\\代码\\demos\\demo\\files\\";

	// 定义接口类型和二级路径,完整的接口url是:/file/upload
    @PostMapping("/upload")
    public void upload(@RequestParam MultipartFile file) {
        // 获取文件的名称
        String fileName = file.getOriginalFilename();
        try {
            // 新建一个文件路径
            File uploadFile = new File(BASE_DIR + fileName);
            // 当父级目录不存在时,自动创建
            if (!uploadFile.getParentFile().exists()) {
                uploadFile.getParentFile().mkdirs();
            }
            // 存储文件到电脑磁盘
            file.transferTo(uploadFile);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

6. 测试下文件上传接口,使用postman测试:

重启下项目,然后测试。

点击这里重启。

请注意下面的5个步骤,缺一不可

然后我们选择本地的一张图片做测试,注意key必须是 file,跟你接口的参数名字保持一致!


开始测试:

7. 文件下载接口

// 注意导包
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

// 下载接口,url: /file/download?fileName=xxx

@GetMapping("/download")
public void download(@RequestParam String fileName, HttpServletResponse response) {
	//  新建文件流,从磁盘读取文件流
    try (FileInputStream fis = new FileInputStream(BASE_DIR + fileName);
         BufferedInputStream bis = new BufferedInputStream(fis);
         OutputStream os = response.getOutputStream()) {    //  OutputStream 是文件写出流,讲文件下载到浏览器客户端
       // 新建字节数组,长度是文件的大小,比如文件 6kb, bis.available() = 1024 * 6
        byte[] bytes = new byte[bis.available()];
        // 从文件流读取字节到字节数组中
        bis.read(bytes);	
        // 重置 response
        response.reset();
        // 设置 response 的下载响应头
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));  // 注意,这里要设置文件名的编码,否则中文的文件名下载后不显示
        // 写出字节数组到输出流
        os.write(bytes);
        // 刷新输出流
        os.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

8. 测试下载接口

重启下项目,然后测试。

点击这里重启。

可以使用刚才上传的文件进行测试,路径为 http://localhost:8080/file/download?fileName=dog.png
在这里插入图片描述
回车后即可完成下载!

9. 文件上传接口返回下载地址

我们可以把刚才的文件上传接口进行稍微的改造:

@PostMapping("/upload")
public String upload(@RequestParam MultipartFile file) {
     String fileName = file.getOriginalFilename();
     try {
         File uploadFile = new File(BASE_DIR + fileName);
         if (!uploadFile.getParentFile().exists()) {
             uploadFile.getParentFile().mkdirs();
         }
         file.transferTo(uploadFile);

     } catch (IOException e) {
         e.printStackTrace();
     }
     // 自己拼接一下下载的接口url,参数就是上传的文件名称
     return "http://localhost:8080/file/download?fileName=" + fileName;
 }

这样就咋上传完成后直接返回了下载的地址。

再来测试下:

换了一个文件,在文件上传完成后直接返回了文件的下载链接,非常nice。

10. 前端网页上传文件

在static文件夹新建 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件</title>
</head>
<body>
    <div style="padding: 30px">
        <input type="file" id="fileInput">
        <div style="margin: 20px 0">
            <button onclick="upload()">上传文件</button>
        </div>
        <div id="url"></div>
    </div>

<script>

    function upload() {
        // 获取input 选择的的文件
        const fileInput = document.getElementById('fileInput')
        const fd = new FormData()
        fd.append('file', fileInput.files[0])
        fetch('http://localhost:8080/file/upload', {
            method: 'POST',
            body: fd
        }).then(res => res.json()).then(res => {
            // 要求后台返回的数据必须是json
            console.log(res)  // res = { url: 'http://localhost:8080/file/download?fileName=足球.png' }
            document.getElementById("url").innerText = `上传成功,文件url: ${res.url}`
        })
    }
</script>
</body>
</html>

前端使用 fetch 上传文件,请求后台接口,但是要求后台返回的数据是json格式,怎么设置后台返回格式为json呢?

我们可以对上传接口改造,使用 Map返回数据,map中有个属性 url,就是我们返回的数据。

@PostMapping("/upload")
public Map<String, Object> upload(@RequestParam MultipartFile file) {
    // 获取文件的名称
    String fileName = file.getOriginalFilename();
    try {
        // 新建一个文件路径
        File uploadFile = new File(BASE_DIR + fileName);
        // 当父级目录不存在时,自动创建
        if (!uploadFile.getParentFile().exists()) {
            uploadFile.getParentFile().mkdirs();
        }
        // 存储文件到电脑磁盘
        file.transferTo(uploadFile);

    } catch (IOException e) {
        e.printStackTrace();
    }
    Map<String, Object> map = new HashMap<>();
    map.put("url", "http://localhost:8080/file/download?fileName=" + fileName);
    return map;
}

这样,我们就完成了后台 upload接口的改造了。

来看看效果:
在这里插入图片描述

OK,至此,我们完成了后台接口和前端的请求,怎么样?很简单吧?快去试试吧!

我是程序员青戈, Java程序员一枚~

关注我的B站:程序员青戈 ,学习更多干货教程!

  • 31
    点赞
  • 93
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
你可以使用Vue.js和Element UI来实现前端界面,使用Spring Boot来处理后端逻辑来实现文件模板的上传和下载功能。 首先,你可以创建一个Vue组件来处理文件上传下载的界面。可以使用Element UI中的Upload组件来实现文件上传功能,使用Button组件来实现文件下载功能。在上传组件中,你可以设置上传的文件类型和大小限制,并在上传成功后获取到文件的URL或者其他信息。 接下来,在后端使用Spring Boot来处理上传和下载的逻辑。你可以创建一个Controller来处理文件上传下载的请求。在文件上传的方法中,你可以使用MultipartFile来接收上传的文件,并将其保存到服务器上的某个目录中。在文件下载的方法中,你可以根据传入的文件名或者其他标识,从服务器上读取相应的文件,并将其以流的形式返回给前端。 以下是一个简单的示例代码: 前端(Vue.js + Element UI): ```vue <template> <div> <el-upload class="upload-demo" action="/api/upload" :on-success="handleSuccess" :before-upload="beforeUpload" > <el-button type="primary">点击上传</el-button> </el-upload> <el-button type="primary" @click="downloadTemplate">下载模板</el-button> </div> </template> <script> export default { methods: { handleSuccess(response) { // 处理上传成功后的逻辑 console.log(response); }, beforeUpload(file) { // 设置上传文件的类型和大小限制 const fileType = file.type; const fileSize = file.size / 1024 / 1024; // MB const allowedTypes = ['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']; // 允许的文件类型 const maxFileSize = 10; // 允许的最大文件大小,单位:MB if (!allowedTypes.includes(fileType)) { this.$message.error('只能上传pdf、doc或docx格式的文件'); return false; } if (fileSize > maxFileSize) { this.$message.error(`文件大小超过了${maxFileSize}MB`); return false; } return true; }, downloadTemplate() { // 处理下载模板的逻辑 window.location.href = '/api/download'; }, }, }; </script> ``` 后端(Spring Boot): ```java @RestController @RequestMapping("/api") public class FileController { @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { // 处理文件上传逻辑 // 可以将上传的文件保存到服务器上的某个目录中 return "上传成功"; } @GetMapping("/download") public void downloadTemplate(HttpServletResponse response) { // 处理文件下载逻辑 // 根据文件名或者其他标识,从服务器上读取相应的文件,并将其以流的形式返回给前端 String fileName = "template.docx"; // 下载的文件名 String filePath = "/path/to/template.docx"; // 文件在服务器上的路径 try { File file = new File(filePath); InputStream inputStream = new FileInputStream(file); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); IOUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这是一个简单的示例,你可以根据自己的需求进行进一步的调整和优化。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员青戈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值