@POST @Path("/run") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile(@FormDataParam("scriptfile") InputStream uploadedInputStream, @FormDataParam("scriptfile") FormDataContentDisposition fileDetail) { try { if (!otpServer.scriptingService.enableScriptingWebService) return Response.status(Status.FORBIDDEN) .entity("Scripting web-service is desactivated for security reasons.\n") .build(); String scriptContent = IOUtils.toString(uploadedInputStream, Charsets.UTF_8.name()); Object retval = otpServer.scriptingService.runScript(fileDetail.getFileName(), scriptContent); return Response.ok().entity(retval).build(); } catch (Throwable e) { return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.toString()) .type(MediaType.TEXT_PLAIN).build(); } }
/** * Sets the {@link MediaType} and fileEntity for this {@link FormDataBodyPart}. * * @param fileEntity the entity of this body part. * @param mediaType the media type. */ public void setFileEntity(final File fileEntity, final MediaType mediaType) { super.setMediaType(mediaType); super.setEntity(fileEntity); this.fileEntity = fileEntity; if (fileEntity != null) { FormDataContentDisposition.FormDataContentDispositionBuilder builder = FormDataContentDisposition.name(getName()); builder.fileName(fileEntity.getName()); if (fileEntity.exists()) { builder.size(fileEntity.length()); builder.modificationDate(new Date(fileEntity.lastModified())); } setFormDataContentDisposition(builder.build()); } }
public FormDataContentDisposition(HttpHeaderReader reader, boolean fileNameFix) throws ParseException { super(reader, fileNameFix); if (!"form-data".equalsIgnoreCase(getType())) { throw new IllegalArgumentException("The content disposition type is not equal to form-data"); } name = getParameters().get("name"); if (name == null) { throw new IllegalArgumentException("The name parameter is not present"); } }
String filename = new String(d.getFileName().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); long filesize = d.getSize();
@FormDataParam(value="file") FormDataContentDisposition fileDisposition; @FormDataParam("fileBodyPart") FormDataBodyPart body; @Path("/v1/source") @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces({ MediaType.APPLICATION_JSON}) @ApiOperation( value = "Create a new Source from an uploaded file.", response = Source.class ) public Response makeSource( @FormDataParam(value="file") InputStream inputStream ) { logger.info(fileDisposition.toString()); return makeSourceRaw(inputStream, fileDisposition.getFileName()); }
@POST @Consumes(MediaType.MULTIPART_FORM_DATA) public void upload(FormDataMultiPart formParams){ Map<String, List<FormDataBodyPart>> fieldsByName = formParams.getFields(); for (List<FormDataBodyPart> fields : fieldsByName.values()) { for (FormDataBodyPart field : fields){ // Check if fields are files(If any one knows better solution to check files[] please share your answers) if (StringUtils.equals("files", StringUtils.substringBefore(field.getName(), "["))) { //To read file content InputStream is = field.getEntityAs(InputStream.class); String fileName = field.getName(); field.getMediaType();//File mimeType //To get file details like size,file name,etc FormDataContentDisposition f=field.getFormDataContentDisposition(); System.out.println(f.getFileName()); Note: f.getType() not return the actual file type it returns mime type as form-data to get actual mime type use FormDataBodyPart media type like above. } get other form data like user name else if(StringUtils.equals(field.getName(),"userName")){ System.out.println(field.getValue()); } } } }
if (fileDetails != null && fileDetails.getSize() > (long) sizeLimit) { throw new IllegalArgumentException(String.format(TOO_BIG_ERROR_MESSAGE, sizeLimit)); int bufLen = (fileDetails != null && fileDetails.getSize() > 0) ? (int) fileDetails.getSize() : 64; ByteArrayOutputStream outStream = new ByteArrayOutputStream(bufLen);
@Override public FormDataContentDisposition build() { return new FormDataContentDisposition(type, name, fileName, creationDate, modificationDate, readDate, size); } }
/** * Constructor for the builder. * * @param type the disposition type. will be "form-data". * @param name the control name. * @param fileName the file name. * @param creationDate the creation date. * @param modificationDate the modification date. * @param readDate the read date. * @param size the size. * @throws IllegalArgumentException if the type is not equal to "form-data" * or the name is {@code null} */ protected FormDataContentDisposition(String type, String name, String fileName, Date creationDate, Date modificationDate, Date readDate, long size) { super(type, fileName, creationDate, modificationDate, readDate, size); this.name = name; if (!"form-data".equalsIgnoreCase(getType())) { throw new IllegalArgumentException("The content disposition type is not equal to form-data"); } if (name == null) { throw new IllegalArgumentException("The name parameter is not present"); } }
final String filename = fileDetail.getParameters().get("filename"); createStream(inputStream, mimeType, fileDetail.getSize()));
/** * Gets the control name. * * @return the control name. */ public String getName() { FormDataContentDisposition formDataContentDisposition = getFormDataContentDisposition(); if (formDataContentDisposition == null) { return null; } return formDataContentDisposition.getName(); }
@POST @Consumes({ MediaType.MULTIPART_FORM_DATA }) @Produces({ MediaType.APPLICATION_JSON }) public Response uploadStorageFile(@Context UriInfo ui, @Context HttpHeaders hh, @FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail){ System.out.println(uploadedInputStream.getClass().getName()); String uploadedFileLocation = fileDetail.getFileName(); long size = fileDetail.getSize(); // save it try { //test.jpg for test purposes OutputStream out = new FileOutputStream(new File("test.jpg")); IOUtils.copy(uploadedInputStream, out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String output = "File uploaded to : "; return Response.status(200).entity(output).build();
@Test(expected = IllegalArgumentException.class) public void testReadStreamSizeExceededMaliciousMetadata() throws IOException { when(mockFileDetails.getSize()).thenReturn(LIMIT_MATCH_LENGTH); RequestUtils.readData(new ByteArrayInputStream(LIMIT_EXCEED), mockFileDetails, SIZE_LIMIT); }
/** * Overrides the behaviour on {@link BodyPart} to ensure that * only instances of {@link FormDataContentDisposition} can be obtained. * * @return the content disposition. * @throws IllegalArgumentException if the content disposition header cannot be parsed. */ @Override public ContentDisposition getContentDisposition() { if (contentDisposition == null) { String scd = getHeaders().getFirst("Content-Disposition"); if (scd != null) { try { contentDisposition = new FormDataContentDisposition(scd, fileNameFix); } catch (ParseException ex) { throw new IllegalArgumentException(LocalizationMessages.ERROR_PARSING_CONTENT_DISPOSITION(scd), ex); } } } return contentDisposition; }
/** * Constructor for the builder. * * @param type the disposition type. will be "form-data". * @param name the control name. * @param fileName the file name. * @param creationDate the creation date. * @param modificationDate the modification date. * @param readDate the read date. * @param size the size. * @throws IllegalArgumentException if the type is not equal to "form-data" * or the name is {@code null} */ protected FormDataContentDisposition(String type, String name, String fileName, Date creationDate, Date modificationDate, Date readDate, long size) { super(type, fileName, creationDate, modificationDate, readDate, size); this.name = name; if (!"form-data".equalsIgnoreCase(getType())) { throw new IllegalArgumentException("The content disposition type is not equal to form-data"); } if (name == null) { throw new IllegalArgumentException("The name parameter is not present"); } }
@Path("/multipart") public class MultipartResource { @POST @Path("/upload2") @Consumes({MediaType.MULTIPART_FORM_DATA}) public Response uploadFileWithData( @FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition cdh, @FormDataParam("emp") Employee emp) throws Exception{ Image img = ImageIO.read(fileInputStream); JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img))); System.out.println(cdh.getName()); System.out.println(emp); return Response.ok("Cool Tools!").build(); } }
/** * Builds the body part content-disposition header which the specified * filename (or the default one if unspecified). * * @return ready to use content-disposition header. */ protected FormDataContentDisposition buildContentDisposition() { FormDataContentDisposition.FormDataContentDispositionBuilder builder = FormDataContentDisposition.name(getName()); if (filename != null) { builder.fileName(filename); } else { // Default is to set the name of the file as a form-field name. builder.fileName(getName()); } return builder.build(); }
@POST @Consumes(MediaType.MULTIPART_FORM_DATA) public Response post(@FormDataParam("file") InputStream stream, @FormDataParam("file") FormDataContentDisposition disposition) throws IOException { post(file, disposition.getFileName()); return Response.ok().build(); }
@POST @Path("/post/images") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response imageUpload(@FormDataParam("image") InputStream hereIsImage, @FormDataParam("image") FormDataContentDisposition hereIsName) { String path = System.getenv("HOME")+"/tmp/"; if(hereIsName.getSize()==0) { return Response.status(500).entity("image parameter is missing").build(); } String name = hereIsName.getFileName(); path += name; try { OutputStream out = new FileOutputStream(new File(name)); int read; byte[] bytes = new byte[1024]; while ((read = hereIsImage.read(bytes)) != -1) { out.write(bytes, 0, read); out.flush(); out.close(); } catch (IOException e) { return Response.status(500).entity(name + " was not uploaded\n"+e.getMessage()).build(); } return Response.status(200).entity(name + " was uploaded").build(); }
public FormDataContentDisposition(HttpHeaderReader reader, boolean fileNameFix) throws ParseException { super(reader, fileNameFix); if (!"form-data".equalsIgnoreCase(getType())) { throw new IllegalArgumentException("The content disposition type is not equal to form-data"); } name = getParameters().get("name"); if (name == null) { throw new IllegalArgumentException("The name parameter is not present"); } }