package com.sercurityControl.decision.service.impl; import cn.hutool.core.util.IdUtil; import com.sercurityControl.decision.domain.Attachment; import com.sercurityControl.decision.mapper.AttachMapper; import com.sercurityControl.decision.service.FileService; import com.sercurityControl.decision.utils.OssUtil; import org.apache.commons.codec.binary.Base64; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.util.List; @Service public class FileServiceImpl implements FileService { @Resource private AttachMapper attachMapper; @Resource private OssUtil ossUtil; @Override public String queryPicId(String md5) { String base64; if (!StringUtils.hasLength(md5)) { return null; } try { byte[] bytes = getBytes(md5); if (bytes == null){ return null; } base64 = Base64.encodeBase64String(bytes); return base64; } catch (Exception e) { e.printStackTrace(); return null; } } @Override public Attachment upload(MultipartFile file) throws Exception { byte[] bytes = file.getBytes(); if (bytes.length == 0) { return null; } Attachment attachment = new Attachment(); attachment.setFileName(file.getOriginalFilename()); String md5 = ossUtil.fileUpload(file); if (!StringUtils.hasLength(md5)){ return null; } attachment.setMd5(md5); return attachment; } @Override public void getFile(String md5, HttpServletResponse response) { Attachment riskAttachment = attachMapper.queryAttachByMd5(md5); fileDown(riskAttachment,response); } @Override public List getFileList(String pkVal, String colName) { if (!StringUtils.hasLength(pkVal)) { return null; } return attachMapper.getFileList(pkVal,colName); } @Override public Attachment getOneFile(String pkVal, String colName) { if (!StringUtils.hasLength(pkVal)) { return null; } return attachMapper.getFile(pkVal,colName); } @Override @Transactional(rollbackFor = Exception.class) public int insertFileList(List fileList,String pkVal, String tableName, String colName) { if (!StringUtils.hasLength(pkVal)) { return 0; } int j = attachMapper.deleteFileByPkValOrId(new Attachment(pkVal,colName,null)); if (ObjectUtils.isEmpty(fileList)) { return Math.max(0,j); } for (int i = 0; i < fileList.size(); i++){ fileList.get(i).setPkVal(pkVal); fileList.get(i).setColName(colName); fileList.get(i).setTableName(tableName); if (!StringUtils.hasLength(fileList.get(i).getMd5())) { fileList.remove(i); i--;continue; } fileList.get(i).setId(IdUtil.fastSimpleUUID()); } if (ObjectUtils.isEmpty(fileList)) { return Math.max(0,j); } return attachMapper.insertFileList(fileList); } @Override @Transactional(rollbackFor = Exception.class) public int insertFile(Attachment file, String pkVal, String tableName, String colName) { if (!StringUtils.hasLength(pkVal)) { return 0; } if (ObjectUtils.isEmpty(file)) { return attachMapper.deleteFileByPkValOrId(new Attachment(pkVal,colName,null)); }else { if (!StringUtils.hasLength(file.getMd5())) { return 0; } attachMapper.deleteFileByPkValOrId(new Attachment(pkVal,colName,null)); file.setId(IdUtil.fastSimpleUUID()); file.setPkVal(pkVal); file.setTableName(tableName); file.setColName(colName); return attachMapper.insertFile(file); } } void fileDown(Attachment riskAttachment,HttpServletResponse response){ if (ObjectUtils.isEmpty(riskAttachment)|| !StringUtils.hasLength(riskAttachment.getMd5())) { return ; } String fileName = riskAttachment.getFileName(); try { fileName = URLEncoder.encode(fileName, "UTF-8"); byte[] fileContentByte = getBytes(riskAttachment.getMd5()); if (fileContentByte == null){ return ; } response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); OutputStream outputStream = response.getOutputStream(); outputStream.write(fileContentByte); outputStream.flush(); } catch (Exception e) { e.printStackTrace(); } } @Override public byte[] getBytes(String md5) { return ossUtil.getFile(md5); } @Override public InputStream getInputStream(String md5) { byte[] bytes = getBytes(md5); return new ByteArrayInputStream(bytes); } @Override @Transactional(rollbackFor = Exception.class) public int deleteFileByPkValOrId(String pkVal,String id) { if (!StringUtils.hasLength(pkVal) && !StringUtils.hasLength(id)) { return 0; } return attachMapper.deleteFileByPkValOrId(new Attachment(pkVal,null,id)); } }