hhtp 上传图片--使用MultipartEntity

 public static String uploadImage(String module, String function, CommonsMultipartFile file) throws Exception {
        DiskFileItem fileItem = (DiskFileItem) file.getFileItem();
        boolean needRemove = false;


        if (fileItem.isInMemory()) {
            logger.debug("HttpClientUtil.uploadImage: save small image to disk!");
            File f = fileItem.getStoreLocation();
            FileOutputStream fos = new FileOutputStream(f);
            fos.write(file.getBytes());
            fos.close();


            needRemove = true;
        }


        String fileName = file.getOriginalFilename();
        if (fileName == null)
            return null;


        if (!fileName.toLowerCase().matches(IMAGE_TYPE))
            return null;


        String targetURL = PropertiesUtil.getImageServerUrl() + "/" + module + "/" + function + "/uploadImage";
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;


        try {
            HttpPost httpPost = new HttpPost(targetURL);
            FileBody fileBody = new FileBody(fileItem.getStoreLocation());
            StringBody fileNameBody = new StringBody(fileName, ContentType.TEXT_PLAIN);


            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addPart("fileName", fileNameBody)
                    .addPart("file_0", fileBody)
                    .setCharset(Charset.forName("UTF-8"))
                    .build();



            httpPost.setEntity(reqEntity);


            httpClient = HttpClients.createDefault();
            response = httpClient.execute(httpPost);


            HttpEntity resEntity = response.getEntity();
            if (resEntity == null)
                return null;


            InputStream is = resEntity.getContent();
            DyResponse dyResponse = JSON.parseObject(is, Charset.forName("UTF-8"), DyResponse.class);


            if (dyResponse == null || dyResponse.getStatus() != DyResponse.OK || dyResponse.getData() == null)
                return null;


            List<Map<String, Object>> imageList = CastUtil.cast(dyResponse.getData());
            if (imageList == null || imageList.size() == 0)
                return null;


            return (String) imageList.get(0).get("fileurl");
        } catch (Exception ex) {
            logger.debug("HttpClientUtil.uploadImage: upload [ " + fileName + " ] failed!", ex);
        } finally {
            try {
                if (response != null)
                    response.close();
            } catch (Exception e) {
                logger.error("uploadImage", e);
            }


            try {
                if (httpClient != null)
                    httpClient.close();
            } catch (Exception e) {
                logger.error("uploadImage", e);
            }


            if (needRemove) {
                logger.info("HttpClientUtil.uploadImage: remove small image from disk!");
                File f = fileItem.getStoreLocation();
                f.delete();
            }
        }


        return null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值