| | |
| | | |
| | | private final static Log log = LogFactory.getLog(UploadAttachService.class); |
| | | |
| | | private static final String NO_FILE = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.NOT_FOUND, "文件找不到")); |
| | | |
| | | /** |
| | | * 获取表名 |
| | | */ |
| | |
| | | entity.setName(oldName); |
| | | entity.setTab(tab); |
| | | entity.setGuid(md5); |
| | | String subPath = PathHelper.getUploadPath() + File.separator + md5; |
| | | String subPath = PathHelper.getUploadPath() + File.separator + md5 + FileHelper.getExtension(oldName); |
| | | entity.setPath(subPath); |
| | | entity.setSizes(sizes); |
| | | if (ue != null) { |
| | |
| | | * 下载文件 |
| | | */ |
| | | 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)) { |
| | | WebHelper.write2Page(res, NO_FILE); |
| | | WebHelper.writeStr2Page(res, StaticData.NO_FILE); |
| | | return; |
| | | } |
| | | |
| | | AttachEntity entity = attachService.selectByGuid(guid); |
| | | if (entity == null) { |
| | | WebHelper.write2Page(res, NO_FILE); |
| | | WebHelper.writeStr2Page(res, StaticData.NO_FILE); |
| | | return; |
| | | } |
| | | |
| | | String file = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath(); |
| | | WebHelper.download(file, entity.getName(), res); |
| | | } catch (Exception ex) { |
| | | try { |
| | | String msg = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.ERROR, "文件下载出错")); |
| | | WebHelper.write2Page(res, msg); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | String filePath = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath(); |
| | | File file = new File(filePath); |
| | | if (!file.exists() || file.isDirectory()) { |
| | | WebHelper.writeJson2Page(res, "文件不存在"); |
| | | } |
| | | |
| | | WebHelper.download(filePath, entity.getName(), inline, res); |
| | | } catch (Exception ex) { |
| | | WebHelper.writeJson2Page(res, "文件下载出错"); |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | |
| | | /** |
| | | * 上传Excel附件 |
| | | */ |
| | | public List<KeyValueEntity> uploadXlsAnnex(List<MetaEntity> ms, List<MetaFileEntity> list, String path) { |
| | | 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 null; |
| | |
| | | |
| | | List<KeyValueEntity> rs = new ArrayList<>(); |
| | | for (MetaEntity meta : ms) { |
| | | int rows = uploadXlsAnnex(meta, files); |
| | | int rows = uploadXlsAnnex(ue, meta, files); |
| | | rs.add(new KeyValueEntity(meta.getName(), String.valueOf(rows))); |
| | | } |
| | | |
| | |
| | | for (MetaFileEntity mf : list) { |
| | | if (mf.getName().contains(StaticData.ZIP)) { |
| | | List<String> subs = getZipFiles(mf, path); |
| | | if (null == subs || subs.size() > 0) { |
| | | if (null != subs && subs.size() > 0) { |
| | | files.addAll(subs); |
| | | } |
| | | continue; |
| | |
| | | /** |
| | | * 上传Excel附件 |
| | | */ |
| | | private int uploadXlsAnnex(MetaEntity meta, List<String> files) { |
| | | private int uploadXlsAnnex(UserEntity ue, MetaEntity meta, List<String> files) { |
| | | List<?> list = queryXlsData(meta); |
| | | if (null == list || list.size() == 0) { |
| | | return 0; |
| | | } |
| | | |
| | | Field field = getAnnexField(list.get(0), meta); |
| | | |
| | | String folder = meta.getName().split("\\.")[0].trim().toLowerCase() + File.separator; |
| | | |
| | | int rows = 0; |
| | | for (Object obj : list) { |
| | | String name = getAnnexName(obj, field); |
| | | if (StringHelper.isEmpty(name)) { |
| | | continue; |
| | | } |
| | | |
| | | String file = findAnnexFile(name, folder, files); |
| | | if (null == file) { |
| | | continue; |
| | | } |
| | | |
| | | BaseEntity be = (BaseEntity) obj; |
| | | rows += insertXlsAnnex(be, files); |
| | | rows += insertXlsAnnex(ue, meta, be, file); |
| | | files.remove(file); |
| | | } |
| | | |
| | | return rows; |
| | |
| | | private Field getAnnexField(Object obj, MetaEntity meta) { |
| | | try { |
| | | String str = ATTACH_TABS.get(meta.getTab()); |
| | | |
| | | Field field = obj.getClass().getDeclaredField(str); |
| | | field.setAccessible(true); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 查找Excel附件 |
| | | * 获取附件名称 |
| | | */ |
| | | private String findXlsAnnex(Object obj) { |
| | | private String getAnnexName(Object obj, Field field) { |
| | | try { |
| | | Object val = field.get(obj); |
| | | |
| | | return null == val ? null : val.toString().trim(); |
| | | } catch (Exception ex) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 查找附件文件 |
| | | */ |
| | | private String findAnnexFile(String name, String folder, List<String> files) { |
| | | name = File.separator + name.split("\\.")[0].trim().toLowerCase(); |
| | | for (String file : files) { |
| | | if (file.toLowerCase().contains(folder) && file.toLowerCase().contains(name)) { |
| | | return file; |
| | | } |
| | | } |
| | | for (String file : files) { |
| | | if (file.toLowerCase().contains(name)) { |
| | | return file; |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | |
| | | /** |
| | | * 插入Excel附件 |
| | | */ |
| | | private int insertXlsAnnex(BaseEntity be, List<String> files) { |
| | | private int insertXlsAnnex(UserEntity ue, MetaEntity meta, BaseEntity be, String file) { |
| | | File f = new File(file); |
| | | if (!f.exists() || f.isDirectory()) { |
| | | return 0; |
| | | } |
| | | |
| | | return 1; |
| | | String md5 = FileHelper.getFileMd5(file); |
| | | AttachEntity old = attachService.selectByTabAndGuid(meta.getTab(), be.getEventid(), md5); |
| | | if (null != old) { |
| | | f.delete(); |
| | | return 0; |
| | | } |
| | | |
| | | String fileName = FileHelper.getFileName(file); |
| | | double sizes = FileHelper.sizeToMb(f.length()); |
| | | |
| | | AttachEntity ae = getAttachEntity(ue, meta.getTab(), be.getEventid(), fileName, md5, sizes); |
| | | String targetPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath(); |
| | | |
| | | f.renameTo(new File(targetPath)); |
| | | int rows = attachService.insert(ae); |
| | | |
| | | return rows > 0 ? 1 : 0; |
| | | } |
| | | |
| | | /** |
| | | * 获取附件实体类 |
| | | */ |
| | | protected AttachEntity getAttachEntity(UserEntity ue, String tabName, String eventid, String fileName, String md5, double sizes) { |
| | | AttachEntity entity = new AttachEntity(); |
| | | entity.setTab(tabName); |
| | | entity.setTabGuid(eventid); |
| | | entity.setName(fileName); |
| | | entity.setGuid(md5); |
| | | String subPath = PathHelper.getUploadPath() + File.separator + md5 + FileHelper.getExtension(fileName); |
| | | entity.setPath(subPath); |
| | | entity.setSizes(sizes); |
| | | if (ue != null) { |
| | | entity.setCreateUser(ue.getId()); |
| | | } |
| | | |
| | | return entity; |
| | | } |
| | | } |