public void Test4() throws Exception {
//文件名称
String fileName = "234232332.zip";
//文件下载地址
String fileUrl = "https://example.com/path/to/your/file.zip";
//第三方请求地址
String remoteUrl = "https://www.baidu.com/?tn=sitemsnntp_1";
InputStream inputStream = null;
ByteArrayOutputStream bos = null;
try {
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
inputStream = connection.getInputStream();
bos = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != 1) {
bos.write(buffer, 0, bytesRead);
}
byte[] bytes = bos.toByteArray();
ByteArrayResource resource = new ByteArrayResource(bytes);
MultiValueMap body = new LinkedMultiValueMap<>();
body.add("fileName", fileName);
body.add("fileItem", resource);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// 第三方 需要token鉴权
headers.set("token", "XJIRNGSMDF");
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(body, headers);
ResponseEntity result = restTemplate.postForEntity(remoteUrl, requestEntity, String.class);
} catch (Exception e) {
System.out.println(e);
} finally {
if (inputStream != null) {
inputStream.close();
}
if (bos != null) {
bos.close();
}
}
}