使用若依框架的springboot项目中的上传功能

上传功能

在自己写的借助于“若依”框架的springboot项目中,写上传下载功能,想记录一下,所以有了此篇文章。

1. 配置文件filePath.properties

##文件上传路径

// 定义文件上传路径 D:\\ 此为路径,可根据自己需求修改
file.fileUploadPath=D:\\AA\\bankCardService

2. 实体类

##定义上传FileUploadProp实体类

package com.cn.web.prop;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "file")
@PropertySource(value = "filePath.properties")
public class FileUploadProp {

	private String fileUploadPath;

	public String getFileUploadPath() {
		return fileUploadPath;
	}

	public void setFileUploadPath(String fileUploadPath) {
		this.fileUploadPath = fileUploadPath;
	}

}

3. 前端写法

##文件上传前端界面upload.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><head>
    <th:block th:include="include :: header('【请填写功能名称】列表')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-appendix-add" enctype="multipart/form-data">
            <input type="hidden" name="appendixRelevanceid" id="appendixRelevanceid" class="form-control" th:value="${arId}">
            <div class="form-group">
                <label class="col-sm-3 control-label">文件:</label>
                <div class="col-sm-8">
                    <input name="file" id="file" class="form-control" type="file">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">说明:</label>
                <div class="col-sm-8">
                    <input type="text" name="appendixRemark" id="appendixRemark" class="form-control">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        const prefix = ctx + "web/targetAppendix";
        function submitHandler() {
            uploadFile();
        }

        //上传方法
        function uploadFile(){
            // debugger;
            var formData = new FormData();
            if ($('#file')[0].files[0] == null) {
                $.modal.alertWarning("请先选择文件");
                return false;
            }
            //参数
            formData.append('appendixRelevanceid', $("#appendixRelevanceid").val());
            formData.append('appendixRemark', $("#appendixRemark").val());
            //文件
            formData.append('file', $('#file')[0].files[0]);

            //上传
            $.ajax({
                url: prefix + "/addAppendix",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }
    </script>
</body>
</html>

3. 后端上传写法

xxController.java

// 文件上传保存
@PostMapping("/addAppendix")
    @ResponseBody
    public AjaxResult addAppendix(@RequestParam("file") MultipartFile file, TargetAppendix targetAppendix) {

        System.out.println("资源查看附件"+fileUploadProp.getFileUploadPath());
        if(file == null){
            return AjaxResult.error("文件不允许为空");
        }
        //文件名
        String fileName = file.getOriginalFilename();
        //文件后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
        //新文件名
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        //拿到路径
        String realPath = fileUploadProp.getFileUploadPath();
        //文件夹不存在的时候创建一个
        File dir = new File(realPath);
        if(!dir.exists()){
            dir.mkdirs();
        }

        String transFerName = newFileName+"."+suffixName;
        //创建file对象,新地址新名字
        File newFile = new File(realPath, transFerName);
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("文件上传失败");
        }

        String id = TargetUtils.getUUID();
        targetAppendix.setId(id);
        targetAppendix.setAppendixName(fileName);
        targetAppendix.setAppendixHidename(transFerName);
        targetAppendix.setAppendixUrl(realPath+"\\"+transFerName);
        //获取当前登陆人
        SysUser sysUser = ShiroUtils.getSysUser();
        targetAppendix.setCreateId(String.valueOf(sysUser.getUserId()));
        targetAppendix.setCreateBy(sysUser.getLoginName());
        targetAppendix.setCreateTime(TargetUtils.getDate());
        return toAjax(targetAppendixService.insertTargetAppendix(targetAppendix));
    }

在这里插入图片描述
希望对您有所帮助!

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot 是一个用于快速构建基于 Spring 框架的应用程序的开发框架。它简化了 Spring 应用程序的开发流程,提供了自动配置和快速启动的能力,让开发者能够更专注于业务逻辑的实现。 Spring Boot 可以实现各种功能,以下是一些常见的功能: 1. Web 应用开发:Spring Boot 提供了对 Web 开发的支持,包括创建 RESTful API、处理 HTTP 请求和响应、路由、过滤器等。 2. 数据访问:Spring Boot 集成了多种数据访问技术,包括 JDBC、ORM 框架(如 Hibernate、MyBatis)、NoSQL 数据库(如 MongoDB、Redis)等。 3. 安全认证与授权:Spring Boot 提供了安全框架,可以进行用户认证和授权管理,支持常见的认证方式(如用户名密码、OAuth2)。 4. 缓存:Spring Boot 支持多种缓存技术,如基于注解的缓存(如 Spring Cache)、分布式缓存(如 Redis)等。 5. 消息队列:Spring Boot 集成了消息队列技术,如 RabbitMQ、Kafka,可以实现异步消息处理和解耦。 6. 定时任务:Spring Boot 提供了定时任务的支持,可以实现定时执行任务或者周期性任务。 7. 日志记录:Spring Boot 集成了常见的日志框架,如 Log4j、Logback,可以方便地进行日志记录和管理。 8. 监控与管理:Spring Boot 提供了健康检查、指标监控等功能,可以方便地进行应用程序的监控和管理。 当然,Spring Boot 还支持其他许多功能,如国际化、文件上传、邮件发送等。开发者可以根据具体需求选择相应的功能进行使用
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值