| | |
| | | package com.lf.server.service.all; |
| | | |
| | | import com.lf.server.entity.ctrl.FileInfo; |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.entity.ctrl.FileInfoEntity; |
| | | import com.lf.server.helper.PathHelper; |
| | | import com.lf.server.helper.StringHelper; |
| | | import org.apache.commons.fileupload.FileItem; |
| | | import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
| | | import org.apache.commons.fileupload.servlet.ServletFileUpload; |
| | | import org.apache.commons.fileupload.servlet.ServletRequestContext; |
| | | 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; |
| | | |
| | |
| | | |
| | | private final static Log log = LogFactory.getLog(CommonsFileuploadService.class); |
| | | |
| | | public Object fileUpload(UserEntity ue, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | public Object fileUpload(HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | List<FileItem> items = getFileItem(req, res); |
| | | |
| | | return copeFileItems(ue, items, req); |
| | | return copeFileItems(items, req); |
| | | } |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 处理解析内容:处理普通表单域和文件表单域 |
| | | */ |
| | | private Object copeFileItems(UserEntity ue, List<FileItem> items, HttpServletRequest req) throws Exception { |
| | | 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>(); |
| | | List<FileInfoEntity> list = new ArrayList<FileInfoEntity>(); |
| | | |
| | | String path = pathHelper.getTempPath(ue.getId()); |
| | | String path = pathHelper.getTempPath(); |
| | | for (FileItem item : items) { |
| | | if (item.isFormField()) { |
| | | String key = item.getFieldName(); |
| | |
| | | continue; |
| | | } |
| | | |
| | | FileInfo fi = copeFile(item, path, req); |
| | | FileInfoEntity fi = copeFile(item, path, req); |
| | | if (fi != null) { |
| | | list.add(fi); |
| | | } |
| | |
| | | /** |
| | | * 处理文件 |
| | | */ |
| | | private FileInfo copeFile(FileItem item, String path, HttpServletRequest req) { |
| | | private FileInfoEntity copeFile(FileItem item, String path, HttpServletRequest req) { |
| | | try { |
| | | // 获取文件名,判断是否合法 |
| | | FileInfo fi = new FileInfo(item.getName()); |
| | | FileInfoEntity fi = new FileInfoEntity(item.getName()); |
| | | if (StringHelper.isEmpty(fi.getFileName())) { |
| | | return null; |
| | | } |
| | |
| | | |
| | | return fi; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getStackTrace()); |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | } |
| | | } |