| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest; |
| | | |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | |
| | | |
| | | @Autowired |
| | | AttachService attachService; |
| | | |
| | | private final static long ONE_DAY = 24 * 60 * 60 * 1000; |
| | | |
| | | private final static long SIZE_MAX = 1024 * 1024 * 1024 * 1024; |
| | | |
| | | private final static long FILE_SIZE_MAX = 256 * 1024 * 1024 * 1024; |
| | | |
| | | private final static Log log = LogFactory.getLog(UploaderService.class); |
| | | |
| | |
| | | /** |
| | | * 上传文件 |
| | | */ |
| | | public void uploadData(HttpServletRequest req, HttpServletResponse res) { |
| | | try { |
| | | // 处理中文乱码问题 |
| | | req.setCharacterEncoding("utf-8"); |
| | | res.setContentType("text/html;charset=utf-8"); |
| | | public Object uploadData(HttpServletRequest request, HttpServletResponse res) throws Exception { |
| | | StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request; |
| | | req.setCharacterEncoding("utf-8"); |
| | | res.setContentType("application/json;charset=utf-8"); |
| | | |
| | | // 检查请求是/否为multipart/form-data类型 |
| | | if (!ServletFileUpload.isMultipartContent(req)) { |
| | | throw new RuntimeException("表单的enctype属性不是multipart/form-data类型"); |
| | | Map<String, String> map = getParams(req); |
| | | List<FileInfo> list = getFiles(req); |
| | | |
| | | return list.size(); |
| | | } |
| | | |
| | | private Map<String, String> getParams(StandardMultipartHttpServletRequest req) { |
| | | Map<String, String> map = new HashMap<String, String>(3); |
| | | |
| | | Enumeration<String> enumeration = req.getParameterNames(); |
| | | while (enumeration.hasMoreElements()) { |
| | | String key = enumeration.nextElement(); |
| | | String value = req.getParameter(key); |
| | | map.put(key, value); |
| | | } |
| | | |
| | | return map; |
| | | } |
| | | |
| | | private List<FileInfo> getFiles(StandardMultipartHttpServletRequest req) throws Exception { |
| | | List<FileInfo> list = new ArrayList<FileInfo>(); |
| | | |
| | | 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())) { |
| | | continue; |
| | | } |
| | | |
| | | // 创建上传所需要的两个对象:磁盘文件对象+文件上传对象 |
| | | DiskFileItemFactory factory = new DiskFileItemFactory(); |
| | | ServletFileUpload sfu = new ServletFileUpload(factory); |
| | | ServletRequestContext ctx = new ServletRequestContext(req); |
| | | fi.setSize(file.getSize()); |
| | | fi.setPath(path + File.separator + fi.getFileName()); |
| | | file.transferTo(new File(fi.getPath())); |
| | | |
| | | // 限制上传的总文件大小 |
| | | sfu.setSizeMax(SIZE_MAX); |
| | | // 限制单个文件的大小 |
| | | sfu.setFileSizeMax(FILE_SIZE_MAX); |
| | | // 设置编码方式 |
| | | sfu.setHeaderEncoding("utf-8"); |
| | | |
| | | // list容器用来保存表单中的所有数据信息 |
| | | List<FileItem> items = sfu.parseRequest(ctx); |
| | | copeFileItems(items, req); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | list.add(fi); |
| | | } |
| | | |
| | | 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 void copeFileItems(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.add(fi); |
| | | } |
| | | } |
| | | |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private void handleFileField(FileItem item, HttpServletRequest req) { |
| | | // 获取 文件数据项中的 文件名 |
| | | String fileName = item.getName(); |
| | | |
| | | // 控制只能上传图片 |
| | | String img = "image"; |
| | | if (!item.getContentType().startsWith(img)) { |
| | | return; |
| | | } |
| | | |
| | | // 获取 当前项目下的 /files 目录的绝对位置 |
| | | ServletContext ctx = req.getSession().getServletContext(); |
| | | String path = ctx.getRealPath("/files"); |
| | | |
| | | // 创建 file对象 |
| | | File file = new File(path); |
| | | if (!file.exists()) { |
| | | // 创建目录 |
| | | file.mkdir(); |
| | | } |
| | | |
| | | // 将文件保存到服务器上(UUID是通用唯一标识码) |
| | | try { |
| | | item.write(new File(file.toString(), UUID.randomUUID() + "_" + fileName)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |