| | |
| | | import java.lang.reflect.Field; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 上传附件服务类 |
| | | * @author WWW |
| | | */ |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | | public class UploadAttachService { |
| | | @Autowired |
| | | PathHelper pathHelper; |
| | |
| | | |
| | | private final static Log log = LogFactory.getLog(UploadAttachService.class); |
| | | |
| | | /** |
| | | * 获取表名 |
| | | */ |
| | | public static String getTabs() { |
| | | return tabs; |
| | | } |
| | | |
| | | /** |
| | | * 初始化附件表 |
| | | */ |
| | | public static void init(String cfg) { |
| | | if (StringHelper.isEmpty(cfg)) { |
| | | return; |
| | |
| | | tabs = StringHelper.join(keys, ","); |
| | | } |
| | | |
| | | /** |
| | | * 上传文件 |
| | | */ |
| | | public ResponseMsg<String> upload(UserEntity ue, String tab, MultipartFile file, BaseController ctrl) { |
| | | try { |
| | | if (file == null && file.isEmpty()) { |
| | |
| | | return ctrl.fail(String.format("文件大于 %d MB", SettingData.MAX_FILE_SIZE / 1024 / 1024), null); |
| | | } |
| | | |
| | | // 传输文件 |
| | | String oldName = file.getOriginalFilename(); |
| | | String filePath = pathHelper.getTempPath() + File.separator + oldName; |
| | | File newFile = new File(filePath); |
| | | file.transferTo(newFile); |
| | | double sizes = FileHelper.sizeToMb(file.getSize()); |
| | | |
| | | // 获取MD5 |
| | | String md5 = FileHelper.getFileMd5(filePath); |
| | | AttachEntity entity = attachService.selectByGuid(md5); |
| | | if (entity != null) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取附件实体类 |
| | | */ |
| | | protected AttachEntity getAttachEntity(UserEntity ue, String tab, String oldName, String md5, double sizes) { |
| | | AttachEntity entity = new AttachEntity(); |
| | | entity.setName(oldName); |
| | |
| | | return entity; |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | */ |
| | | public void download(String guid, HttpServletResponse res) { |
| | | download(guid, false, res); |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | */ |
| | | public void download(String guid, boolean inline, HttpServletResponse res) { |
| | | try { |
| | | if (StringHelper.isEmpty(guid)) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 上传Excel附件 |
| | | */ |
| | | public List<KeyValueEntity> uploadXlsAnnex(UserEntity ue, List<MetaEntity> ms, List<MetaFileEntity> list, String path) { |
| | | List<String> files = getAttachFiles(list, path); |
| | | if (files.size() == 0) { |
| | |
| | | return rs; |
| | | } |
| | | |
| | | /** |
| | | * 获取附件文件 |
| | | */ |
| | | private List<String> getAttachFiles(List<MetaFileEntity> list, String path) { |
| | | List<String> files = new ArrayList<>(); |
| | | |
| | |
| | | return files; |
| | | } |
| | | |
| | | /** |
| | | * 获取zip中的文件 |
| | | */ |
| | | private List<String> getZipFiles(MetaFileEntity mf, String path) { |
| | | File file = new File(mf.getPath()); |
| | | if (!file.exists() || file.isDirectory()) { |
| | |
| | | return files; |
| | | } |
| | | |
| | | /** |
| | | * 根据路径获取文件 |
| | | */ |
| | | private void getFilesByPath(List<String> list, String path) { |
| | | File file = new File(path); |
| | | if (!file.isDirectory()) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 上传Excel附件 |
| | | */ |
| | | private int uploadXlsAnnex(UserEntity ue, MetaEntity meta, List<String> files) { |
| | | List<?> list = queryXlsData(meta); |
| | | if (null == list || list.size() == 0) { |
| | |
| | | return rows; |
| | | } |
| | | |
| | | /** |
| | | * 获取名称 |
| | | */ |
| | | public static String[] getNames(String str) { |
| | | if (StringHelper.isEmpty(str)) { |
| | | return null; |
| | |
| | | return str.replace(";", ",").replace(",", ",").split(","); |
| | | } |
| | | |
| | | /** |
| | | * 查询Excel数据 |
| | | */ |
| | | private List<?> queryXlsData(MetaEntity meta) { |
| | | String entity = meta.getTab().substring(meta.getTab().indexOf(".") + 1).replace("_", "").toLowerCase(); |
| | | BasicMapper basicMapper = ClassHelper.getBasicMapper(entity); |
| | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 获取附件字段 |
| | | */ |
| | | public static Field getAnnexField(Object obj, String tab) { |
| | | try { |
| | | String str = ATTACH_TABS.get(tab); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取附件名称 |
| | | */ |
| | | public static String getAnnexName(Object obj, Field field) { |
| | | try { |
| | | Object val = field.get(obj); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 查找附件文件 |
| | | */ |
| | | private String findAnnexFile(String name, String folder, List<String> files) { |
| | | name = File.separator + name.split("\\.")[0].trim().toLowerCase(); |
| | | for (String file : files) { |
| | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 插入Excel附件 |
| | | */ |
| | | private int insertXlsAnnex(UserEntity ue, MetaEntity meta, BaseEntity be, String file) { |
| | | File f = new File(file); |
| | | if (!f.exists() || f.isDirectory()) { |
| | |
| | | return attachService.insert(ae); |
| | | } |
| | | |
| | | /** |
| | | * 获取附件实体类 |
| | | */ |
| | | protected AttachEntity getAttachEntity(UserEntity ue, String tabName, String eventid, String fileName, String md5, double sizes) { |
| | | AttachEntity entity = new AttachEntity(); |
| | | entity.setTab(tabName); |
| | |
| | | return entity; |
| | | } |
| | | |
| | | /** |
| | | * 请求下载 |
| | | */ |
| | | public String downloadReqForGuids(UserEntity ue, List<String> guids) { |
| | | List<AttachEntity> list = attachService.selectByGuids(guids); |
| | | |
| | | return downloadReq(ue, list); |
| | | } |
| | | |
| | | /** |
| | | * 请求下载 |
| | | */ |
| | | public String downloadReqForTabGuids(UserEntity ue, String tab, List<String> guids) { |
| | | List<AttachEntity> list = attachService.selectByTabGuids(tab, guids); |
| | | |
| | | return downloadReq(ue, list); |
| | | } |
| | | |
| | | /** |
| | | * 请求下载 |
| | | */ |
| | | protected String downloadReq(UserEntity ue, List<AttachEntity> list) { |
| | | if (null == list || list.isEmpty()) { |
| | | return null; |
| | |
| | | return rows > 0 ? downloadEntity.getGuid() : null; |
| | | } |
| | | |
| | | /** |
| | | * 添加Zip文件 |
| | | */ |
| | | private void addZipFiles(ZipFile zip, ZipParameters params, List<AttachEntity> list) { |
| | | int i = 1; |
| | | String uploadPath = pathHelper.getConfig().getUploadPath(); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取下载实体类 |
| | | */ |
| | | private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) { |
| | | DownloadEntity de = new DownloadEntity(); |
| | | de.setName(FileHelper.getFileName(file)); |