¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.service.all; |
| | | |
| | | import com.lf.server.entity.ctrl.FileInfoEntity; |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | 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 javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * CommonsFileuploadä¸ä¼ æå¡ |
| | | * @author WWW |
| | | */ |
| | | @Service |
| | | public class CommonsFileuploadService { |
| | | @Autowired |
| | | PathHelper pathHelper; |
| | | |
| | | private final static Log log = LogFactory.getLog(CommonsFileuploadService.class); |
| | | |
| | | 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<FileInfoEntity> list = new ArrayList<FileInfoEntity>(); |
| | | |
| | | 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; |
| | | } |
| | | |
| | | FileInfoEntity fi = copeFile(item, path, req); |
| | | if (fi != null) { |
| | | list.add(fi); |
| | | } |
| | | } |
| | | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * å¤çæä»¶ |
| | | */ |
| | | private FileInfoEntity copeFile(FileItem item, String path, HttpServletRequest req) { |
| | | try { |
| | | // è·åæä»¶åï¼å¤ææ¯å¦åæ³ |
| | | FileInfoEntity fi = new FileInfoEntity(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.getMessage(), ex); |
| | | return null; |
| | | } |
| | | } |
| | | } |