public static State save(String content, Map<String, Object> conf) { byte[] data = decode(content); long maxSize = ((Long) conf.get("maxSize")).longValue(); if (!validSize(data, maxSize)) { return new BaseState(false, AppInfo.MAX_SIZE); } String suffix = FileType.getSuffix("JPG"); String savePath = PathFormat.parse((String) conf.get("savePath"), (String) conf.get("filename")); savePath = savePath + suffix; String physicalPath = (String) conf.get("rootPath") + savePath; State storageState = StorageManager.saveBinaryFile(data, physicalPath); if (storageState.isSuccess()) { storageState.putInfo("url", PathFormat.format(savePath)); storageState.putInfo("type", suffix); storageState.putInfo("original", ""); } return storageState; }
@ResponseBody @RequestMapping(value = "/upload", method = RequestMethod.POST) public ImmutableMap<String, String> linkImgUpload( MultipartFile file, HttpServletRequest request) { if (file.isEmpty()) { return ImmutableMap.of("status", "0", "message", getMessage("global.uploadempty.message")); } Site site = (Site) (SystemUtils.getSessionSite()); String linkPath = PathFormat.parseSiteId(SystemConstant.LINK_RES_PATH, site.getSiteId() + ""); String realPath = HttpUtils.getRealPath(request, linkPath); SystemUtils.isNotExistCreate(realPath); String timeFileName = SystemUtils.timeFileName(file.getOriginalFilename()); try { file.transferTo(new File(realPath + "/" + timeFileName)); } catch (IOException e) { LOGGER.error("链接图片添加失败,错误:{}", e.getMessage()); return ImmutableMap.of("status", "0", "message", getMessage("link.uploaderror.message")); } return ImmutableMap.of( "status", "1", "message", getMessage("link.uploadsuccess.message"), "filePath", (linkPath + "/" + timeFileName), "contentPath", HttpUtils.getBasePath(request)); }
public static final State save(HttpServletRequest request, Map<String, Object> conf) { FileItemStream fileStream = null; boolean isAjaxUpload = request.getHeader("X_Requested_With") != null; if (!ServletFileUpload.isMultipartContent(request)) { return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT); } ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); if (isAjaxUpload) { upload.setHeaderEncoding("UTF-8"); } try { FileItemIterator iterator = upload.getItemIterator(request); while (iterator.hasNext()) { fileStream = iterator.next(); if (!fileStream.isFormField()) break; fileStream = null; } if (fileStream == null) { return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA); } String savePath = (String) conf.get("savePath"); String originFileName = fileStream.getName(); String suffix = FileType.getSuffixByFilename(originFileName); originFileName = originFileName.substring(0, originFileName.length() - suffix.length()); savePath = savePath + suffix; long maxSize = ((Long) conf.get("maxSize")).longValue(); if (!validType(suffix, (String[]) conf.get("allowFiles"))) { return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE); } savePath = PathFormat.parse(savePath, originFileName); String physicalPath = (String) conf.get("rootPath") + savePath; InputStream is = fileStream.openStream(); State storageState = StorageManager.saveFileByInputStream(is, physicalPath, maxSize); is.close(); if (storageState.isSuccess()) { storageState.putInfo("url", PathFormat.format(savePath)); storageState.putInfo("type", suffix); storageState.putInfo("original", originFileName + suffix); } return storageState; } catch (FileUploadException e) { return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR); } catch (IOException e) { } return new BaseState(false, AppInfo.IO_ERROR); }
private String getPath(String savePath, String filename, String suffix) { return PathFormat.parse(savePath + suffix, filename); }