【SpringBoot学习】5、SpringBoot 实现文件上传,图片上传并显示功能[通俗易懂]

【SpringBoot学习】5、SpringBoot 实现文件上传,图片上传并显示功能[通俗易懂]我先看一下《颈椎病康复指南》再给大家说怎么实现的这两个功能,毕竟只是一个新手,解决这种复杂点的问题(相对而言),还是需要花费大量时间的,这篇文章花了两天的时间才实现的功能,现在就记录一下使用springboot怎么实现文件上传下载的。我这里使用的是springboot2.0.3,不需要导入相关jar包,2.x的版本已经整合进去了,直接使用即可。spring官网提供了springbo…

大家好,又见面了,我是你们的朋友全栈君。

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

我先看一下《颈椎病康复指南》再给大家说怎么实现的这两个功能,毕竟只是一个新手,解决这种复杂点的问题(相对而言),还是需要花费大量时间的,这篇文章花了两天的时间才实现的功能,现在就记录一下使用 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

这里写图片描述

推荐阅读:Spring Cloud Alibaba

微信公众号

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130082.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SynchronousQueue同步队列

    SynchronousQueue同步队列SynchronousQueue简介Java6的并发编程包中的SynchronousQueue是一个没有数据缓冲的BlockingQueue,生产者线程对其的插入操作put必须等待消费者的移除操作take,反过来也一样。不像ArrayBlockingQueue或LinkedListBlockingQueue,SynchronousQueue内部并没有数据缓存空间,你不能调用peek()方…

    2022年6月22日
    21
  • PostgreSQL数据库psql连接报错connections on Unix domain socket “/pgsql/data/.s.PGSQL.5432“?

    PostgreSQL数据库psql连接报错connections on Unix domain socket “/pgsql/data/.s.PGSQL.5432“?问题现象[postgres@lyp~]$pg_ctl-D/pgsql/data/startwaitingforservertostart….2021-10-1501:42:31.606CST[32453]LOG:redirectinglogoutputtologgingcollectorprocess2021-10-1501:42:31.606CST[32453]HINT:Futurelogoutputwillappearin…

    2022年6月19日
    21
  • linux中getchar函数用法,linux getchar函数使用

    linux中getchar函数用法,linux getchar函数使用1函数介绍1)函数原型intgetchar(void);2)函数功能从stdin中读取一个字符。3)返回值返回读取字符的ASCII值或者EOF字符或者出错值。4)头文件#include2函数使用2.1getchar函数的特点Linux下编写的一个例子:#includeintmain(void){charch;intnum;num=0;printf…

    2022年10月18日
    0
  • TinyXML用法小结[通俗易懂]

    TinyXML用法小结[通俗易懂]TinyXML用法小结1.     介绍Tinyxml的官方网址:http://www.grinninglizard.com官方介绍文档:http://www.grinninglizard.com/tinyxmldocs/tutorial0.html在TinyXML中,根据XML的各种元素来定义了一些类:TiXmlBase:整个TinyXML模型的基类。TiXmlAttr…

    2022年5月7日
    87
  • java switch是什么意思_java switch语句详解[通俗易懂]

    java switch是什么意思_java switch语句详解[通俗易懂]switch语句的格式:switch(整型或字符型变量){case变量可能值1:分支一;break;case变量可能值2:分支二;break;case变量可能值3:分支三;break;…default:最后分支;}在switch的语法里,我们要学到4个关键字:switch、case、break、default。在 switch(变量) 这一行里,变量只能是整型…

    2022年7月7日
    17
  • mybatis中的动态sql表现为_MybatisPlus

    mybatis中的动态sql表现为_MybatisPlus如何分页查询Mybatis如何分页查询?Mysql中可以使用limit语句,但limit并不是标准SQL中的,如果是其它的数据库,则需要使用其它语句。MyBatis提供了RowBounds类,用于实现分页查询。RowBounds中有两个数字,offset和limit。MyBatis如何利用RowBounds

    2022年9月22日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号