/**
* 服务器获取图片下载保存(暂时不需要存储)
* @param imgList(图片集合类)
*/
public static List<?> serviceFileDownload(List<String> imgList){
/**
* 1.从前端获取****上传图片路径
* 2.通过路径从*******服务器下载图片到本地,
*
*/
// String fileUrl = "http://static.dingtalk.com/media/lADPBbCc1W.jpg";//测试用例
List<String> listImg = new ArrayList<String>();
if(imgList == null){
logger.error("imgList集合为空[{}]",JSON.toJSONString(listImg));
return null;
}
for(String img : imgList){
try{
URL url = new URL(img);
HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
int respones = httpUrl.getResponseCode();
if(respones == HttpURLConnection.HTTP_OK){
logger.info("从服务器下载图片调用成功返回参数为:[{}]",respones);
InputStream inputStram = httpUrl.getInputStream();
int fileLength = img.lastIndexOf("/")+1;
String courrenFileName = img.substring(fileLength);
String fileNameNew = courrenFileName.replaceAll("/", "");
inputStram.close();
listImg.add(fileUrlAddress);
}
httpUrl.disconnect();
}catch(Exception e){
e.printStackTrace();
}
}
return listImg;
}
java通过服务器下载文件(这是根据我自己当时业务所需写的,如有需要请自己借鉴拷贝必要部分)
最新推荐文章于 2022-08-19 16:10:46 发布