若依分离版上传文图片 在不分离的Springboot的项目中回显 模板引擎Beetl

yaml

# 项目相关配置
beetl-web:
  # 名称
  name: Beetl
  # 版本
  version: 3.8.2
  # 版权年份
  copyrightYear: 2023
  # 实例演示开关
  demoEnabled: true
  # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
  profile: D:/beetl/uploadPath
  # 获取ip地址开关
  addressEnabled: false

配置类

package com.beetl.common.config;

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


/**
 * @author:myb
 * @description: 资源配置
 * @date:2023/11/9 17:41
 */

@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
    @Value("${beetl-web.profile}")
    private String filePath;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        /** 通过url访问项目外的目录图片*/
        registry.addResourceHandler("/cert/**").addResourceLocations("file:/"+ filePath +"/");
    }

}

文件路径Utils

package com.beetl.common.utils.file;

/**
 * @author:myb
 * @description: 文件路径Utils
 * @date:2023/11/10 9:13
 */
public class FilePathUtils
{
    public static String ImageFilePath(String filePath){

        int profilePathIndex = filePath.indexOf("/profile/");

        String result = "";
        if (profilePathIndex != -1)
        {
             result = filePath.substring(profilePathIndex + "/profile/".length());
            if (!result.startsWith("/"))
            {
                result = "/cert/"+ result;
            }
        }

        return result;
    }
}

html

<div class="row w-100 mt-3">
     <div class="col ml-5 d-flex align-items-center pr-0">
           <div>
                <label style="font-size: .9rem;width: 8rem;text-align: right;"class="pl-1 my-2 mx-3">身份证人像页</label>
                <input type="file" id="person-card-front"  style="opacity: 0;width: .1rem"  name="cardFrontFile">
                 <div class="img d-inline" >
                      <img src="${nvl(personBaseInfo.userCertFront!,'/images/idcard_front.png')}" id="person-card-front-img" style="width: 16rem; height: 10rem;"alt="身份证头像页">
                </div>
           </div>

      </div>
</div>      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot,实现头像上的步骤如下: 1. 添加件上依赖 在`pom.xml`添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> ``` 2. 编写上页面 在HTML页面添加上表单: ```html <form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="Upload"/> </form> ``` 3. 编写上接口 在控制器添加上接口: ```java @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file, Model model) { String fileName = StringUtils.cleanPath(file.getOriginalFilename()); try { FileUploadUtil.saveFile(uploadDir, fileName, file); } catch (IOException e) { e.printStackTrace(); } model.addAttribute("message", "File uploaded successfully!"); model.addAttribute("fileName", fileName); return "upload-result"; } ``` 其,`FileUploadUtil`是一个工具类,用于保存件: ```java public class FileUploadUtil { public static void saveFile(String uploadDir, String fileName, MultipartFile multipartFile) throws IOException { Path uploadPath = Paths.get(uploadDir); if (!Files.exists(uploadPath)) { Files.createDirectories(uploadPath); } try (InputStream inputStream = multipartFile.getInputStream()) { Path filePath = uploadPath.resolve(fileName); Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException ioe) { throw new IOException("Could not save image file: " + fileName, ioe); } } } ``` 4. 示上结果 在上成功后,跳转到上结果页面,示上件名和头像: ```html <h1>Upload Result</h1> <p th:text="'File uploaded successfully: ' + ${fileName}"></p> <img th:src="@{${'/uploads/' + fileName}}" width="200" height="200"/> ``` 其,`/uploads/`是上件保存的目录。需要在`application.properties`添加以下配置: ```properties spring.servlet.multipart.enabled=true spring.servlet.multipart.file-size-threshold=2KB spring.servlet.multipart.max-file-size=200MB spring.servlet.multipart.max-request-size=215MB # Upload directory for files file.upload-dir=uploads ``` 在控制器添加注入`uploadDir`的代码: ```java @Value("${file.upload-dir}") private String uploadDir; ``` 5. 运行应用程序 现在,启动Spring Boot应用程序并访问上页面。选择要上件并单击“上”按钮。上成功后,将示上结果页面,并示上件名和头像。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值