| | |
| | | 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.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; |
| | |
| | | req.setCharacterEncoding("utf-8"); |
| | | res.setContentType("application/json;charset=utf-8"); |
| | | |
| | | setEntity(t, req); |
| | | List<MetaFileEntity> list = getFiles(req); |
| | | if (t != null) { |
| | | setEntity(t, req); |
| | | } |
| | | List<MetaFileEntity> list = getFiles(ue, req); |
| | | |
| | | return list; |
| | | } |
| | |
| | | /** |
| | | * 获取文件 |
| | | */ |
| | | private List<MetaFileEntity> getFiles(StandardMultipartHttpServletRequest req) throws Exception { |
| | | private List<MetaFileEntity> getFiles(UserEntity ue, StandardMultipartHttpServletRequest req) throws Exception { |
| | | List<MetaFileEntity> list = new ArrayList<MetaFileEntity>(); |
| | | |
| | | String path = pathHelper.getTempPath(); |
| | | String path = pathHelper.getTempPath(ue.getId()); |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * 获取文件项 |
| | | */ |
| | | 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"); |
| | | |
| | | // 检查请求是/否为multipart/form-data类型 |
| | | if (!ServletFileUpload.isMultipartContent(req)) { |
| | | throw new RuntimeException("表单的enctype属性不是multipart/form-data类型"); |
| | | } |
| | | |
| | | // 创建上传所需要的两个对象:磁盘文件对象+文件上传对象 |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | /** |