这里以从服务器上下载图片为例,photoUrl地址,filePath文件存放路径,fileName下载之后存放的文件名称(这里采用服务器上的图片名称),method请求方式。
话不多说直接上代码。
public static void main(String[] args)
{
String photoUrl = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"; //获取最后一个分隔符后面的图片名称
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
System.out.println("fileName----"+fileName);
String filePath = "d:/photo";
File file = saveUrlAs(photoUrl, filePath , fileName,"GET");
System.out.println("filePath ----"+file);
}
public static File saveUrlAs((String url,String filePath,String fileName,String method){
//创建不同的文件夹目录
File file=new File(filePath);