如何使用Dropwizard Project中的Postman上传图像。

问题描述 投票:0回答:1

我正在使用Dropwizard框架进行Gradle项目,任何人都可以帮助我如何使用邮递员将图像上传到Dropwizard。

提前致谢

postgresql hibernate postman dropwizard
1个回答
0
投票

我认为你可以使用以下代码,可以使用邮递员上传图像

@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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.