springboot上传图片

  • 新建一个springboot项目;

  • 在java/main/com/ljx 创建一个controller.fileController类

    内容如下:

    package com.ljx.controller;

    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 javax.servlet.http.HttpServletRequest;
    import javax.websocket.server.PathParam;
    import java.io.File;
    import java.io.IOException;
    import java.util.UUID;

    /**
    * @author 李捷禧
    * Date: 2022/12/13
    * ClassName: fileController
    */

    @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 = "E://work//image//";
    // 新文件名
    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 = "/work/image/" + fileName;
    model.addAttribute("filename", filename);
    return "/file";
    }
    }

  • 在com/ljx/config一个配置类

    内容如下:

    package com.ljx.config;

    /**
    * @author 李捷禧
    * Date: 2022/12/13
    * ClassName: MyWebAppConfigurer
    */

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

    /**
    * 资源映射路径
    */
    @Configuration
    public class MyWebAppConfigurer implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/work/image/**").addResourceLocations("file:E:/work/image/");
    }
    }
  • 在application.properties文件加一下配置:

    spring.mvc.view.prefix=/WEB-INF/
    spring.mvc.view.suffix=.jsp
  • 在webapp/WEB-INF创建一个file.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可运行环境,见以往博客园

运行结果是:

 

springboot上传图片的更多相关文章

  1. https://segmentfault.com/a/1190000012844836---------关于SpringBoot上传图片的几种方式

    关于SpringBoot上传图片的几种方式 https://segmentfault.com/a/1190000012844836

  2. Springboot上传图片并访问

    Springboot上传图片并访问 步骤 配置绝对路径,并将这个绝对路径添加到springboot静态资源目录中. 文件上传使用绝对路径保存.返回web相对路径,前端加上域名和项目路径,生成完整的路径 ...

  3. springboot上传图片大小限制

    背景:springboot项目上传图片超过1M报错,经了解,springboot默认上传文件1M 需求:更改默认配置,控制上传文件大小 方法:①更改配置文件(经试验不可行,不知道为什么):②更改启动B ...

  4. [原创]SpringBoot上传图片踩的坑

    最近项目里面有个需求,要上传图片到阿里云的OSS服务.所以需要写个上传图片的接口给前端. 这个简单的接口本来就给分配了1个工时,感觉也蛮简单的.但编码过程中遇到了好几个问题,现在一一记录下来,避免再次 ...

  5. springboot 上传图片与回显

    在网上找了很多例子,不能完全契合自己的需求,自行整理了下.需求是这样的:项目小,所以不需要单独的图片服务器,图片保存在服务器中任意的地方,并且可以通过访问服务器来获取图片.话不多说上代码: 1.依赖 ...

  6. springboot 上传图片

    1. 创建多层目录 创建多层目录要使用File的mkdirs()方法,其他可以使用mkdir()方法. 2. 文件大小限制 配置文件中,在spring1.4以后要使用 ` spring.http.mu ...

  7. springboot 上传图片,地址,在页面展示图片

    package com.cxwlw.zhcs.controller.api;import org.springframework.stereotype.Controller;import org.sp ...

  8. 如何在SpringBoot当中上传多个图片或者上传单个图片 工具类

    如何在SpringBoot当中上传多个图片[上传多个图片 ] 附赠工具类 1.SpringBoot 上传图片工具类 public class SpringUploadUtil { /*** * 上传图 ...

  9. ckeditor4.7配置图片上传

    ckeditor作为老牌的优秀在线编辑器,一直受到开发者的青睐. 这里我们讲解下 ckeditor最新版本4.7的图片上传配置. https://ckeditor.com/ 官方 进入下载 https ...

  10. Spring Boot进阶系列二

    上一篇文章,主要分析了怎么建立一个Restful web service,系列二主要创建一个H5静态页面使用ajax请求数据,功能主要有添加一本书,请求所有书并且按照Id降序排列,以及查看,删除一本书 ...

随机推荐

  1. 如何找到CSDN中关注的用户和粉丝?

    如何找到CSDN中关注的用户和粉丝? 刚刚在CSDN个人账号里找了半天都没找到自己关注的人 对CSDN的页面更新感到很迷, 个人账号管理很不人性化, 或者说是根本找不到自己关注的用户以及关注自己的粉丝 ...

  2. Quartz.Net源码Example之Quartz.Examples.AspNetCore

    Quartz.Examples.AspNetCore ​ .NetCore的Web系统,后台主要执行多个触发器任务,前台展示所有触发器信息和正在执行的作业的相关信息,还可以通过访问health-UI来 ...

  3. mysql14 sql优化-索引失效

    1.索引失效 先创建符合索引,三个字段 ALTER table abilityassessrecord add INDEX idx_customerno_roomno_abilityassessrec ...

  4. 视觉十四讲:第七讲_3D-3D:ICP估计姿态

    1.ICP 假设有一组配对好的3D点, \(P={P_{1}, ..., P_{N}}\) , \(P^{'}={P_{1}^{'}, ..., P_{N}^{'}}\). 有一个欧式变换R,t,使得 ...

  5. 四:SSM框架整合

    四:SSM框架整合 Spring+Spring Mvc+Mybatis整合: 代码实现: 1.创建maven工程 pom.xml   <dependencies><!--  spri ...

  6. Rpc-实现Client对ZooKeeper的服务监听

    1.前言 在上一篇文章中,完成了ZooKeeper注册中心.但是在上一篇中,ZooKeeper添加了一个简单的本地缓存,存在一些问题: 当本地缓存OK,ZooKeeper对应服务有新的实例时,本地缓存 ...

  7. JZOJ 2022.02.11【提高A组】模拟

    \(\text{Solution}\) 首先把 \(T2\) 给切了,\(T1\) 找半天规律找不到 然后打了个表算是暴力了 \(T3\) 也暴... 太暴了... \(T4\) 直接啥也不会 \(\ ...

  8. CSS 页面整体变灰色

    body {-webkit-filter: grayscale(100%) !important;-moz-filter: grayscale(100%) !important;-ms-filter: ...

  9. C# 时间各种格式

    1.1 取当前年月日时分秒 currentTime=System.DateTime.Now; 1.2 取当前年 int 年=currentTime.Year; 1.3 取当前月 int 月=curre ...

  10. OpenLayers点聚合

    1. 引言 当页面加载的数据量过大时,拖拽.缩放时往往会产生卡顿 然而,页面实现的内容是有限的,人眼可见范围也是有限的,过于微小的部分是可以不予显示的 聚合是解决这种问题的一个办法,当数据比较多,单个 ...