| | |
| | | package com.lf.server.service.data; |
| | | |
| | | import com.lf.server.entity.ctrl.FileInfo; |
| | | import com.lf.server.entity.data.MetaFileEntity; |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.helper.FileHelper; |
| | | import com.lf.server.helper.PathHelper; |
| | | import com.lf.server.helper.StringHelper; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.apache.tomcat.util.http.fileupload.FileItem; |
| | | import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory; |
| | | import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload; |
| | | import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | |
| | | /** |
| | | * 上传文件 |
| | | */ |
| | | public <T> List<MetaFileEntity> uploadData(T t, UserEntity ue, HttpServletRequest request, HttpServletResponse res) throws Exception { |
| | | StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request; |
| | | public <T> List<MetaFileEntity> uploadData(T t, String path, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | StandardMultipartHttpServletRequest request = (StandardMultipartHttpServletRequest) req; |
| | | req.setCharacterEncoding("utf-8"); |
| | | res.setContentType("application/json;charset=utf-8"); |
| | | |
| | | setEntity(t, req); |
| | | List<MetaFileEntity> list = getFiles(req); |
| | | if (t != null) { |
| | | setEntity(t, request); |
| | | } |
| | | List<MetaFileEntity> list = getFiles(path,request); |
| | | |
| | | return list; |
| | | } |
| | |
| | | String value = req.getParameter(key); |
| | | |
| | | switch (field.getType().toString()) { |
| | | case "double": |
| | | field.set(t, Double.valueOf(value)); |
| | | break; |
| | | case "long": |
| | | field.set(t, Long.valueOf(value)); |
| | | break; |
| | |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error(ex.getStackTrace()); |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | } |
| | |
| | | /** |
| | | * 获取文件 |
| | | */ |
| | | private List<MetaFileEntity> getFiles(StandardMultipartHttpServletRequest req) throws Exception { |
| | | private List<MetaFileEntity> getFiles(String subPath,StandardMultipartHttpServletRequest req) throws Exception { |
| | | List<MetaFileEntity> list = new ArrayList<MetaFileEntity>(); |
| | | |
| | | String path = pathHelper.getTempPath(); |
| | | String path = pathHelper.getTempPath(subPath); |
| | | Iterator<String> iterator = req.getFileNames(); |
| | | while (iterator.hasNext()) { |
| | | MultipartFile file = req.getFile(iterator.next()); |
| | |
| | | return list; |
| | | } |
| | | |
| | | public Object fileUpload(HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | List<FileItem> items = getFileItem(req, res); |
| | | |
| | | return copeFileItems(items, req); |
| | | /** |
| | | * 自理实体集合 |
| | | */ |
| | | public void dealEntities(List<MetaFileEntity> list) { |
| | | for (MetaFileEntity entity : list) { |
| | | entity.setPath(FileHelper.getRelativePath(entity.getPath())); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取文件项 |
| | | * 删除文件 |
| | | * @param list 实体集合 |
| | | * @return |
| | | */ |
| | | private List<FileItem> getFileItem(HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | // 处理中文乱码问题 |
| | | req.setCharacterEncoding("utf-8"); |
| | | // text/html;charset=utf-8 |
| | | res.setContentType("application/json;charset=utf-8"); |
| | | public Integer deleteFiles(List<MetaFileEntity> list){ |
| | | String root=pathHelper.getConfig().getTempPath(); |
| | | |
| | | // 检查请求是/否为multipart/form-data类型 |
| | | if (!ServletFileUpload.isMultipartContent(req)) { |
| | | throw new RuntimeException("表单的enctype属性不是multipart/form-data类型"); |
| | | } |
| | | int count = 0; |
| | | for (MetaFileEntity entity : list) { |
| | | if (!StringHelper.isEmpty(entity.getPath())){ |
| | | String file=root+File.separator+entity.getPath(); |
| | | |
| | | // 创建上传所需要的两个对象:磁盘文件对象+文件上传对象 |
| | | DiskFileItemFactory factory = new DiskFileItemFactory(); |
| | | ServletFileUpload sfu = new ServletFileUpload(factory); |
| | | ServletRequestContext ctx = new ServletRequestContext(req); |
| | | |
| | | // 设置编码方式 |
| | | sfu.setHeaderEncoding("utf-8"); |
| | | factory.setSizeThreshold(4096); |
| | | |
| | | // 获取表单中的所有数据信息 |
| | | List<FileItem> list = sfu.parseRequest(ctx); |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * 处理解析内容:处理普通表单域和文件表单域 |
| | | */ |
| | | private Object copeFileItems(List<FileItem> items, HttpServletRequest req) throws Exception { |
| | | Map<String, String> map = new HashMap<String, String>(3); |
| | | List<FileInfo> list = new ArrayList<FileInfo>(); |
| | | |
| | | String path = pathHelper.getTempPath(); |
| | | for (FileItem item : items) { |
| | | if (item.isFormField()) { |
| | | String key = item.getFieldName(); |
| | | String value = item.getString("utf-8"); |
| | | map.put(key, value); |
| | | continue; |
| | | } |
| | | |
| | | FileInfo fi = copeFile(item, path, req); |
| | | if (fi != null) { |
| | | list.add(fi); |
| | | File f=new File(file); |
| | | if (f.exists()){ |
| | | f.delete(); |
| | | count++; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 处理文件 |
| | | */ |
| | | private FileInfo copeFile(FileItem item, String path, HttpServletRequest req) { |
| | | try { |
| | | // 获取文件名,判断是否合法 |
| | | FileInfo fi = new FileInfo(item.getName()); |
| | | if (StringHelper.isEmpty(fi.getFileName())) { |
| | | return null; |
| | | } |
| | | |
| | | fi.setSize(item.getSize()); |
| | | fi.setPath(path + File.separator + fi.getFileName()); |
| | | item.write(new File(fi.getPath())); |
| | | |
| | | return fi; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getStackTrace()); |
| | | return null; |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | /** |