private long calcMultipartFormDataBodyLength(final MultipartFormData<?> multipartFormData) { final String boundaryToContentTypeStart = MultipartFormatter.boundaryToContentType(""); final String boundary = getHeaders().get(HeaderNames.CONTENT_TYPE) .filter(ct -> ct.startsWith(boundaryToContentTypeStart)) .map(ct -> "\r\n--" + ct.substring(boundaryToContentTypeStart.length())) .orElseThrow(() -> new RuntimeException(("Content-Type header starting with \"" + boundaryToContentTypeStart + "\" needs to be present"))); long dataSizeSum = multipartFormData.asFormUrlEncoded().entrySet().stream().mapToLong(dataPart -> Arrays.stream(dataPart.getValue()).mapToLong(value -> partLength(boundary, "form-data", dataPart.getKey() + (dataPart.getValue().length > 1 ? "[]" : ""), null, null, value) ).sum()).sum(); long fileHeadersSizeSum = multipartFormData.getFiles().stream() .mapToLong(filePart -> // Pass empty body because we add the file size sum later instead anyway (see next assignment below) partLength(boundary, filePart.getDispositionType(), filePart.getKey(), filePart.getFilename(), filePart.getContentType(), "") ).sum(); long fileSizeSum = multipartFormData.getFiles().stream().mapToLong(filePart -> filePart.getFileSize()).sum(); long length = dataSizeSum + fileHeadersSizeSum + fileSizeSum; if(length > 0) { // Remove trailing "\r\n" from first boundary length -= 2; // Add last boundary with double dash (--) at the end length += (boundary + "--").getBytes(StandardCharsets.UTF_8).length; } return length; }
final String key = meta.getFilename(); final Upload upload = tm.upload(BUCKET_NAME, key, meta.getFile()); Logger.info("start upload " + meta.getFilename()); return asPromise(meta.getFilename(), upload).map(new Function<UploadResult, Result>() { @Override public Result apply(UploadResult uploadResult) throws Throwable { Logger.info("finished " + meta.getFilename()); return ok(asString(uploadResult));
private Result postFile(String sid) { // System.out.println("POST FILE CALLED: " + sid); Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); if (fileParts.size() <= 0) { // System.out.println("**** NO FILE ****"); return badRequest("No file uploaded"); } Http.MultipartFormData.FilePart uploadedFile = fileParts.get(0); String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); List<String> lines; try { lines = FileUtils.readLines(file); } catch (IOException e) { // System.out.println("**** COULD NOT READ FILE ****"); return badRequest("Could not read from uploaded file"); } knownSessionIDs.put(sid, lines); return ok("File uploaded: " + fileName + ", " + contentType + ", " + file); } }
private Result postFile(String sid) { // System.out.println("POST FILE CALLED: " + sid); Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); if (fileParts.size() <= 0) { // System.out.println("**** NO FILE ****"); return badRequest("No file uploaded"); } Http.MultipartFormData.FilePart uploadedFile = fileParts.get(0); String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); List<String> lines; try { lines = FileUtils.readLines(file); } catch (IOException e) { // System.out.println("**** COULD NOT READ FILE ****"); return badRequest("Could not read from uploaded file"); } knownSessionIDs.put(sid, lines); return ok("File uploaded: " + fileName + ", " + contentType + ", " + file); } }
private Result postFile(String sid) { // System.out.println("POST FILE CALLED: " + sid); Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); if (fileParts.size() <= 0) { // System.out.println("**** NO FILE ****"); return badRequest("No file uploaded"); } Http.MultipartFormData.FilePart uploadedFile = fileParts.get(0); String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); List<String> lines; try { lines = FileUtils.readLines(file); } catch (IOException e) { // System.out.println("**** COULD NOT READ FILE ****"); return badRequest("Could not read from uploaded file"); } knownSessionIDs.put(sid, lines); return ok("File uploaded: " + fileName + ", " + contentType + ", " + file); } }
private Result uploadFile() { Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); if (fileParts.size() <= 0) { return badRequest("No file uploaded"); } Http.MultipartFormData.FilePart uploadedFile = fileParts.get(0); String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); try { uploadedFileLines = FileUtils.readLines(file); } catch (IOException e) { return badRequest("Could not read from uploaded file"); } return ok("File uploaded: " + fileName + ", " + contentType + ", " + file); }
protected <A> Map<String, Http.MultipartFormData.FilePart<?>> resolveDuplicateFilePartKeys(final List<Http.MultipartFormData.FilePart<A>> fileParts) { final Map<String, List<Http.MultipartFormData.FilePart<?>>> resolvedDuplicateKeys = fileParts.stream() .collect(Collectors.toMap(filePart -> filePart.getKey(), filePart -> new ArrayList<>(Arrays.asList(filePart)), (a, b) -> { a.addAll(b); return a; })); final Map<String, Http.MultipartFormData.FilePart<?>> data = new HashMap<>(); resolvedDuplicateKeys.forEach((key, values) -> { if (key.endsWith("[]")) { String k = key.substring(0, key.length() - 2); for (int i = 0; i < values.size(); i++) { data.put(k + "[" + i + "]", values.get(i)); } } else if (!values.isEmpty()) { data.put(key, values.get(0)); } }); return data; }
protected <A> Map<String, Http.MultipartFormData.FilePart<?>> resolveDuplicateFilePartKeys(final List<Http.MultipartFormData.FilePart<A>> fileParts) { final Map<String, List<Http.MultipartFormData.FilePart<?>>> resolvedDuplicateKeys = fileParts.stream() .collect(Collectors.toMap(filePart -> filePart.getKey(), filePart -> new ArrayList<>(Arrays.asList(filePart)), (a, b) -> { a.addAll(b); return a; })); final Map<String, Http.MultipartFormData.FilePart<?>> data = new HashMap<>(); resolvedDuplicateKeys.forEach((key, values) -> { if (key.endsWith("[]")) { String k = key.substring(0, key.length() - 2); for (int i = 0; i < values.size(); i++) { data.put(k + "[" + i + "]", values.get(i)); } } else if (!values.isEmpty()) { data.put(key, values.get(0)); } }); return data; }
private Result uploadFile() { Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); if (fileParts.size() <= 0) { return badRequest("No file uploaded"); } Http.MultipartFormData.FilePart uploadedFile = fileParts.get(0); String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); try { uploadedFileLines = FileUtils.readLines(file); } catch (IOException e) { return badRequest("Could not read from uploaded file"); } return ok("File uploaded: " + fileName + ", " + contentType + ", " + file); }
private Result uploadFile() { Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); if (fileParts.size() <= 0) { return badRequest("No file uploaded"); } Http.MultipartFormData.FilePart uploadedFile = fileParts.get(0); String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); try { uploadedFileLines = FileUtils.readLines(file); } catch (IOException e) { return badRequest("Could not read from uploaded file"); } return ok("File uploaded: " + fileName + ", " + contentType + ", " + file); }
protected <A> Map<String, Http.MultipartFormData.FilePart<?>> resolveDuplicateFilePartKeys(final List<Http.MultipartFormData.FilePart<A>> fileParts) { final Map<String, List<Http.MultipartFormData.FilePart<?>>> resolvedDuplicateKeys = fileParts.stream() .collect(Collectors.toMap(filePart -> filePart.getKey(), filePart -> new ArrayList<>(Arrays.asList(filePart)), (a, b) -> { a.addAll(b); return a; })); final Map<String, Http.MultipartFormData.FilePart<?>> data = new HashMap<>(); resolvedDuplicateKeys.forEach((key, values) -> { if (key.endsWith("[]")) { String k = key.substring(0, key.length() - 2); for (int i = 0; i < values.size(); i++) { data.put(k + "[" + i + "]", values.get(i)); } } else if (!values.isEmpty()) { data.put(key, values.get(0)); } }); return data; }
Http.MultipartFormData body = request().body().asMultipartFormData(); List<Http.MultipartFormData.FilePart> fileParts = body.getFiles(); for (Http.MultipartFormData.FilePart filePart : fileParts) { if (filePart.getFilename().toLowerCase().endsWith(".png")) { String filename = filePart.getFilename(); Files.write(Paths.get(filename + ".png"), readContentIntoByteArray((File) filePart.getFile())); } else { return badRequest("Invalid request, only PNGs are allowed."); } }
Http.MultipartFormData.FilePart uploadedFile = body.getFile("file"); if (uploadedFile != null) { String fileName = uploadedFile.getFilename(); String contentType = uploadedFile.getContentType(); File file = uploadedFile.getFile(); file.renameTo(new File("D:/images/" +fileName));// it can be source File newFile = new File("D:/images/files/" + uploadedFile.getFilename()); java.nio.file.Files.move( file.toPath(),newFile.toPath(), StandardCopyOption.ATOMIC_MOVE); // org.apache.commons.io.FileUtils.moveFile(file, new File(path, fileName)); return ok("File is uploaded at source and copied the same file at destination"); }
Http.MultipartFormData body = request().body().asMultipartFormData(); for(Http.MultipartFormData.FilePart part : body.getFiles()){ Logger.debug(part.getFilename()); Logger.debug(part.getKey()); Logger.debug(part.getContentType()); Logger.debug(part.getFile().getName()); Logger.debug(part.getFile().getAbsolutePath()); Logger.debug(String.valueOf(part.getFile().getTotalSpace())); }
private long calcMultipartFormDataBodyLength(final MultipartFormData<?> multipartFormData) { final String boundaryToContentTypeStart = MultipartFormatter.boundaryToContentType(""); final String boundary = getHeaders().get(HeaderNames.CONTENT_TYPE) .filter(ct -> ct.startsWith(boundaryToContentTypeStart)) .map(ct -> "\r\n--" + ct.substring(boundaryToContentTypeStart.length())) .orElseThrow(() -> new RuntimeException(("Content-Type header starting with \"" + boundaryToContentTypeStart + "\" needs to be present"))); long dataSizeSum = multipartFormData.asFormUrlEncoded().entrySet().stream().mapToLong(dataPart -> Arrays.stream(dataPart.getValue()).mapToLong(value -> partLength(boundary, "form-data", dataPart.getKey() + (dataPart.getValue().length > 1 ? "[]" : ""), null, null, value) ).sum()).sum(); long fileHeadersSizeSum = multipartFormData.getFiles().stream() .mapToLong(filePart -> // Pass empty body because we add the file size sum later instead anyway (see next assignment below) partLength(boundary, filePart.getDispositionType(), filePart.getKey(), filePart.getFilename(), filePart.getContentType(), "") ).sum(); long fileSizeSum = multipartFormData.getFiles().stream().mapToLong(filePart -> filePart.getFileSize()).sum(); long length = dataSizeSum + fileHeadersSizeSum + fileSizeSum; if(length > 0) { // Remove trailing "\r\n" from first boundary length -= 2; // Add last boundary with double dash (--) at the end length += (boundary + "--").getBytes(StandardCharsets.UTF_8).length; } return length; }
private long calcMultipartFormDataBodyLength(final MultipartFormData<?> multipartFormData) { final String boundaryToContentTypeStart = MultipartFormatter.boundaryToContentType(""); final String boundary = getHeaders().get(HeaderNames.CONTENT_TYPE) .filter(ct -> ct.startsWith(boundaryToContentTypeStart)) .map(ct -> "\r\n--" + ct.substring(boundaryToContentTypeStart.length())) .orElseThrow(() -> new RuntimeException(("Content-Type header starting with \"" + boundaryToContentTypeStart + "\" needs to be present"))); long dataSizeSum = multipartFormData.asFormUrlEncoded().entrySet().stream().mapToLong(dataPart -> Arrays.stream(dataPart.getValue()).mapToLong(value -> partLength(boundary, "form-data", dataPart.getKey() + (dataPart.getValue().length > 1 ? "[]" : ""), null, null, value) ).sum()).sum(); long fileHeadersSizeSum = multipartFormData.getFiles().stream() .mapToLong(filePart -> // Pass empty body because we add the file size sum later instead anyway (see next assignment below) partLength(boundary, filePart.getDispositionType(), filePart.getKey(), filePart.getFilename(), filePart.getContentType(), "") ).sum(); long fileSizeSum = multipartFormData.getFiles().stream().mapToLong(filePart -> filePart.getFileSize()).sum(); long length = dataSizeSum + fileHeadersSizeSum + fileSizeSum; if(length > 0) { // Remove trailing "\r\n" from first boundary length -= 2; // Add last boundary with double dash (--) at the end length += (boundary + "--").getBytes(StandardCharsets.UTF_8).length; } return length; }
public static Source<ByteString, ?> transform(Source<? super Http.MultipartFormData.Part<Source<ByteString, ?>>, ?> parts, String boundary) { Source<MultipartFormData.Part<akka.stream.scaladsl.Source<ByteString, ?>>, ?> source = parts.map((part) -> { if (part instanceof Http.MultipartFormData.DataPart) { Http.MultipartFormData.DataPart dp = (Http.MultipartFormData.DataPart) part; return (MultipartFormData.Part) new MultipartFormData.DataPart(dp.getKey(), dp.getValue()); } else if (part instanceof Http.MultipartFormData.FilePart) { Http.MultipartFormData.FilePart fp = (Http.MultipartFormData.FilePart) part; if (fp.ref instanceof Source) { Source ref = (Source) fp.ref; Option<String> ct = Option.apply(fp.getContentType()); return (MultipartFormData.Part)new MultipartFormData.FilePart<akka.stream.scaladsl.Source<ByteString, ?>>(fp.getKey(), fp.getFilename(), ct, ref.asScala(), fp.getFileSize(), fp.getDispositionType()); } } throw new UnsupportedOperationException("Unsupported Part Class"); }); return source.via(Multipart.format(boundary, Charset.defaultCharset(), 4096)); }
public static Source<ByteString, ?> transform(Source<? super Http.MultipartFormData.Part<Source<ByteString, ?>>, ?> parts, String boundary) { Source<MultipartFormData.Part<akka.stream.scaladsl.Source<ByteString, ?>>, ?> source = parts.map((part) -> { if (part instanceof Http.MultipartFormData.DataPart) { Http.MultipartFormData.DataPart dp = (Http.MultipartFormData.DataPart) part; return (MultipartFormData.Part) new MultipartFormData.DataPart(dp.getKey(), dp.getValue()); } else if (part instanceof Http.MultipartFormData.FilePart) { Http.MultipartFormData.FilePart fp = (Http.MultipartFormData.FilePart) part; if (fp.ref instanceof Source) { Source ref = (Source) fp.ref; Option<String> ct = Option.apply(fp.getContentType()); return (MultipartFormData.Part)new MultipartFormData.FilePart<akka.stream.scaladsl.Source<ByteString, ?>>(fp.getKey(), fp.getFilename(), ct, ref.asScala(), fp.getFileSize(), fp.getDispositionType()); } } throw new UnsupportedOperationException("Unsupported Part Class"); }); return source.via(Multipart.format(boundary, Charset.defaultCharset(), 4096)); }
/** * Access a file part. * * @param key name of the file part to access * @return the file part specified by key */ public FilePart<A> getFile(String key) { for(FilePart filePart: getFiles()) { if(filePart.getKey().equals(key)) { return filePart; } } return null; } }
/** * Access a file part. * * @param key name of the file part to access * @return the file part specified by key */ public FilePart<A> getFile(String key) { for(FilePart filePart: getFiles()) { if(filePart.getKey().equals(key)) { return filePart; } } return null; } }