项目需求,向指定ip上传文件,刚开始http上传,没有任何问题,后来被调用方要求使用https调用,即向指定ip发送https请求。
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
/**
* @since 2021/11/30 11:20
*/
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception {
super();
SSLContext ctx = SSLContext.getInstance("SSL");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
};
ctx.init(null, new TrustManager[]{tm}, new SecureRandom());
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
public boolean upload(String fileid, int index, String cryptSliceHash, int cryptSliceSize, int offset, byte[] data, String ip) {
log.debug("开始上传");
StringBuilder url = new StringBuilder("https://")
.append(ip).append("/dc/upload/v1.0")
.append("?")
.append("fileId=").append(fileid).append("&")
.append("index=").append(index).append("&")
.append("cryptSliceHash=").append(cryptSliceHash).append("&")
.append("cryptSliceSize=").append(cryptSliceSize).append("&")
.append("offset=").append(offset);
log.debug("上传url=" + url.toString());
CloseableHttpClient httpclient = null;
try {
try {
httpclient = new SSLClient();
} catch (Exception e) {
e.printStackTrace();
throw new OSSClientException(e.getMessage());
}
HttpPut httpPut = new HttpPut(url.toString());
httpPut.setHeader("X-Auth-Token", this.authToken);
InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(data, offset, cryptSliceSize - offset),
cryptSliceSize - offset, ContentType.APPLICATION_OCTET_STREAM);
httpPut.setEntity(reqEntity);
try (CloseableHttpResponse response = httpclient.execute(httpPut)) {
if (response.getStatusLine().getStatusCode() == 200) {
final InputStream contentStream = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(contentStream);
byte[] c = new byte[1024 * 4];
int cnt = 0;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((cnt = bis.read(c)) > 0) {
out.write(c, 0, cnt);
}
out.flush();
final ResponseBody res = JSONUtil.toBean(out.toString(), ResponseBody.class);
log.debug("上传返回:" + JSONUtil.toJsonStr(res));
return res.getCode() == 200;
}
} catch (Exception e) {
e.printStackTrace();
throw new OSSClientException(e.getMessage());
}
} finally {
try {
httpclient.close();
} catch (IOException e) {
log.error("upload,关闭httpclient出错,", e);
}
}
return false;
}