[修改] 文件批量上传
This commit is contained in:
@@ -28,6 +28,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -395,6 +396,19 @@ public class ReportManageConroller {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量上传文件
|
||||
* @param files
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
@ApiOperation(value = "批量上传文件")
|
||||
@PostMapping("/batchUploadFile")
|
||||
public ResponseMessage<List<Map<String, Object>>> batchUploadFile(@RequestParam("files") @ApiParam(value="上传文件",required=true) MultipartFile[] files) throws IOException {
|
||||
List<Map<String, Object>> uploadFile = fileService.batchUploadFile(files);
|
||||
return Result.success(uploadFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
* @param file
|
||||
|
||||
@@ -2,6 +2,10 @@ package com.adc.da.report.service;
|
||||
|
||||
|
||||
import com.adc.da.report.eo.FileEntity;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author doudxw
|
||||
@@ -22,4 +26,5 @@ public interface IFileService {
|
||||
*/
|
||||
FileEntity getFile(String fileId);
|
||||
|
||||
List<Map<String, Object>> batchUploadFile(MultipartFile[] files);
|
||||
}
|
||||
|
||||
@@ -3,19 +3,47 @@ package com.adc.da.report.service.impl;
|
||||
import com.adc.da.report.dao.mysql.FileDao;
|
||||
import com.adc.da.report.eo.FileEntity;
|
||||
import com.adc.da.report.service.IFileService;
|
||||
import javax.annotation.Resource;
|
||||
import com.adc.da.report.util.LocalFileUtil;
|
||||
import com.adc.da.report.util.OSSClientUtil;
|
||||
import com.adc.da.report.util.ObsBootUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @Author doudxw
|
||||
* @Date 2021/11/8 15:00
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class IFileServiceImpl implements IFileService {
|
||||
|
||||
@Resource
|
||||
private FileDao fileDao;
|
||||
|
||||
@Value("${uploadFile}")
|
||||
private String uploadFile;
|
||||
|
||||
@Value("${picPath}")
|
||||
private String picPath;
|
||||
@Value("${uploadType}")
|
||||
private String uploadType;
|
||||
|
||||
@Autowired
|
||||
private ObsBootUtil obsBootUtil;
|
||||
|
||||
@Autowired
|
||||
private LocalFileUtil localFileUtil;
|
||||
|
||||
@Resource
|
||||
private OSSClientUtil ossClientUtil;
|
||||
|
||||
/**
|
||||
* 添加文件
|
||||
@@ -36,4 +64,61 @@ public class IFileServiceImpl implements IFileService {
|
||||
FileEntity fileEntity = fileDao.selectById(fileId);
|
||||
return fileEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public List<Map<String, Object>> batchUploadFile(MultipartFile[] files) {
|
||||
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||
for (MultipartFile file : files) {
|
||||
Map<String, Object> resultMap = new HashMap();
|
||||
//上传路径
|
||||
//此路径映射到localhost
|
||||
String path = uploadFile;
|
||||
String realName = file.getOriginalFilename();
|
||||
Date now = new Date();
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
String date = dateFormat.format(now);
|
||||
Random r = new Random();
|
||||
String num = "";
|
||||
for (int i = 0; i < 3; i++) {
|
||||
int s = r.nextInt(10);
|
||||
num += String.valueOf(s);
|
||||
}
|
||||
|
||||
//获取上传文件名
|
||||
String fileName = file.getOriginalFilename();
|
||||
String contentType = file.getContentType();
|
||||
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
fileName = fileName.substring(0, fileName.lastIndexOf("."))+date + num + "." + ext;
|
||||
log.info("fileName>>" + fileName);
|
||||
try{
|
||||
String result="";
|
||||
// 上传方式:阿里云:alioss 华为云:hwobs
|
||||
if ("hwobs".equals(uploadType)){
|
||||
result = obsBootUtil.upload(file, fileName);
|
||||
}else if ("alioss".equals(uploadType)) {
|
||||
result = ossClientUtil.uploadFile2OSS(file.getInputStream(),fileName);
|
||||
}else if ("local".equals(uploadType)) {
|
||||
localFileUtil.saveFile(file.getInputStream(), fileName);
|
||||
}
|
||||
resultMap.put("key", date + num);
|
||||
resultMap.put("value", path + "//" + fileName);
|
||||
resultMap.put("name", realName);
|
||||
resultList.add(resultMap);
|
||||
}
|
||||
catch (Exception e){
|
||||
log.error(e.getMessage(),e);
|
||||
}
|
||||
FileEntity entity = new FileEntity();
|
||||
entity.setContentType(contentType);
|
||||
entity.setCreateTime(new Date());
|
||||
entity.setFileName(realName);
|
||||
entity.setFileType(ext);
|
||||
entity.setFileSize(String.valueOf(file.getSize()));
|
||||
entity.setFileId(date + num);
|
||||
entity.setSavePath(fileName);
|
||||
this.addFile(entity);
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user