Created
December 18, 2017 01:35
-
-
Save TouiSoraHe/9baa56a676222939c5b893ab0a037818 to your computer and use it in GitHub Desktop.
下载网易云音乐里面的歌曲或者歌单的封面到本地
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedInputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
//下载网易云音乐里面歌曲或者歌单的封面到本地 | |
public class Cover { | |
public static String urlAddress = "http://music.163.com/#/user/home?id=123293344"; | |
public static String dirPath= "E:/封面"; | |
public static void main(String[] args) throws IOException { | |
String urltemp=""; | |
for (String string : urlAddress.split("/#")) { | |
urltemp+=string; | |
} | |
URL url = new URL(urltemp); | |
URLConnection conn = url.openConnection(); | |
BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); | |
byte[] buff = new byte[1024]; | |
int len = -1; | |
StringBuilder str = new StringBuilder(); | |
while((len=in.read(buff))!=-1) | |
{ | |
str.append(new String(buff, 0, len)); | |
} | |
int start=0; | |
int end = 0; | |
if(urlAddress.indexOf("/user")!=-1) | |
{ | |
start=str.indexOf("src=\"", str.indexOf("id=\"ava\""))+5; | |
end = str.indexOf("?", start); | |
} | |
else | |
{ | |
start=str.indexOf("src=\"", str.indexOf("u-cover"))+5; | |
end = str.indexOf("?", start); | |
} | |
String u = str.substring(start, end); | |
System.out.println(u); | |
url = new URL(u); | |
conn = url.openConnection(); | |
File dirFile = new File(dirPath); | |
if(!dirFile.exists()) | |
{ | |
dirFile.mkdirs(); | |
} | |
File imgFile = new File(dirFile.getPath()+File.separator+u.substring(u.lastIndexOf("/")+1, u.length())); | |
if(imgFile.exists()) | |
{ | |
imgFile.delete(); | |
} | |
Files.copy(conn.getInputStream(), Paths.get(imgFile.getPath())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment