如何在 Dropwizard Project 中使用 Postman 上传图片?

How to upload image using Postman in Dropwizard Project.?

我正在使用 Dropwizard 框架开发一个 Gradle 项目,任何人都可以帮助我如何使用 postman 将图像上传到 Dropwizard。

提前致谢

我想你可以使用下面的代码,可以使用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();
    }
}