功能描述:上传文件到文件服务器,返回文件在服务器地址,下载(获取)文件直接可以在浏览器拼接地址(ip:端口/返回文件在服务器地址)
一、feign上传下载
引入maven依赖
org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-openfeign
1、上传接口
@FeignClient(name = "fileUploadAPI", url = "${file.uploadPath}")
public interface FileUploadAPI {
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String uploadImage(@RequestPart(value = "img") MultipartFile img);
}
2、下载接口
@FeignClient(name = "FileDownAPI",url = "${file.downPath}")
public interface FileDownAPI {
@GetMapping(value = "{path}")
Response getImage(@PathVariable("path") String path);
}
下载文件api调用
private void downImage(String path, String parentFile) {
Response response = fileDownAPI.getImage(path);
Response.Body body = response.body();
try {
InputStream inputStream = body.asInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
//路劲类似格式:/images/20220719/6204bc93415ec044c50f60ad5bcc132c.png
String[] split = path.split("\/");
if (split.length == 4) {
File img = new File(parentFile + "/" + split[3]);
if (!img.exists()) {
img.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(img);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int length = 0;
byte[] bytes = new byte[1024 * 10];
while ((length = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, length);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
bufferedInputStream.close();
inputStream.close();
} else {
throw new RuntimeException("图片路径格式不对,必须为:***/**.jpg 格式");
}
} catch (IOException e) {
logger.error(e.getMessage());
throw new RuntimeException("获取文件失败", e);
}
}
这里之所以写两个接口是因为如果上传地址和下载地址不一致(端口开始就不一样)的情况
二、httpclient上传
1、http协议文件上传
private static String inputFileByHttp(String uploadUrl) throws IOException {
// uploadUrl = "http://192.168.0.1:8080/images";
String localFile = "docs/test.txt";
File file = new File(localFile);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(uploadUrl);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
//这两个都可以
// multipartEntityBuilder.addBinaryBody("img",fileInputStream,ContentType.MULTIPART_FORM_DATA,"test.txt");
multipartEntityBuilder.addBinaryBody("img", file);
HttpEntity httpEntity = multipartEntityBuilder.build();
post.setEntity(httpEntity);
CloseableHttpResponse response = httpClient.execute(post);
String result = "无数据返回";
if (200 == response.getStatusLine().getStatusCode()) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
System.out.println(result);
}
return result;
}
2、https协议文件上传
private static String inputFileByHttps(String uploadUrl) throws IOException {
// uploadUrl = "https://baidu.xx.com:10000/images";
String localFile = "docs/test.txt";
File file = new File(localFile);
FileInputStream fileInputStream = new FileInputStream(file);
CloseableHttpClient httpClient = createSSLClient();
HttpPost post = new HttpPost(uploadUrl);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
//这两个都可以
multipartEntityBuilder.addBinaryBody("img",fileInputStream,ContentType.MULTIPART_FORM_DATA,"test.txt");
// multipartEntityBuilder.addBinaryBody("img", file);
HttpEntity httpEntity = multipartEntityBuilder.build();
post.setEntity(httpEntity);
CloseableHttpResponse response = httpClient.execute(post);
String result = "无数据返回";
if (200 == response.getStatusLine().getStatusCode()) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
System.out.println(result);
}
return result;
}
private static CloseableHttpClient createSSLClient() {
SSLContext sslContext;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
@Override
public boolean isTrusted(X509Certificate[] xcs, String string) {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyStoreException ex) {
ex.printStackTrace();
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
} catch (KeyManagementException ex) {
ex.printStackTrace();
}
return HttpClients.createDefault();
}
http和https综上代码可以看到在获取httpclient的时候有区别,https需要设置ssl信任
三、postman上面测试https协议文件上传设置
文件key必须要正确
关闭settings设置里面的ssl证书验证,一般默认是开启的
开启settings设置里面的CA证书,默认是关闭的



