「java怎么爬取音乐」如何爬取音乐

博主:adminadmin 2022-12-15 13:36:10 61

今天给各位分享java怎么爬取音乐的知识,其中也会对如何爬取音乐进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java 怎么读取音乐文件

以随机读写方式打开一个MP3文件

将文件指针偏移量移动到文件长度倒数128位

判断标签头"TAG"是否存在

读取TAG_V1中的各个内容

关闭文件

TAG_V1(长度是固定的)其中包括的信息有:

标签头"TAG"    3字节

标题            30字节

作者            30字节

专辑            30字节

出品年份        4字节

备注信息        28字节

保留            1字节

音轨            1字节

类型            1字节

入一个MP3文件的TAG_V1部分信息:

byte buf = new byte[128];//初始化标签信息的byte数组

RandomAccessFile raf = new RandomAccessFile(mp3, "r");//随机读写方式打开MP3文件

raf.seek(raf.length() - 128);//移动到文件MP3末尾

raf.read(buf);//读取标签信息

raf.close();//关闭文件

if(buf.length != 128){//数据长度是否合法

throw new Exception("MP3标签信息数据长度不合法!");

}

if(!"TAG".equalsIgnoreCase(new String(buf,0,3))){//标签头是否存在

throw new Exception("MP3标签信息数据格式不正确!");

}

获得TAG_V1中的各个内容:

 String SongName = new String(buf,3,30,"utf-8").trim();//歌曲名称

  String Artist = new String(buf,33,30,"utf-8").trim();//歌手名字

  String Album = new String(buf,63,30,"utf-8").trim();//专辑名称

  String Year = new String(buf,93,4,"utf-8").trim();//出品年份

String Comment = new String(buf,97,28,"utf-8").trim();//备注信息

如何在javaweb项目中获取文件夹下的所有音频文件,并进行播放(有播放,暂停),请高手指教,

public class Test {

public static void main(String[] args) {

File file = new File("c:/我的文档/音乐");

File[] files = file.listFiles(new FilenameFilter() {

public boolean accept(File dir, String name) {

int i = name.lastIndexOf(".");

name = name.substring(i);

if (name.equalsIgnoreCase(".flv"))//根据格式自己判断

return true;

else

return false;

}

});

ListFile list =new ArrayListFile();

for (int i = 0; i files.length; i++) {

File f = new File(file.getAbsolutePath() + "/" + files[i].getName());

list.add(f);

System.out.println(f.getName());

}

}

}//播放器 自己找找看 有了file 就好办了吧

请问JAVA软件包里的音乐怎么提取??

你看看解压后的软件有没有DATA之类的文件夹,一般都放在里面的啊,以MID文件居多~

如果都没有的话那可能是处理过了,也可能只是触发你手机里的声音~

java如何获取.mp3格式文件内置歌曲封面

封面就是图片,用java的类抓取图片即可

package tool;

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.LinkedList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextField;

import javax.swing.JTextPane;

import javax.swing.SwingUtilities;

import javax.swing.text.Style;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyleContext;

import javax.swing.text.StyledDocument;

public class ImageViewer extends JFrame

{

private static final long serialVersionUID = 1L;

private static final String DOWNLOADPATH = "download/";

JTextPane textPane = new JTextPane ();

LinkedListString initString = new LinkedListString ();

LinkedListString initStyles = new LinkedListString ();

LinkedListString path = new LinkedListString ();

public ImageViewer ()

{

setTitle ("图片预览下载器 v1.0");

setLayout (new BorderLayout ());

setSize (500, 300);

setLocationRelativeTo (null);

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

}

private void addComponents ()

{

final JTextField urltField = new JTextField ();

JPanel right = new JPanel (new FlowLayout (FlowLayout.RIGHT, 0, 0));

final JButton go = new JButton ("GO");

textPane.setEditable (false);

JScrollPane content = new JScrollPane (textPane);

go.addActionListener (new ActionListener ()

{

@Override

public void actionPerformed ( ActionEvent e )

{

String url = urltField.getText ().trim ();

resolveHTML (url, "utf8", "(?i)\\img[^\\]*src[\\=\\s\'\"]+([^\\\'\"]+)[\'\"]?[^\\]*\\");

}

});

JPanel up = new JPanel (new BorderLayout ());

up.add (urltField, BorderLayout.CENTER);

right.add (go);

JButton download = new JButton ("DOWNLOAD");

download.addActionListener (new ActionListener ()

{

@Override

public void actionPerformed ( ActionEvent e )

{

downloadImages ();

}

});

right.add (download);

up.add (right, BorderLayout.EAST);

add (up, BorderLayout.NORTH);

add (content, BorderLayout.CENTER);

}

private void downloadImages ()

{

File fp = new File (DOWNLOADPATH);

if (!fp.exists ())

{

fp.mkdir ();

}

for ( int i = 0; i  path.size (); i++ )

{

try

{

String p = path.get (i);

URL url = new URL (p);

HttpURLConnection huc = (HttpURLConnection) url.openConnection ();

huc.setRequestMethod ("GET");

huc.setConnectTimeout (5 * 1000);

InputStream is = huc.getInputStream ();

ByteArrayOutputStream baos = new ByteArrayOutputStream ();

byte[] buffer = new byte[1024];

int len = -1;

while (( len = is.read (buffer) ) != -1)

{

baos.write (buffer, 0, len);

}

baos.flush ();

baos.close ();

is.close ();

huc.disconnect ();

byte[] data = baos.toByteArray ();

String name = p.substring (p.lastIndexOf ("/") + 1, p.length ());

name = name.contains (".") ? name : name + ".jpg";

FileOutputStream fos = new FileOutputStream (new File (DOWNLOADPATH + name));

fos.write (data);

fos.flush ();

fos.close ();

}

catch (Exception e)

{

continue;

}

}

}

private void loadImages ( ListString initString, ListString initStyles, ListString url )

{

try

{

StyledDocument doc = textPane.getStyledDocument ();

doc.remove (0, doc.getLength ());

addStylesToDocument (doc, url, initStyles);

for ( int i = 0; i  initString.size (); i++ )

{

doc.insertString (doc.getLength (), initString.get (i), doc.getStyle (initStyles.get (i)));

}

}

catch (Exception e)

{}

}

protected void addStylesToDocument ( StyledDocument doc, ListString url, ListString initStyles )

{

Style def = StyleContext.getDefaultStyleContext ().getStyle (StyleContext.DEFAULT_STYLE);

for ( int i = 0; i  initStyles.size (); i++ )

{

Style s = doc.addStyle (initStyles.get (i), def);

StyleConstants.setAlignment (s, StyleConstants.ALIGN_CENTER);

ImageIcon icon = createImageIcon (url.get (i));

if (icon != null)

{

StyleConstants.setIcon (s, icon);

}

}

}

protected static ImageIcon createImageIcon ( String url )

{

URL imgURL = null;

try

{

imgURL = new URL (url);

if (imgURL != null)

{

return new ImageIcon (imgURL);

}

}

catch (Exception e)

{}

return null;

}

private void resolveHTML ( String spec, String charsetName, String regex )

{

try

{

URL url = new URL (spec);

HttpURLConnection huc = (HttpURLConnection) url.openConnection ();

InputStreamReader isr = new InputStreamReader (huc.getInputStream (), charsetName);

BufferedReader br = new BufferedReader (isr);

StringBuilder builder = new StringBuilder ();

String line = null;

while (null != ( line = br.readLine () ))

{

builder.append (line);

}

br.close ();

isr.close ();

huc.disconnect ();

String bs = builder.toString ();

Pattern pattern = Pattern.compile (regex);

Matcher matcher = pattern.matcher (bs);

initString.clear ();

initStyles.clear ();

path.clear ();

while (matcher.find ())

{

String p = matcher.group (1);

initString.add (" ");

initStyles.add (p);

path.add (p);

}

loadImages (initString, initStyles, path);

}

catch (Exception e)

{

return;

}

}

public static void main ( String[] args )

{

SwingUtilities.invokeLater (new Runnable ()

{

@Override

public void run ()

{

ImageViewer tester = new ImageViewer ();

tester.addComponents ();

tester.setVisible (true);

}

});

}

}

用java编写网络爬虫,用来爬网络音乐资源,再返回java页面显示该怎么实现

下面是源代码,希望可以帮到你~~

package com.ly.mainprocess;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.Consts;

import org.apache.http.Header;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.StatusLine;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.cookie.Cookie;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

public class Test1 {

public static void main(String[] args){

Test1 test1 = new Test1();

System.out.println(test1.process("******","******"));

}

@SuppressWarnings("deprecation")

public boolean process(String username,String password) {

boolean ret=false;

DefaultHttpClient httpclient = new DefaultHttpClient();

try {

HttpGet httpget;

HttpResponse response;

HttpEntity entity;

ListCookie cookies;

//组建登录的post包

HttpPost httppost = new HttpPost(""); // 用户登录

ListNameValuePair nvps = new ArrayListNameValuePair();

nvps.add(new BasicNameValuePair("nickname", username));

nvps.add(new BasicNameValuePair("password", password));

nvps.add(new BasicNameValuePair("origURL", ""));

nvps.add(new BasicNameValuePair("loginregFrom", "index"));

nvps.add(new BasicNameValuePair("ss", "10101"));

httppost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

httppost.addHeader("Referer", "");

httppost.addHeader("Connection", "keep-alive");

httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");

httppost.addHeader("Accept-Language", "zh-CN,zh;q=0.8");

httppost.addHeader("Origin", "");

httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");

response = httpclient.execute(httppost);

entity = response.getEntity();

// System.out.println("Login form get: " + response.getStatusLine());

EntityUtils.consume(entity);

// System.out.println("Post logon cookies:");

cookies = httpclient.getCookieStore().getCookies();

if (cookies.isEmpty()) {

// System.out.println("None");

} else {

for (int i = 0; i cookies.size(); i++) {

// System.out.println("- " + cookies.get(i).toString());

}

}

//进行页面跳转

String url = ""; // 页面跳转

Header locationHeader = response.getFirstHeader("Location");

// System.out.println(locationHeader.getValue());

if (locationHeader != null) {

url = locationHeader.getValue(); // 得到跳转href

HttpGet httpget1 = new HttpGet(url);

response = httpclient.execute(httpget1);

// 登陆成功。。。hoho

}

entity = response.getEntity();

// System.out.println(response.getStatusLine());

if (entity != null) {

// System.out.println("Response content length: " + entity.getContentLength());

}

// 显示结果

BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));

String line = null;

while ((line = reader.readLine()) != null) {

// System.out.println(line);

}

//自动打卡

// 访问网站的子网页。

HttpPost httppost1 = new HttpPost(""); // 设置个人信息页面

httppost1.addHeader("Content-Type", "text/plain;charset=UTF-8");

httppost1.addHeader("Accept", "text/plain, */*");

httppost1.addHeader("X-Requested-With", "XMLHttpRequest");

httppost1.addHeader("Referer", "");

response = httpclient.execute(httppost1);

entity = response.getEntity();

// System.out.println(response.getStatusLine());

if(response.getStatusLine().toString().indexOf("HTTP/1.1 200 OK")=0){

ret = true;

}

if (entity != null) {

// System.out.println("Response content length: " + entity.getContentLength());

}

// 显示结果

reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));

line = null;

while ((line = reader.readLine()) != null) {

System.out.println(line);

}

} catch (Exception e) {

} finally {

httpclient.getConnectionManager().shutdown();

}

return ret;

}

}

能用java写出一个爬取音频和视频的爬虫吗

这个肯定是可以的,不过我只做过爬取文本和图片的。没做过爬视频和音频的,不过就我所知是可以的

关于java怎么爬取音乐和如何爬取音乐的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-15,除非注明,否则均为首码项目网原创文章,转载请注明出处。