• httpclient上传文件


    MobileTokenCloudClient.java

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.ndkey.im.product.mobiletoken;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.ndkey.security.SymmetricTicket;
    import com.ndkey.web.ResponseData;
    import java.io.File;
    import java.io.IOException;
    import org.apache.commons.io.FileUtils;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.springframework.web.multipart.MultipartFile;
    
    /**
     *
     * @author zxf
     */
    public class MobileTokenCloudClient {
    
        private final String BASE_URL = "http://mtc.ndkey.com/mtc/rest/app/1";
        private final static ObjectMapper _objectMapper = new ObjectMapper();
        private MobileTokenCloudSecurityService mobileTokenCloudSecurityService;
    
        public MobileTokenCloudClient() {
    
        }
    
        public ResponseData uploadLogo(String serviceId, MultipartFile file) throws IOException {
            try {
                CloseableHttpClient httpClient = HttpClients.createDefault();
                HttpPost httpPost = new HttpPost(BASE_URL + "/logo/update");
                SymmetricTicket ticket = mobileTokenCloudSecurityService.generateTicket();
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                File sigleFile = new File(file.getOriginalFilename());
                FileUtils.copyInputStreamToFile(file.getInputStream(), sigleFile);
                builder.addTextBody("serviceId", serviceId);
                builder.addPart("file", new FileBody(sigleFile));
                builder.addTextBody("signature", ticket.getSignature());
                builder.addTextBody("timestamp", Long.toString(ticket.getTimestamp()));
                builder.addTextBody("nonce", ticket.getNonce());
                HttpEntity httpEntity = builder.build();
                httpPost.setEntity(httpEntity);
                HttpResponse response = httpClient.execute(httpPost);
                return _objectMapper.readValue(response.getEntity().getContent(), ResponseData.class);
            } catch (Exception e) {
                return ResponseData.errorData(e.getLocalizedMessage());
            }
        }
    
        public ResponseData uploadApp(String platform, String version, MultipartFile file) throws IOException {
            try {
                CloseableHttpClient httpClient = HttpClients.createDefault();
                HttpPost httpPost = new HttpPost(BASE_URL + "/update");
                SymmetricTicket ticket = mobileTokenCloudSecurityService.generateTicket();
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                File sigleFile = new File(file.getOriginalFilename());
                FileUtils.copyInputStreamToFile(file.getInputStream(), sigleFile);
                builder.addTextBody("platform", platform);
                builder.addTextBody("version", version);
                builder.addPart("file", new FileBody(sigleFile));
                builder.addTextBody("signature", ticket.getSignature());
                builder.addTextBody("timestamp", Long.toString(ticket.getTimestamp()));
                builder.addTextBody("nonce", ticket.getNonce());
                HttpEntity httpEntity = builder.build();
                httpPost.setEntity(httpEntity);
                HttpResponse response = httpClient.execute(httpPost);
                return _objectMapper.readValue(response.getEntity().getContent(), ResponseData.class);
            } catch (Exception e) {
                return ResponseData.errorData(e.getLocalizedMessage());
            }
        }
        
        public void setMobileTokenCloudSecurityService(MobileTokenCloudSecurityService mobileTokenCloudSecurityService) {
            this.mobileTokenCloudSecurityService = mobileTokenCloudSecurityService;
        }
    
    }
  • 相关阅读:
    Java入门
    Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8
    pypy3.8安装
    asyncio执行阻塞代码
    linux安装go
    python消费rabbitmq
    绑定进程到指定cpu运行
    负载均衡算法
    django版本规划
    FastAPI WebSocket 简单演示
  • 原文地址:https://www.cnblogs.com/littlehoom/p/4725073.html
Copyright © 2020-2023  润新知