hhtp 上传图片--使用MultipartEntity

来源:互联网 发布:数据流量怎么打开不了 编辑:程序博客网 时间:2024/04/30 17:43
 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
原创粉丝点击