赞
踩
import com.baomidou.mybatisplus.activerecord.Model; import com.baomidou.mybatisplus.annotations.TableId; import com.baomidou.mybatisplus.enums.IdType; import java.io.Serializable; import java.util.Date; public class File extends Model<File> { private static final long serialVersionUID = 1L; @TableId(value="id", type= IdType.UUID) private String id; private String name; private String url; private String remarks; private Date createDate; private String createUser; private String suffix; private String fileMd5; private String groupName; private String fullPath; private String size; private String brandOfficeId; public String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } public String getBrandOfficeId() { return brandOfficeId; } public void setBrandOfficeId(String brandOfficeId) { this.brandOfficeId = brandOfficeId; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public String getFullPath() { return fullPath; } public void setFullPath(String fullPath) { this.fullPath = fullPath; } public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public String getCreateUser() { return createUser; } public void setCreateUser(String createUser) { this.createUser = createUser; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } public String getFileMd5() { return fileMd5; } public void setFileMd5(String fileMd5) { this.fileMd5 = fileMd5; } @Override protected Serializable pkVal() { return this.id; } }
model:FileDto
import java.util.Date; public class FileDto { private static final long serialVersionUID = 1L; private String id; private String name; private String url; private String remarks; private Date createDate; private String createUser; private String suffix; private String fileMd5; private String groupName; private String fullPath; private String size; private String brandOfficeId; public String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } public String getBrandOfficeId() { return brandOfficeId; } public void setBrandOfficeId(String brandOfficeId) { this.brandOfficeId = brandOfficeId; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public String getFullPath() { return fullPath; } public void setFullPath(String fullPath) { this.fullPath = fullPath; } public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public String getCreateUser() { return createUser; } public void setCreateUser(String createUser) { this.createUser = createUser; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } public String getFileMd5() { return fileMd5; } public void setFileMd5(String fileMd5) { this.fileMd5 = fileMd5; } }
Controller:FileController
package com.kcidea.bms.controller; import com.alibaba.fastjson.JSONObject; import com.github.tobato.fastdfs.service.AppendFileStorageClient; import com.kcidea.bms.common.bean.ObjectResult; import com.kcidea.bms.common.util.*; import com.kcidea.bms.common.utils.ReadProper; import com.kcidea.bms.domain.User; import com.kcidea.bms.model.FileDto; import com.kcidea.bms.service.FileService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Date; /** * Created by Administrator on 2019/3/27 0027. */ @Slf4j @Controller @RequestMapping("/file") public class FileController { @Resource FileService fileService; @Resource private AppendFileStorageClient appendFileStorageClient; @Resource private FastDFSClientWrapper fastDFSClientWrapper; /** * 文件上传 * @param file * @param redirectAttributes * @return */ @PostMapping("/upload") @ResponseBody public ObjectResult singleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes,String type, HttpSession session) { ObjectResult result = new ObjectResult(); result.setFlag(false); if (file.isEmpty()) { result.setMsg(Vm.DAMAGED_OR_EMPTY_FILES); return result; } try { FileDto fileDto=saveFile(file); User user = WebUtils.getUserBySession(session); fileDto.setCreateDate(new Date()); fileDto.setCreateUser(user.getId()); fileDto.setBrandOfficeId(user.getBrandOfficeId()); com.kcidea.bms.domain.File tempFile = fileService.addFile(fileDto); result.setFlag(true); result.setData(tempFile); result.setMsg(Vm.SUCCESS); return result; } catch (Exception e) { log.error("文件上传失败:"+e.getMessage(),e); } return result; } /** * 保存文件 * @param multipartFile * @return * @throws IOException */ public FileDto saveFile(MultipartFile multipartFile) throws IOException { FileDto fileDto = new FileDto(); String[] fileAbsolutePath={}; String fileName=multipartFile.getOriginalFilename(); fileDto.setName(fileName.substring(0,fileName.lastIndexOf("."))); String ext = fileName.substring(fileName.lastIndexOf(".") + 1); fileDto.setSuffix(ext); byte[] file_buff = null; InputStream inputStream=multipartFile.getInputStream(); if(inputStream!=null){ int len1 = inputStream.available(); file_buff = new byte[len1]; inputStream.read(file_buff); } inputStream.close(); FastDFSFile file = new FastDFSFile(fileName, file_buff, ext); try { fileAbsolutePath = FastDFSClient.upload(file); //upload to fastdfs } catch (Exception e) { log.error("文件上传失败:"+e.getMessage(),e); } if (fileAbsolutePath==null) { log.error("upload file failed,please upload again!"); } String path=FastDFSClient.getTrackerUrl()+fileAbsolutePath[0]+ "/"+fileAbsolutePath[1]; fileDto.setUrl(fileAbsolutePath[1]); fileDto.setGroupName(fileAbsolutePath[0]); fileDto.setFullPath(path); return fileDto; } /** * 文件删除接口 * @param response * @param groupName * @param remoteFileName * @throws IOException */ @RequestMapping(value = "/deleteFile") @ResponseBody public ObjectResult deleteFile(HttpServletResponse response,String groupName,String remoteFileName,String fileId) throws IOException { ObjectResult result = new ObjectResult(); try { //例子FastDFSClient.deleteFile("group1","M00/00/00/wKgBrVycZQ-ARi-9AAIMpuR-Zfc06.jpeg"); FastDFSClient.deleteFile(groupName,remoteFileName); /*projectFileRelService.deleteProjectFileRelByFileId(fileId);*/ result.setFlag(true); } catch (IOException e) { log.error("文件删除失败:"+e.getMessage(),e); result.setFlag(false); return result; } catch (Exception e) { log.error("文件删除失败:"+e.getMessage(),e); result.setFlag(false); return result; } return result; } /** * 根据md5查询文件是否存在 * @param md5 * @return */ @RequestMapping(value = "/getFileByMd5") @ResponseBody public ObjectResult getFileByMd5(String md5,HttpSession session){ ObjectResult result = new ObjectResult(); result.setFlag(false); User user = WebUtils.getUserBySession(session); if(null==user){ //未登录 result.setMsg(Vm.USER_NOT_LOGIN); return result; } if(StringUtils.isBlank(md5)){ result.setMsg(Vm.REQUIRED_ENTRIES_ARE_EMPTY); return result; } try { com.kcidea.bms.domain.File file = fileService.getFileByMd5(md5); result.setData(file); result.setMsg(Vm.SUCCESS); result.setFlag(true); }catch (Exception e){ result.getMsg(Vm.FAIL); log.error("根据md5查询文件是否存在:"+e.getMessage(),e); } return result; } //获取配置 @RequestMapping("/config") @ResponseBody public String config(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("fileServerUrl", "http:\\\\"+ ReadProper.getResourceValue("fileServerUrl")+"/"); jsonObject.put("maxSize", ReadProper.getResourceValue("maxSize")); return jsonObject.toString(); } /** * 文件上传 * 最后返回fastDFS中的文件名称;group1/M00/01/04/CgMKrVvS0geAQ0pzAACAAJxmBeM793.doc * @param file 页面提交时文件 * @return */ @PostMapping("/uploadFile") public String uploadFile(MultipartFile file){ byte[] bytes = new byte[0]; try { bytes = file.getBytes(); } catch (IOException e) { log.error("获取文件错误"); e.printStackTrace(); } //获取源文件名称 String originalFileName = file.getOriginalFilename(); //获取文件后缀--.doc String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1); String fileName = file.getName(); //获取文件大小 long fileSize = file.getSize(); System.out.println(originalFileName + "==" + fileName + "==" + fileSize + "==" + extension + "==" + bytes.length); return fastDFSClientWrapper.uploadFile(bytes, fileSize, extension); } }
FileService
package com.kcidea.bms.service; import com.kcidea.bms.domain.File; import com.kcidea.bms.model.FileDto; /** * Created by kcidea-code on 2019/3/29. */ public interface FileService { /** * 添加一个文件 * @param fileDto */ File addFile(FileDto fileDto); /** * 文件id查询文件 * @param ysbgFileId * @return */ File getFileById(String ysbgFileId); /** * md5 查询文件信息 * @param md5 * @return */ File getFileByMd5(String md5); }
FileServiceImpl
package com.kcidea.bms.service.impl; import com.baomidou.mybatisplus.plugins.Page; import com.kcidea.bms.common.util.Contants; import com.kcidea.bms.common.util.MsgException; import com.kcidea.bms.common.util.Vm; import com.kcidea.bms.dao.*; import com.kcidea.bms.domain.File; import com.kcidea.bms.model.*; import com.kcidea.bms.service.FileService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; /** * Created by kcidea-code on 2019/3/29. */ @Service @Slf4j @Transactional public class FileServiceImpl implements FileService{ @Resource private FileDao fileDao; @Override public File addFile(FileDto fileDto) { File file = new File(); BeanUtils.copyProperties(fileDto,file); fileDao.insert(file); return file; } @Override public File getFileById(String ysbgFileId) { return fileDao.selectById(ysbgFileId); } @Override public File getFileByMd5(String md5) { File file = new File(); file.setFileMd5(md5); file = fileDao.selectOne(file); return file; } }
FileDao
package com.kcidea.bms.dao; import com.baomidou.mybatisplus.mapper.BaseMapper; import com.baomidou.mybatisplus.plugins.Page; import com.kcidea.bms.domain.File; import com.kcidea.bms.model.FileDto; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; /** * Created by kcidea-code on 2019/3/29. */ @Repository public interface FileDao extends BaseMapper<File>{ /** * 查询近一个月的文件 * @return */ List<FileDto> findAllFilesByLastMonth(); /** * 根据项目id查询当前项目中的文件 * @param projectId * @return */ List<FileDto> findFilesByProjectId(String projectId); /** * @Description 根据项目id查询当前项目中的文件/分页 * @Author wuliwei * @Date 2019/5/6 * @Param [projectId, page] * @return java.util.List<com.kcidea.architecture.model.FileDto> */ List<FileDto> findFilesByProjectIdPage(@Param("projectId") String projectId, @Param("page") Page<FileDto> page, @Param("fileName") String fileName); /** * * @param visaId * @param fileType * @return */ List<FileDto> findFilesByVisaIdAndFileType(@Param("visaId") String visaId, @Param("fileType") String fileType); /** * * @param progressId * @return */ List<FileDto> findFilesByProgressId(@Param("progressId") String progressId, @Param("type") String type); /** * 根据预算查询文件 * @param budgetId * @param page * @param fileName * @param currentStep * @return */ List<FileDto> findFilesByBudgetIdPage(@Param("budgetId") String budgetId, @Param("page") Page<FileDto> page, @Param("fileName") String fileName, @Param("currentStep") String currentStep); /** * 根据结算查询文件 * @param settlementId * @param page * @param fileName * @param currentStep * @return */ List<FileDto> findFilesBySettlementIdPage(@Param("settlementId") String settlementId, @Param("page") Page<FileDto> page, @Param("fileName") String fileName, @Param("currentStep") String currentStep); /** * 根据文件id查询文件信息 * @param fileId * @return */ FileDto getFileById(@Param("fileId") String fileId); }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。