root.addElement("music")
.addElement("resname").addText(resname).getParent()
.addElement("name").addText(name);
//得到输出document的输出样式
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream(xmlrealpath),format);
// 将document写出,关流
writer.write(document);
writer.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Description : 对资源歌曲名重新命名
* @param:resname 将要命名的资源名称
* @param: rename 资源新名称
*/
public void setResname(String resname, String rename){
//得到存放音乐文件夹的绝对地址
String realpath = this.getServletContext().getRealPath("/resource/music");
File resfile = new File(realpath +"/"+ resname);
File refile = new File(realpath +"/"+ rename);
resfile.renameTo(refile);
}
/**
* Description : 获取资源的新命名
* @param:resname 资源名称
* @return:String 资源新名称
*/
public String getRename(String resname){
try {
//得到MD5算法的MessageDigest
MessageDigest dm = MessageDigest.getInstance("md5");
byte[] md5 = dm.digest(resname.getBytes());
//对用MD5处理后的数据进行计算,得到处理后的字符串
BASE64Encoder encoder = new BASE64Encoder();
String baset64 = encoder.encode(md5);
String name = baset64 + resname.substring(resname.lastIndexOf("."));
//将影响文件地址的字符替换
String rename = name.replace('/', '0').replace('\\', '0').replace(' ', '0');
return rename;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
/**
* Description : 获取xml文件中的信息
* @return:Map
*/
public Map
Map
String xmlrealpath = this.getServletContext().getRealPath("/resource/musiclist.xml");
//得到文件的document
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(new File(xmlrealpath));
} catch (DocumentException e) {
e.printStackTrace();
}
if (document == null){
System.out.println("没有得到xml文件
");
return null;
}else{
//System.out.println("得到xml文件!
");
}
//得到根节点
Element root = document.getRootElement();
List
for (int i = 0; i < musics.size(); i++){
//分别获取name节点,resname节点的文本信息
String name = (musics.get(i)).element("name").getText();
String resname = (musics.get(i)).element("resname").getText();
//将获取的信息存到musmap集合中
musmap.put(resname, name);
}
return musmap;
}
/**
* Description : 获取资源名称
* @return:String[] 存有资源名称的字符串数组
*/
public String[] getResNames() {
String[] resname