JAVA实现附件上传,同一附件多次上传只保留一份节省磁盘空间

该博客介绍了如何使用Java和Spring Boot实现附件上传功能,并通过优化确保同一附件多次上传时仅保存一份,从而节省磁盘空间。在Controller层,接收前端指定的file参数;Service层处理业务逻辑;Service Impl层实现具体操作;Entity层用于创建数据库表。博主强调代码可适应不同技术框架,只需调整核心逻辑。
摘要由CSDN通过智能技术生成

## 标题JAVA实现附件上传,同一附件多次上传只保留一份节省磁盘空间


)

Controller层

	 @ApiOperation(value = "上传附件", notes = "key为file")
	 @ApiImplicitParam(value = "request", name = "request", dataType = "MultipartHttpServletRequest", required = true, paramType = "body")
	 @PostMapping("upload")
	 public Result<SysAttachment> uploadFile(MultipartHttpServletRequest request)  {
   
		 Result<SysAttachment> result = new Result<>();
		 Map<String, MultipartFile> fileMap = request.getFileMap();
		 if(!fileMap.containsKey("file")) {
   
			 return null;
		 }
		 SysAttachment sysAttachment = sysAttachmentService.uploadFile(fileMap.get("file"));
		 result.setCode(CommonConstant.SC_OK_200);
		 result.setMessage(sysAttachment.getAttachmentName());
		 result.setSuccess(true);
		 result.setResult(sysAttachment);
		 return result;
	 }

注:前端需要传指定参数:file

Service层

package org.jeecg.modules.system.service;

import com.baomidou.mybatisplus.extension.service.IService;
import org.jeecg.modules.system.entity.SysAttachment;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

public interface ISysAttachmentService extends IService<SysAttachment> {
   

    /**
     * 上传附件
     * @param file
     * @return
     */
    SysAttachment uploadFile(MultipartFile file);

}

Service Impl层

package org.jeecg.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.comm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值