| | |
| | | package cc.mrbird.febs.server.hr.service.impl; |
| | | |
| | | import java.io.*; |
| | | import java.util.*; |
| | | |
| | | import cc.mrbird.febs.common.core.constant.ModuleCode; |
| | | import cc.mrbird.febs.common.core.exception.FebsException; |
| | | import cc.mrbird.febs.common.core.utils.FebsUtil; |
| | | import cc.mrbird.febs.common.core.utils.SequenceUtil; |
| | | import cc.mrbird.febs.server.hr.entity.FilesUpload; |
| | | import cc.mrbird.febs.server.hr.entity.Folder; |
| | | import cc.mrbird.febs.server.hr.mapper.FilesUploadMapper; |
| | | import cc.mrbird.febs.server.hr.properties.FebsServerHrProperties; |
| | | import cc.mrbird.febs.server.hr.service.IFilesUploadService; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.core.io.file.FileWriter; |
| | | import cn.hutool.core.lang.Snowflake; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.transaction.annotation.Propagation; |
| | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import cc.mrbird.febs.common.core.entity.QueryRequest; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.List; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | | /** |
| | | * 文件上传 Service实现 |
| | |
| | | public class FilesUploadServiceImpl extends ServiceImpl<FilesUploadMapper, FilesUpload> implements IFilesUploadService { |
| | | |
| | | private final FilesUploadMapper filesUploadMapper; |
| | | private final FebsServerHrProperties properties; |
| | | private final String operatorId = Optional.ofNullable(FebsUtil.getCurrentUser()) |
| | | .map(u -> u.getUserId().toString()) |
| | | .orElse("1"); |
| | | /** |
| | | * 参数2为数据中心ID 参数1为终端ID |
| | | */ |
| | | private Snowflake snowflake = IdUtil.getSnowflake(ModuleCode.HR_FIlE, 1); |
| | | |
| | | @Override |
| | | public IPage<FilesUpload> findFilesUploads(QueryRequest request, FilesUpload filesUpload) { |
| | |
| | | @Override |
| | | public List<FilesUpload> findFilesUploads(FilesUpload filesUpload) { |
| | | LambdaQueryWrapper<FilesUpload> queryWrapper = new LambdaQueryWrapper<>(); |
| | | if (null != filesUpload.getFolderid()){ |
| | | queryWrapper.eq(FilesUpload::getFolderid,filesUpload.getFolderid()); |
| | | queryWrapper.eq(FilesUpload::getDelFlag,0); |
| | | if (null != filesUpload.getFolderid()) { |
| | | queryWrapper.eq(FilesUpload::getFolderid, filesUpload.getFolderid()); |
| | | } |
| | | if (StrUtil.isNotBlank(filesUpload.getFilesname())){ |
| | | queryWrapper.like(FilesUpload::getFilesname,filesUpload.getFilesname()) ; |
| | | if (StrUtil.isNotBlank(filesUpload.getFilesname())) { |
| | | queryWrapper.like(FilesUpload::getFilesname, filesUpload.getFilesname()); |
| | | } |
| | | queryWrapper.orderByDesc(FilesUpload::getModifytime); |
| | | return this.baseMapper.selectList(queryWrapper); |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void createFilesUpload(FilesUpload filesUpload) { |
| | | public void createFilesUpload(Long folderid, MultipartFile file) throws FebsException, IOException { |
| | | if (file.isEmpty()) { |
| | | throw new FebsException("上传的文件不能为空!请重新上传"); |
| | | } |
| | | if (file.getSize() <= 0) { |
| | | throw new FebsException("上传的文件大小需要大于0kb"); |
| | | } |
| | | if (file.getSize() > 50 * 1024* 1024) { |
| | | throw new FebsException("上传的文件大于50M"); |
| | | } |
| | | if (!FileUtil.exist(properties.getUploadPpath())) { |
| | | FileUtil.mkdir(properties.getUploadPpath()); |
| | | } |
| | | //原本名字 |
| | | String fileName = file.getOriginalFilename(); |
| | | String suffix = ""; |
| | | if (fileName.indexOf(".") > 0) { |
| | | //后缀 |
| | | suffix = fileName.substring(fileName.indexOf("."), fileName.length()); |
| | | } |
| | | //生成新的名字 |
| | | String newName = snowflake.nextIdStr() + suffix; |
| | | //上传 |
| | | file.transferTo(new File(properties.getUploadPpath() + newName)); |
| | | |
| | | FilesUpload filesUpload = new FilesUpload(); |
| | | filesUpload.setCreatetime(new Date()); |
| | | filesUpload.setCreator(operatorId); |
| | | filesUpload.setDelFlag(0); |
| | | filesUpload.setFilesaddress(newName); |
| | | filesUpload.setFilesformat(StrUtil.isBlank(suffix) ? suffix : suffix.substring(1)); |
| | | filesUpload.setFilesid(SequenceUtil.generateId(0L, ModuleCode.HR_FIlE)); |
| | | filesUpload.setFilesname(fileName); |
| | | filesUpload.setFolderid(folderid); |
| | | filesUpload.setModifier(operatorId); |
| | | filesUpload.setModifytime(new Date()); |
| | | filesUpload.setVersion(0); |
| | | this.save(filesUpload); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteFilesUpload(FilesUpload filesUpload) { |
| | | LambdaQueryWrapper<FilesUpload> wapper = new LambdaQueryWrapper<>(); |
| | | // TODO 设置删除条件 |
| | | this.remove(wapper); |
| | | public void deleteFilesUpload(String fileids) { |
| | | String[] arr = fileids.split(","); |
| | | LambdaUpdateWrapper<FilesUpload> wapper = new LambdaUpdateWrapper<>(); |
| | | wapper.in(FilesUpload::getFilesid,arr).setSql("delFlag = 1"); |
| | | this.update(wapper); |
| | | |
| | | LambdaQueryWrapper<FilesUpload> queryWrapper = new LambdaQueryWrapper<>(); |
| | | wapper.in(FilesUpload::getFilesid,arr); |
| | | List<FilesUpload> list = this.list(queryWrapper); |
| | | list.parallelStream().forEach(i-> FileUtil.del(properties.getUploadPpath() + i.getFilesaddress())); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void download(String fileids, HttpServletRequest request, HttpServletResponse response) { |
| | | String[] arr = fileids.split(","); |
| | | List<File> files = new ArrayList<>(); |
| | | Arrays.stream(arr).forEach(i -> { |
| | | FilesUpload filesUpload = this.getById(i); |
| | | files.add(new File(properties.getUploadPpath() + filesUpload.getFilesaddress())); |
| | | }); |
| | | //设置压缩包的名字 |
| | | //解决不同浏览器压缩包名字含有中文时乱码的问题 |
| | | String downloadName ="PersonnelInformation-" + DateUtil.format(new Date(), "yyyyMMddhhmmsss") + ".zip"; |
| | | String agent = request.getHeader("USER-AGENT"); |
| | | try { |
| | | if (agent.contains("MSIE") || agent.contains("Trident")) { |
| | | downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8"); |
| | | } else { |
| | | downloadName = new String(downloadName.getBytes("UTF-8"), "ISO-8859-1"); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\""); |
| | | |
| | | |
| | | //设置压缩流:直接写入response,实现边压缩边下载 |
| | | ZipOutputStream zipos = null; |
| | | try { |
| | | zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream())); |
| | | //设置压缩方法 |
| | | zipos.setMethod(ZipOutputStream.DEFLATED); |
| | | } catch (Exception e) { |
| | | log.error("设置压缩流出现异常" + e.getMessage()); |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | //循环将文件写入压缩流 |
| | | DataOutputStream os = null; |
| | | for (int i = 0; i < files.size(); i++) { |
| | | File file = files.get(i); |
| | | try { |
| | | //添加ZipEntry,并ZipEntry中写入文件流 |
| | | //这里,加上i是防止要下载的文件有重名的导致下载失败 |
| | | zipos.putNextEntry(new ZipEntry(file.getName())); |
| | | os = new DataOutputStream(zipos); |
| | | InputStream is = new FileInputStream(file); |
| | | byte[] b = new byte[100]; |
| | | int length; |
| | | while ((length = is.read(b)) != -1) { |
| | | os.write(b, 0, length); |
| | | } |
| | | is.close(); |
| | | zipos.closeEntry(); |
| | | } catch (IOException e) { |
| | | log.error("循环将文件写入压缩流出现异常" + e.getMessage()); |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | //关闭流 |
| | | try { |
| | | os.flush(); |
| | | os.close(); |
| | | zipos.close(); |
| | | } catch (IOException e) { |
| | | log.error("关闭流现异常" + e.getMessage()); |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | } |