Q:

How to upload image using Postman in Dropwizard Project.?

I am working on a Gradle project with the Dropwizard framework can anyone help me how to upload an image using postman into the Dropwizard.
thanks in advance

A:

i think you can use the following code that can upload the image using postman
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response simpleUpload(@FormDataParam("file") InputStream uploadedInputStream,
                        @FormDataParam("file") FormDataContentDisposition fileDetail) {
 return Response.ok(saveTOFile(uploadedInputStream, fileDetail)).type(MediaType.TEXT_PLAIN_TYPE).build();
}

private String saveTOFile(InputStream uploadedInputStream, FormDataContentDisposition fileDetail) {
   final String UPLOAD_FOLDER="D://uploads/";
    String filelocation = UPLOAD_FOLDER + fileDetail.getFileName();
    File file = new File(filelocation);

    try {
        createFolderIfNotExists(UPLOAD_FOLDER);
    } catch (Exception e){
        Response.status(Response.Status.BAD_REQUEST).entity("could not create Folder").type(MediaType.TEXT_PLAIN_TYPE).build();
    }
    try {

        Files.copy(uploadedInputStream,file.toPath(),StandardCopyOption.REPLACE_EXISTING);

        OutputStream out=new FileOutputStream("D://"+fileDetail.getFileName());
        IOUtils.copyLarge(uploadedInputStream,out);
        IOUtils.closeQuietly(out);
        return "file copied to "+filelocation;
    } catch (IOException e) {
        e.printStackTrace();
        return "file did not copied";
    }
}
private void createFolderIfNotExists(String dirName)
        throws SecurityException {
    File theDir = new File(dirName);
    if (!theDir.exists()) {
        theDir.mkdir();
    }
}

