0

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

nikhil kumar
  • 55
  • 1
  • 10
  • Hi and welcome at SO. We will be more likely to help us if you can specify your problem in more detail, show us what you have tried and the specific problem that you ran into. You can read up on how to ask here:https://stackoverflow.com/help/how-to-ask – pandaadb Feb 11 '19 at 11:45

1 Answers1

0

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();
    }
}
Tech Learner
  • 16
  • 1
  • 5