| | |
| | | import com.lf.server.entity.all.ResponseMsg; |
| | | import com.lf.server.entity.all.SettingData; |
| | | import com.lf.server.entity.ctrl.FileInfo; |
| | | import com.lf.server.entity.data.MetaEntity; |
| | | import com.lf.server.entity.data.MetaFileEntity; |
| | | import com.lf.server.entity.sys.AttachEntity; |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.helper.FileHelper; |
| | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Field; |
| | | import java.net.URLEncoder; |
| | | import java.sql.Timestamp; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取文件 MD5 码 |
| | | */ |
| | | private String getFileMd5(String filePath) throws IOException { |
| | | FileInputStream fileStream = new FileInputStream(filePath); |
| | | String md5 = DigestUtils.md5Hex(fileStream); |
| | |
| | | return md5; |
| | | } |
| | | |
| | | /** |
| | | * 获取附件实体类 |
| | | */ |
| | | protected AttachEntity getAttachEntity(UserEntity ue, String tab, String oldName, String md5, long sizes) { |
| | | AttachEntity entity = new AttachEntity(); |
| | | entity.setName(oldName); |
| | |
| | | /** |
| | | * 下载文件 |
| | | */ |
| | | public void download(String guid, HttpServletRequest req, HttpServletResponse res, BaseController ctrl) { |
| | | public void download(String guid, HttpServletResponse res) { |
| | | try { |
| | | if (StringHelper.isEmpty(guid)) { |
| | | WebHelper.write2Page(res, NO_FILE); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置下载响应信息 |
| | | */ |
| | | private void setDownloadResponse(AttachEntity entity, HttpServletResponse res) throws IOException { |
| | | String fileName = URLEncoder.encode(entity.getName(), "UTF-8"); |
| | | |
| | |
| | | /** |
| | | * 上传文件 |
| | | */ |
| | | public Object uploadData(HttpServletRequest request, HttpServletResponse res) throws Exception { |
| | | public Object uploadData(UserEntity ue, HttpServletRequest request, HttpServletResponse res) throws Exception { |
| | | StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request; |
| | | req.setCharacterEncoding("utf-8"); |
| | | res.setContentType("application/json;charset=utf-8"); |
| | | |
| | | Map<String, String> map = getParams(req); |
| | | List<FileInfo> list = getFiles(req); |
| | | MetaEntity me = getMetaEntity(req); |
| | | List<MetaFileEntity> list = getFiles(req); |
| | | // |
| | | |
| | | return list.size(); |
| | | } |
| | | |
| | | private MetaEntity getMetaEntity(StandardMultipartHttpServletRequest req) { |
| | | MetaEntity me = new MetaEntity(); |
| | | |
| | | Enumeration<String> enumeration = req.getParameterNames(); |
| | | while (enumeration.hasMoreElements()) { |
| | | String key = enumeration.nextElement(); |
| | | |
| | | try { |
| | | Field field = me.getClass().getDeclaredField(key); |
| | | if (field != null) { |
| | | field.setAccessible(true); |
| | | String value = req.getParameter(key); |
| | | switch (field.getType().toString()) { |
| | | case "class java.lang.String": |
| | | field.set(me, value); |
| | | break; |
| | | case "long": |
| | | field.set(me, Long.valueOf(value)); |
| | | break; |
| | | case "int": |
| | | field.set(me, Integer.valueOf(value)); |
| | | break; |
| | | case "class java.sql.Timestamp": |
| | | field.set(me, Timestamp.valueOf(value)); |
| | | break; |
| | | default: |
| | | field.set(me, value); |
| | | break; |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | | // |
| | | } |
| | | } |
| | | |
| | | return me; |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 获取文件 |
| | | */ |
| | | private List<FileInfo> getFiles(StandardMultipartHttpServletRequest req) throws Exception { |
| | | List<FileInfo> list = new ArrayList<FileInfo>(); |
| | | private List<MetaFileEntity> getFiles(StandardMultipartHttpServletRequest req) throws Exception { |
| | | List<MetaFileEntity> list = new ArrayList<MetaFileEntity>(); |
| | | |
| | | String path = pathHelper.getTempPath(); |
| | | Iterator<String> iterator = req.getFileNames(); |
| | | while (iterator.hasNext()) { |
| | | MultipartFile file = req.getFile(iterator.next()); |
| | | |
| | | FileInfo fi = new FileInfo(file.getOriginalFilename()); |
| | | if (StringHelper.isEmpty(fi.getFileName())) { |
| | | if (StringHelper.isEmpty(file.getOriginalFilename())) { |
| | | continue; |
| | | } |
| | | |
| | | fi.setSize(file.getSize()); |
| | | fi.setPath(path + File.separator + fi.getFileName()); |
| | | file.transferTo(new File(fi.getPath())); |
| | | fi.setMd5(getFileMd5(fi.getPath())); |
| | | MetaFileEntity mf = new MetaFileEntity(); |
| | | mf.setName(file.getOriginalFilename()); |
| | | mf.setSizes(file.getSize()); |
| | | mf.setPath(path + File.separator + mf.getName()); |
| | | |
| | | list.add(fi); |
| | | file.transferTo(new File(mf.getPath())); |
| | | mf.setGuid(getFileMd5(mf.getPath())); |
| | | |
| | | list.add(mf); |
| | | } |
| | | |
| | | return list; |