接上:
1.com.download包
HttpDownloader类
package com.download;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.util.FileUtils;
public class HttpDownloader {
private URL url = null;
/**
* 根据URL下载文件,前提是这个文件是文本的,函数的返回值就是文件当中的内容
* 1.创建一个URL对象
* 2.通过URL对象,创建一个HttpURLConnection对象
* 3.得到InputStream
* 4.从InputStream得到数据
*/
public String download(String urlStr){
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try{
//创建一个url连接
url = new URL(urlStr);
//创建一个http连接
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
//使用IO读取数据
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while((line = buffer.readLine()) != null){
sb.append(line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 该函数返回整形,-1代表下载文件出错,0代表下载文件成功,1代表文件已经存在
*/
public int downFile(String urlStr, String path,String fileName){
InputStream inputStream = null;
try{
FileUtils fu = new FileUtils();
if(fu.isFileExist(fileName,path)){
return 1;
}else{
inputStream = getInputStreamFromUrl(urlStr);
System.out.println("test");
File resultFile = fu.write2SDFromInput(path, fileName, inputStream);
if(resultFile != null){
return -1;
}
}
}catch(Exception e){
e.printStackTrace();
}
return 0;
}
/**
* 根据URL得到输入流
*/
public InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException, IOException{
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
}
2.com.util包
FileUtils 类
package com.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import com.model.Mp3Info;
import android.os.Environment;
public class FileUtils {
private String SDCardRoot;
public FileUtils(){
//得到当前外部储存设备的目录,File.separator是文件分隔符,比如在window下是"\"
SDCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator;
}
/**
* 在SD卡上创建文件
* @param fileName
* @return
* @throws IOException
*/
public File createSDFile(String fileName,String dir) throws IOException{
File file = new File(SDCardRoot + dir + File.separator + fileName);
System.out.println("file-->"+file);
file.createNewFile();
return file;
}
/**
* 在SD卡上创建目录
*/
public File createSDDir(String dir){
File dirFile = new File(SDCardRoot + dir + File.separator);
System.out.println("create dir " + dirFile.mkdir());
return dirFile;
}
/**
* 判断SD卡上的文件夹是否存在
*/
public boolean isFileExist(String fileName,String path){
File file = new File(SDCardRoot + path +File.separator + fileName);
return file.exists();
}
/**
* 将一个InputSream里面的数据写入到SD卡上
*/
public File write2SDFromInput(String path,String fileName,InputStream input){
File file = null;
OutputStream output = null;
try{
createSDDir(path);
file = createSDFile(fileName , path );
output = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024];
int temp;
while((temp = (input.read(buffer))) != -1){
output.write(buffer ,0 ,temp);
}
output.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
output.close();
}catch(Exception e){
e.printStackTrace();
}
}
return file;
}
/**
* 读取目录中mp3文件的名字和大小
*/
public List<Mp3Info> getMp3Files(String path){
List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
//得到文件夹
File file = new File(SDCardRoot + File.separator + path);
//返回到当前文件夹得所有文件
File[] files = file.listFiles();
FileUtils fileUtils = new FileUtils();
for(int i = 0; i < files.length; i++){
if(files[i].getName().endsWith("mp3")){
Mp3Info mp3Info = new Mp3Info();
mp3Info.setMp3Name(files[i].getName());
mp3Info.setMp3Size(files[i].length() + "");
String temp [] = mp3Info.getMp3Name().split("\\.");
String eKrcName = temp[0] + ".Krc";
if(fileUtils.isFileExist(eKrcName, "/mp3")){
mp3Info.setKrcName(eKrcName);
}
mp3Infos.add(mp3Info);
}
}
return mp3Infos;
}
}
3.com.model包
Mp3Info类:
package com.model;
import java.io.Serializable;
public class Mp3Info implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String mp3Name;
private String mp3Size;
private String krcName;
private String krcSize;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMp3Name() {
return mp3Name;
}
public void setMp3Name(String mp3Name) {
this.mp3Name = mp3Name;
}
public String getMp3Size() {
return mp3Size;
}
public void setMp3Size(String mp3Size) {
this.mp3Size = mp3Size;
}
public String getKrcName() {
return krcName;
}
public void setKrcName(String krcName) {
this.krcName = krcName;
}
public String getKrcSize() {
return krcSize;
}
public void setKrcSize(String krcSize) {
this.krcSize = krcSize;
}
public Mp3Info(String id, String mp3Name, String mp3Size, String krcName,
String krcSize) {
super();
this.id = id;
this.mp3Name = mp3Name;
this.mp3Size = mp3Size;
this.krcName = krcName;
this.krcSize = krcSize;
}
public Mp3Info() {
super();
}
@Override
public String toString() {
return "Mp3Info [id=" + id + ", krcName=" + krcName + ", krcSize="
+ krcSize + ", mp3Name=" + mp3Name + ", mp3Size=" + mp3Size
+ "]";
}
}
4.com.xml包:
Mp3ListContenHandler 类:
package com.xml;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.model.Mp3Info;
public class Mp3ListContenHandler extends DefaultHandler{
private List<Mp3Info> infos=null;
private Mp3Info mp3Info=null;
private String tagName=null;
public Mp3ListContenHandler(List<Mp3Info> infos) {
super();
this.infos = infos;
}
public List<Mp3Info> getInfos() {
return infos;
}
public void setInfos(List<Mp3Info> infos) {
this.infos = infos;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String temp=new String(ch,start,length);
if(tagName.equals("id")){
mp3Info.setId(temp);
}else if(tagName.equals("mp3.name")){
mp3Info.setMp3Name(temp);
}else if(tagName.equals("mp3.size")){
mp3Info.setMp3Size(temp);
}else if(tagName.equals("krc.name")){
mp3Info.setKrcName(temp);
}else if(tagName.equals("krc.size")){
mp3Info.setKrcSize(temp);
}
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// System.out.println("!!!!!!!!!!!!!!!!!!!!++++++++++");
if(qName.equals("resource")){
infos.add(mp3Info);
}
tagName="";
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
this.tagName=localName;
if(tagName.equals("resource")){
mp3Info=new Mp3Info();
}
}
}
先到这里,待续。。。