废话不多说上代码
public static void main(String[] args) {
String fileUrl ="图片地址";
String uploadUrl = "上传地址";
System.out.println(getUploadUrlByInputStream(fileUrl,uploadUrl));
System.out.println(getUploadUrlByByteArrayOutStream(fileUrl,uploadUrl));
}
public static String getUploadUrlByInputStream(String fileUrl, String hwObsUrl){
InputStream stream = getInputStreamByImgUrl(fileUrl);
if (stream == null) {
log.info("InputStream is null:{}",fileUrl);
return null;
}
long sysTime = System.currentTimeMillis();
String fxHwUrl = null;
try {
String fileName = getFileName(fileUrl);
String responseBody = postMultipartFile(hwObsUrl,fileName,stream);
if (responseBody == null) {
log.info("upload error api response is null:{}",fileUrl);
return null;
}
JSONObject obj = JSONObject.parseObject(responseBody);
fxHwUrl = obj.getJSONObject("data").getString("filePath");
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.info("----------上传耗时:{}", System.currentTimeMillis() - sysTime);
return fxHwUrl;
}
public static String getFileName(String fileUrl){
String tempUrl = fileUrl;
if(fileUrl.indexOf("?")>0){
tempUrl = fileUrl.substring(0,fileUrl.indexOf("?"));
}
return tempUrl.substring(tempUrl.lastIndexOf("/")+1);
}
public static String postMultipartFile(String url, String fileName, InputStream stream) {
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(StandardCharsets.UTF_8);
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", stream, ContentType.MULTIPART_FORM_DATA, fileName);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
return EntityUtils.toString(responseEntity, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
log.error("postMultipartFile error,url:{},ex:{}", url, e.getMessage());
}
return null;
}
public static InputStream getInputStreamByImgUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(2000);
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
return connection.getInputStream();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
public static String getUploadUrlByByteArrayOutStream(String fileUrl, String hwObsUrl){
ByteArrayOutputStream stream = getByteArrayOutStreamByImgUrl(fileUrl);
if (stream == null) {
log.info("ByteArrayOutputStream is null:{}",fileUrl);
return null;
}
long sysTime = System.currentTimeMillis();
String fxHwUrl = null;
try {
String fileName = getFileName(fileUrl);
HwObsUploadApiResponse responseBody = uploadToHwObs(hwObsUrl, stream, fileName);
if (responseBody == null) {
log.info("upload error api response is null:{}",fileUrl);
return null;
}
fxHwUrl = responseBody.getFilePath();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.info("----------上传耗时:{}", System.currentTimeMillis() - sysTime);
return fxHwUrl;
}
public static ByteArrayOutputStream getByteArrayOutStreamByImgUrl(String imageUrl) {
try {
InputStream inputStream = getInputStreamByImgUrl(imageUrl);
if (null != inputStream) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
return byteArrayOutputStream;
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}