List of usage examples for org.springframework.web.multipart.commons CommonsMultipartFile isEmpty
@Override public boolean isEmpty()
From source file:com.suntek.gztpb.controller.DriverLicenseController.java
@RequestMapping(value = "upload.htm", method = RequestMethod.POST) // public String handleFormUpload(HttpServletRequest request, HttpServletResponse response) throws IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; String inputName = request.getParameter("name"); CommonsMultipartFile mFile = (CommonsMultipartFile) multipartRequest.getFile(inputName); if (!mFile.isEmpty()) { String path = this.servletContext.getRealPath("/picUpload/"); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); String fileName = format.format(new Date()) + "_" + mFile.getOriginalFilename(); File fold = new File(path); if (!fold.exists()) { fold.mkdir();/*from w w w . j ava 2 s. co m*/ } path = path + "\\" + fileName; // ? File file = new File(path); // response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); try { mFile.getFileItem().write(file); // out.write("<script>parent.callback(1,'" + fileName + "','" + inputName + "')</script>"); } catch (Exception e) { logger.error(e.getMessage()); out.write("<script>parent.callback(0)</script>"); } } return null; }
From source file:com.msds.km.reparifacoty.controller.DrivingLicenseController.java
/** * ??/* w w w . j a v a2 s . c o m*/ * @param file * @return */ @RequestMapping(value = "/recongnize", method = RequestMethod.POST) @ResponseBody public BaseResponse recongnize(HttpServletRequest request) throws RecognitionException, IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file"); if (file != null && !file.isEmpty()) { try { DrivingLicense drivingLicense = drivingLicenseRecognitionServcie .recognition(saveFile(file, request)); if (drivingLicense != null) { // return new Result(businessCode, Result.RESULT_OK, drivingLicense); return returnResponse(SUCCESS_CODE, drivingLicense); } else { logger.info("can not recongnize the driving license"); // return new Result(businessCode, Result.RESULT_SYS_ERROR, "??"); return retEnumResponse(DrivingLicenseResponseEnum.code_1300); } } catch (RecognitionException e) { logger.error("driving license recongnize error", e); // return new Result(businessCode, Result.RESULT_SYS_ERROR, "?"); return retEnumResponse(DrivingLicenseResponseEnum.code_1301); } } else { // return new Result(businessCode, Result.RESULT_PARAMETER_INVALID,"??"); return retEnumResponse(DrivingLicenseResponseEnum.code_1302); } }
From source file:org.smigo.constraints.CommonsMultipartFileMimeTypeValidator.java
public boolean isValid(CommonsMultipartFile file, ConstraintValidatorContext constraintContext) { log.debug("Validating:" + file.getOriginalFilename() + " contenttype:" + file.getContentType() + " storagedescrip:" + file.getStorageDescription()); return file.isEmpty() || mimeTypes.contains(file.getContentType()); }
From source file:org.smigo.constraints.CommonsMultipartFileImageValidator.java
public boolean isValid(CommonsMultipartFile file, ConstraintValidatorContext constraintContext) { log.debug("Validating:" + file.getOriginalFilename() + " contenttype:" + file.getContentType() + " storagedescrip:" + file.getStorageDescription()); if (file.isEmpty()) return true; try {/*from w w w .j a v a 2 s . c o m*/ BufferedImage image = ImageIO.read(file.getInputStream()); if (image.getHeight() == height && image.getWidth() == width) return true; } catch (Exception e) { return false; } return false; }
From source file:org.openmrs.module.report.web.controller.report.ReportTypeController.java
@RequestMapping(method = RequestMethod.POST) public String onSubmit(@ModelAttribute("reportType") BirtReportType reportType, BindingResult bindingResult, HttpServletRequest request, SessionStatus status) { new BirtReportTypeValidator().validate(reportType, bindingResult); if (bindingResult.hasErrors()) { return "/module/report/report/reportType"; } else {// w w w . j a va2s.co m BirtReportService birtReportService = Context.getService(BirtReportService.class); BirtReportConfig config = ReportConstants.getConfig(); String tempPath = config.getRealPath(); if (StringUtils.isBlank(tempPath)) { Throwable th = new Throwable("Not exist realpath for save file report!"); } MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("reportFile"); if (file != null && file.getBytes() != null && file.isEmpty() == false) { long temp = new Date().getTime(); //String nameReport = org.openmrs.module.report.util.StringUtils.replaceSpecialWithUnderLineCharacter(reportType.getBirtReport().getName()); //String nameReportType = org.openmrs.module.report.util.StringUtils.replaceSpecialWithUnderLineCharacter(reportType.getName()); String fileName = reportType.getBirtReport().getId() + "_" + temp + ".rptdesign"; String reportFilename = tempPath + "/" + fileName; reportFilename = reportFilename.replaceAll("//", "/"); try { FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(reportFilename)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } reportType.setPath(fileName); } else if (reportType.getId() != null && reportType.getId().intValue() > 0) { reportType.setPath(birtReportService.getBirtReportTypeById(reportType.getId()).getPath()); } reportType.setCreatedBy(Context.getAuthenticatedUser().getGivenName()); reportType.setCreatedOn(new Date()); birtReportService.saveBirtReportType(reportType); status.setComplete(); return "redirect:/module/report/reportType.form?reportId=" + reportType.getBirtReport().getId(); } }
From source file:com.company.project.web.controller.FileUploadController.java
@RequestMapping(value = "/doUpload", method = RequestMethod.POST) public String handleFileUpload(HttpServletRequest request, @RequestParam CommonsMultipartFile[] fileUpload) throws Exception { if (fileUpload != null && fileUpload.length > 0) { for (CommonsMultipartFile aFile : fileUpload) { String fileName = null; if (!aFile.isEmpty()) { try { fileName = aFile.getOriginalFilename(); byte[] bytes = aFile.getBytes(); BufferedOutputStream buffStream = new BufferedOutputStream( new FileOutputStream(new File("D:/CX1/" + fileName))); buffStream.write(bytes); buffStream.close();/*w w w . ja v a 2 s .co m*/ System.out.println("You have successfully uploaded " + fileName); } catch (Exception e) { System.out.println("You failed to upload " + fileName + ": " + e.getMessage()); } } else { System.out.println("Unable to upload. File is empty."); } System.out.println("Saving file: " + aFile.getOriginalFilename()); aFile.getOriginalFilename(); aFile.getBytes(); } } return "admin"; }
From source file:com.company.project.web.controller.FileUploadController.java
@RequestMapping(value = "/asyncUpload", method = RequestMethod.POST/*, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE*/) @ResponseBody//from w w w. ja va 2 s . co m public ResponseEntity<String> asyncFileUpload(HttpServletRequest request, @RequestParam CommonsMultipartFile[] fileUpload) throws Exception { if (fileUpload != null && fileUpload.length > 0) { for (CommonsMultipartFile aFile : fileUpload) { String fileName = null; if (!aFile.isEmpty()) { try { fileName = aFile.getOriginalFilename(); byte[] bytes = aFile.getBytes(); BufferedOutputStream buffStream = new BufferedOutputStream( new FileOutputStream(new File("D:/CX1/" + fileName))); buffStream.write(bytes); buffStream.close(); System.out.println("You have successfully uploaded " + fileName); } catch (Exception e) { System.out.println("You failed to upload " + fileName + ": " + e.getMessage()); } } else { System.out.println("Unable to upload. File is empty."); } System.out.println("Saving file: " + aFile.getOriginalFilename()); aFile.getOriginalFilename(); aFile.getBytes(); } } return new ResponseEntity<>("success", HttpStatus.OK); }