首页 > 其他分享 >【SpringBoot学习】5、SpringBoot 实现文件上传,图片上传并显示功能

【SpringBoot学习】5、SpringBoot 实现文件上传,图片上传并显示功能

时间:2023-08-03 17:56:17浏览次数:40  
标签:SpringBoot springframework fileName file org import 上传 图片

SpringBoot 实现文件上传,图片上传并显示功能

我这里使用的是 springboot 2.0.3,不需要导入相关 jar 包,2.x 的版本已经整合进去了,直接使用即可。

spring 官网提供了 springboot 的文件上传下载案例,这是网址:https://spring.io/guides/gs/uploading-files/,使用的是流的输出。

下面的案例是 springboot2.x 图片上传与回显。我使用的工具是 idea。

1、创建 idea 默认的 springboot 项目,我的版本是 2.0.3

2、创建一个控制层 FileController

package com.rainy.controller;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

/**
 * 文件上传
 */
@Controller
public class FileController {

    @GetMapping(value = "/file")
    public String file() {
        return "file";
    }

    @PostMapping(value = "/fileUpload")
    public String fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) {
        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = "D://temp-rainy//"; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = "/temp-rainy/" + fileName;
        model.addAttribute("filename", filename);
        return "file";
    }
}

3、创建 MyWebMvcConfigurer,这里是配置资源映射路径,详细点的介绍看这篇文章:https://blog.csdn.net/qq_38762237/article/details/81283241

/**
 * 资源映射路径
 */
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/temp-rainy/**").addResourceLocations("file:D:/temp-rainy/");
    }
}

4、jsp 页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/fileUpload" method="post" enctype="multipart/form-data">
        <label>上传图片</label>
        <input type="file" name="file"/>
        <input type="submit" value="上传"/>
    </form>
    <p>图片:</p>
    <img src="${filename }"/>
</body>
</html>

注意一点:我是使用 jsp 引擎来渲染,因为我不会用 Thymeleaf,添加 jsp 页面,springboot 使用 jsp 页面是需要进行配置 jsp 整合的,默认的是 Thymeleaf 的页面,简单的就是 HTML 页面

springboot 配置 jsp 页面的方法:https://blog.csdn.net/qq_38762237/article/details/81283352

标签:SpringBoot,springframework,fileName,file,org,import,上传,图片
From: https://www.cnblogs.com/mountainstudy/p/17604047.html

相关文章

  • 《Ext详解与实践》节选:文件上传
    rel="File-List"href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">rel="Edit-Time-Data"href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01......
  • 【SpringBoot学习】4、SpringBoot 配置本地资源映射路径已解决
    springboot配置本地资源映射路径需要配置一下映射资源位置,当时springboot1.x和spring波特2.x的配置方法不同,这里就分开记录一下配置过程。1、springboot1.x配置@ConfigurationpublicclassMyWebMvcConfigurerAdapterextendsWebMvcConfigurerAdapter{@Overri......
  • R&S 图片或者文件数据解析
    当我们使用SCPI命令获取图片或者文件内容时,需要对返回的数据进行解析一般来说返回的数据以#6123456XXXXXXXXXXXXXXX。。。。开始现在对数据解析:#表示数据的开始#后的第一个数字为以多少位数字来表示文件的大小(字节长度)从以上的例子看6代表后面的6位数字代表文件数据的大小,所......
  • 【SpringBoot学习】3、SpringBoot 多个版本配置简单的拦截器
    springboot1.x和springboot2.x配置拦截器区别就在于注册拦截器的方式不同,springboot1.x配置方法是:publicclassWebAppConfigextendsWebMvcConfigurerAdapter{springboot2.x配置方法是:publicclassLoginConfigurerimplementsWebMvcConfigurer{下面详细的介绍使......
  • Web编辑器 图片粘贴上传,实现图文粘贴,图片自动上传
    ​ 图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-editor的二进制流保存为例:打开umeditor.js,找到UM.plugins['autoupload'],然后找到autoUploadHandler方法,注释掉其中的代码。加入下面的代码://判断剪......
  • 【SpringBoot学习】2、idea 配置 SpringBoot 热启动详解,和热启动失效解决方案
    一、idea配置springboot热启动方法1、添加spring-boot-devtools的包,true必须加上。<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></d......
  • 【SpringBoot学习】1、SpringBoot 配置 jsp 模板引擎
    springboot整合jsp页面创建springboot项目就不废话了。在原来的基础上直接加东西就可以了1、添加jsp支持的jar包<!--servlet依赖--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><scope>provid......
  • 在线编辑器 图片粘贴上传,实现图文粘贴,图片自动上传
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@     page contentType="text/html;cha......
  • 【亲测】解决使用super_gradients库预测图片无法获取预测结果图片
    问题在使用super_gradients库中的Yolo-nas预测图片时,想要获取预测好的图片,但执行out=model.predict("camera01.png",conf=0.6,batch_size=None)之后只能out.show()和out.save(),无法返回预测结果图片。解决方法importcv2importtorchfromsuper_gradients.trainingimportmo......
  • 富文本编辑器 图片粘贴上传,实现图文粘贴,图片自动上传
    ​ 由于工作需要必须将word文档内容粘贴到编辑器中使用但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直接解决这个问题考虑到自己除了工作其他时间基本上不使用windows,因此打算使用nodejs来解......