Open leelance opened 4 years ago
pom.xml
<dependency> <groupId>org.jodd</groupId> <artifactId>jodd-http</artifactId> <version>5.0.13</version> </dependency>
@Slf4j public class ImageUploadUtilsTests { @Test public void upload() throws IOException { File file = ResourceUtils.getFile("classpath:20180822153319.jpg"); HttpRequest request = HttpRequest .post("http://127.0.0.1:8899/upload") .body(Files.toByteArray(file), suffex(file.getName())) ; HttpResponse response = request.send() .acceptEncoding(Charsets.UTF_8.name()) .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE); ImgResult result = JSON.parseObject(response.bodyText(), ImgResult.class); log.info("===>{}", result); } private static String suffex(String fileName) { return StringUtils.substringAfter(fileName, "."); } @Data static class ImgResult{ private boolean ret; private ImgError error; private ImgInfo info; } @Data static class ImgError{ private int code; private String message; } @Data static class ImgInfo{ private String md5; private long size; } }
返回值:
===>JSON: {"ret":true,"info":{"md5":"2a3d3a3ae4a6d8cce26f8e1b3e7a467d","size":1601925}} ===>ImgResult: ImageUploadUtils.ImgResult(ret=true, error=null, info=ImageUploadUtils.ImgInfo(md5=2a3d3a3ae4a6d8cce26f8e1b3e7a467d, size=1601925))
pom.xml
返回值: