SpringBoot----2分钟搞定SpringBoot项目中图片以及其他文件的上传与下载

文章目录

  • 前言
  • 一、创建SpringBoot项目
  • 二、新建上传下载的html文件
    • 1.内容
    • 2.前端样式
  • 三、新建FileController.java
    • 1.相关代码
    • 2.重新启动项目
  • 总结


前言

最近收到几个粉丝小伙伴的私信,说能不能写一篇SpringBoot项目的图片文件的上传与下载案例,so 作为一个十分爱粉的小小( 菜鸡)程序员,也为了感谢还在默默关注我的朋友们,我一气呵成,自称–>史上最简SpringBoot项目的图片文件的上传与下载。

一、创建SpringBoot项目

先创建一个spring-boot项目,创建成功以后的目录如下:
SpringBoot----2分钟搞定SpringBoot项目中图片以及其他文件的上传与下载_第1张图片

注意:创建成功后,只需要按照上方结果目录创建一个controller文件夹,在controller包中新建一个FileController类,在static文件夹中新建一个html文件即可,pom.xml与application.properties主配置文件中,什么都不需要添加和配置,够简单直接吧

二、新建上传下载的html文件

1.内容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传+下载</title>
</head>
<body>
<!--post请求,post提交方式对应FlieController类中的@PostMapping方法-->
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file"/>
    <button type="submit">上传</button>
</form>
<br/>
<!--学过web的朋友应该都知道,超链接默认是get请求,get提交方式对应FlieController类中的@GetMapping方法-->
<a href="/upload">
    <button>下载</button>
</a>

</body>
</html>

2.前端样式

启动项目,浏览器地址栏输入:http://localhost:8080/up_download.html
SpringBoot----2分钟搞定SpringBoot项目中图片以及其他文件的上传与下载_第2张图片

三、新建FileController.java

1.相关代码

package com.sogu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @Description:文件上传下载,采用restful风格,会根据请求方式,自动执行对应的方法
 * @Author nanyi
 * @Date 2020/12/30 11:05
 **/
@Controller
@RequestMapping("/upload")
public class FileController {
     
    /**
     * @Description:上传文件
     */
    @PostMapping
    @ResponseBody
    public String upload(MultipartFile file) {
     
//        获取原始文件名
        String filename = file.getOriginalFilename();
//        获取文件后缀名
        String suffixName = filename.substring(filename.lastIndexOf("."));
//        文件保存路径
        String filePath = "D:/upload/";
//        添加UUID编码,防止文件名重复
        filename = filePath + UUID.randomUUID() + filename;
//        文件对象
        File file1 = new File(filename);
//        判断路径是否存在,如果不存在则创建
        if (!file1.getParentFile().exists()) {
     
            file1.getParentFile().mkdirs();
        }
        try {
     
//            保存到服务器中
            file.transferTo(file1);
            return "上传成功";
        } catch (IOException e) {
     
            e.printStackTrace();
        }
        return "上传失败";
    }

    /**
     * @Description:下载文件
     */
    @GetMapping
    public void download(HttpServletResponse response) throws Exception {
     
//        文件地址,真实环境是存放在数据库中的
        File file = new File("D:\\upload\\南一小宝宝啊.txt");
//        传入输出对象
        FileInputStream fileInputStream = new FileInputStream(file);
//        设置相关格式
        response.setContentType("application/force-download");
//        设置下载后的文件名以及header
        response.addHeader("Content-disposition", "attachment;fileName=" + "南一小宝宝啊.txt");
//       创建输出对象
        ServletOutputStream outputStream = response.getOutputStream();
//        常规操作
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = fileInputStream.read(bytes)) != -1) {
     
            outputStream.write(bytes, 0, len);
        }
        fileInputStream.close();
    }
}

2.重新启动项目

重新启动项目,浏览器地址栏输入:http://localhost:8080/up_download.html
(1)执行上传文件功能
SpringBoot----2分钟搞定SpringBoot项目中图片以及其他文件的上传与下载_第3张图片

  点击上传成功后返回:上传成功
SpringBoot----2分钟搞定SpringBoot项目中图片以及其他文件的上传与下载_第4张图片
  去文件夹中查看,刚刚上传的文件已经存在
SpringBoot----2分钟搞定SpringBoot项目中图片以及其他文件的上传与下载_第5张图片
(2)执行下载文件功能
  点击下载按钮下载文件
SpringBoot----2分钟搞定SpringBoot项目中图片以及其他文件的上传与下载_第6张图片

总结

日常一句:我是菜鸡,我是菜鸡,赶紧滚去学习。。。

你可能感兴趣的:(SpringBoot,spring,boot,java)