play.mvc.Http.MultipartFormData Java Examples
The following examples show how to use
play.mvc.Http.MultipartFormData.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BadgeIssuerController.java From sunbird-lms-service with MIT License | 6 votes |
/** * This method will read from input data and put all the requested key and value inside a map. * * @param body MultipartFormData * @param map Map<String,Object> * @return Map<String,Object> * @throws IOException */ private Map<String, Object> readFormData(MultipartFormData body, Map<String, Object> map) throws IOException { Map<String, String[]> data = body.asFormUrlEncoded(); for (Entry<String, String[]> entry : data.entrySet()) { map.put(entry.getKey(), entry.getValue()[0]); } List<FilePart<Files.TemporaryFile>> filePart = body.getFiles(); if (filePart != null && !filePart.isEmpty()) { File f = filePart.get(0).getRef().path().toFile(); InputStream is = new FileInputStream(f); byte[] byteArray = IOUtils.toByteArray(is); map.put(JsonKey.FILE_NAME, filePart.get(0).getFilename()); map.put(JsonKey.IMAGE, byteArray); } return map; }
Example #2
Source File: ProfileController.java From htwplus with MIT License | 5 votes |
/** * Handles the upload of the temporary avatar image * * @param id * @return */ public Result createTempAvatar(Long id) { Account account = accountManager.findById(id); if (account == null) { return notFound(); } ObjectNode result = Json.newObject(); MultipartFormData body = request().body().asMultipartFormData(); if (body == null) { result.put("error", "No file attached"); return badRequest(result); } MultipartFormData.FilePart avatar = body.getFile("avatarimage"); if (avatar == null) { result.put("error", "No file with key 'avatarimage'"); return badRequest(result); } try { avatarManager.setTempAvatar(avatar, account.id); } catch (ValidationException e) { result.put("error", e.getMessage()); return badRequest(result); } result.put("success", getTempAvatar(id).toString()); return ok(result); }
Example #3
Source File: PackagesController.java From remote-monitoring-services-java with MIT License | 4 votes |
/** * Create a package from a multipart form post which expects * a "type" and a file uploaded with the name "package". * @return Returns the result in the form of {@link PackageApiModel} */ @Authorize("CreatePackages") public CompletionStage<Result> createAsync() throws BaseException, BadRequestException, IOException, ExecutionException, InterruptedException { final MultipartFormData formData = request().body().asMultipartFormData(); if (formData == null) { throw new BadRequestException("Multipart form-data is empty"); } /** * The form is sending multipart form/data content type. This is so that the form data can handle any possible * form input types that may come in. In this case it is just a single value for each field, but if we ever * included multiple file select for example then it could be more values. */ final Map<String, String[]> data = formData.asFormUrlEncoded(); if(!data.containsKey(PACKAGE_TYPE_PARAM) || ArrayUtils.isEmpty(data.get(PACKAGE_TYPE_PARAM)) || StringUtils.isEmpty(data.get(PACKAGE_TYPE_PARAM)[0])) { throw new BadRequestException(String.format("Package type not provided. Please specify %s " + "parameter", PACKAGE_TYPE_PARAM)); } final MultipartFormData.FilePart<File> file = formData.getFile(FILE_PARAM); if (file == null) { throw new BadRequestException(String.format("Package not provided. Please upload a file with " + "attribute name %s", FILE_PARAM)); } final String content = new String(Files.readAllBytes(file.getFile().toPath())); final String packageType = data.get(PACKAGE_TYPE_PARAM)[0]; String configType = data.get(PACKAGE_CONFIG_TYPE_PARAM)[0]; if (!(PackagesHelper.verifyPackageType(content, packageType))) { throw new BadRequestException(String.format("Package uploaded is invalid. Package contents" + " do not match with the given package type %s.", packageType.toString())); } if (packageType.equals(PackageType.edgeManifest.toString()) && !(StringUtils.isBlank(configType))) { throw new BadRequestException("Package of type EdgeManifest cannot have parameter " + "configType."); } if (configType == null) { configType = StringUtils.EMPTY; } final PackageApiModel input = new PackageApiModel(file.getFilename(), EnumUtils.getEnumIgnoreCase(PackageType.class, packageType), configType, content); return storage.addPackageAsync(input.ToServiceModel()).thenApplyAsync(m -> ok(toJson(new PackageApiModel(m)))); }