以迅雷不及掩耳响叮当之势 ,biu~biu~! 搞完了一个老师的布置的结业课设!
下面总结一下遇到的阻力以及解决的策略:
No.1 文件扫描问题(对于一个未知或者说已知的目录下,扫描出我们想要的mp3格式的音乐文件,而过滤掉其他文件呢?)
处理的套路:
第一步,假如我们能实现这样一个低版本技术,对于一个已知的文件取名为Hopg,我们我们能扫描出Hopg里面所有的文件以及目录的名称:
1 package Hbut.Gxjun.Util; 2 3 import java.io.File; 4 import java.util.Scanner; 5 6 /** 7 * @author Gxjun 8 */ 9 public class Filer {10 11 12 static void ScannFile(String path){13 14 File Myfile = new File(path); //声明一个文件15 String FileName [] ;16 if(Myfile.isDirectory()){ 17 //判断该文件是否是一个目录,若果是目录18 //取出该目录下所有的文件 19 FileName=Myfile.list();20 for(int i=0;i
部分结果显示:
1 D:\KuGou 2 Path=D:\KuGou\ 3 Adele - Set Fire To The Rain.mp3 4 Path=D:\KuGou\ 5 Beast - ONE.mp3 6 Path=D:\KuGou\ 7 Beyond - 不再犹豫.mp3 8 Path=D:\KuGou\ 9 Beyond - 光辉岁月.mp3 10 Path=D:\KuGou\ 11 Beyond - 海阔天空.mp3 12 Path=D:\KuGou\ 13 Beyond、黄家驹 - 再见理想.mp3 14 Path=D:\KuGou\ 15 Big Sean、justin Bieber - As Long As You Love Me.mp3 16 Path=D:\KuGou\ 17 bigbang - 谎言.mp3 18 Path=D:\KuGou\ 19 By2、汪苏泷 - 有点甜 - 副本.mp3 20 Path=D:\KuGou\ 21 By2、汪苏泷 - 有点甜.mp3 22 Path=D:\KuGou\Cache\ 23 05f34c6687d33c237de18d8850644b5d.kg! 24 Path=D:\KuGou\Cache\ 25 307a9fcc3710db123faf128ef731becd.kg! 26 Path=D:\KuGou\Cache\ 27 350fe68306912b26c81de5b8bddbfe67.kg! 28 Path=D:\KuGou\Cache\ 29 3f8d84c738ca1101361c3095b9b299a9.kg! 30 Path=D:\KuGou\Cache\ 31 47aedcf2b436dd0ed74be3c7e8f0269c.kg! 32 Path=D:\KuGou\Cache\ 33 4a0261fc66f392469c6900dee046e4e0.kg! 34 Path=D:\KuGou\Cache\ 35 5100a94f545e4c78e2f9e901dfba3db9.kg! 36 Path=D:\KuGou\Cache\ 37 6b9766ec0bd6466bd9b2230424bfe954.kg! 38 Path=D:\KuGou\Cache\ 39 6d7a0adcd727cd09fc31c25e781c8fd0.kg! 40 Path=D:\KuGou\Cache\ 41 742e793c1a61ceefb75f4419ad90b3fc.kg! 42 Path=D:\KuGou\Cache\ 43 93a8251d3b11fde6fcab4490edf3445e.kg! 44 Path=D:\KuGou\Cache\ 45 c7a379cad7e71c3d0e17297f174a4c61.kg! 46 Path=D:\KuGou\Cache\ 47 c92e6c3c7cbb470c95d47dfd5b50aaf8.kg! 48 Path=D:\KuGou\Cache\ 49 d857528545b27773754de5993550d8d5.kg! 50 Path=D:\KuGou\ 51 Cleopatra Stratan - Numar Pan La Unu.mp3 52 Path=D:\KuGou\ 53 Crossfire、龚格尔 - 穿越火线.mp3 54 Path=D:\KuGou\ 55 Cs 战歌.mp3 56 Path=D:\KuGou\ 57 Cs.mp3 58 Path=D:\KuGou\ 59 Eagles - 加州旅馆.mp3 60 Path=D:\KuGou\ 61 Ein Kleines Lied - Candee!.mp3 62 Path=D:\KuGou\ 63 F4 - 流星雨.mp3 64 Path=D:\KuGou\ 65 Groove Coverage - On the Radio.mp3 66 Path=D:\KuGou\ 67 Groove Coverage - Wait(Radio Edit).mp3 68 Path=D:\KuGou\ 69 July - Story Of Us.mp3 70 Path=D:\KuGou\ 71 Justin Bieber - Home To Mama.mp3 72 Path=D:\KuGou\ 73 Leessang、白智英 - 回忆.mp3 74 Path=D:\KuGou\ 75 Lily Allen - Air Balloon.mp3 76 Path=D:\KuGou\ 77 Lily Allen - Fuck You.mp3 78 Path=D:\KuGou\ 79 Lily Allen - Fuckyou.mp3 80 Path=D:\KuGou\ 81 Lily Allen - Hard Out Here.mp3 82 Path=D:\KuGou\ 83 Lily Allen - Littlest Things.mp3 84 Path=D:\KuGou\ 85 Lily Allen - Not Fair.mp3 86 Path=D:\KuGou\ 87 Lily Allen - Somewhere Only We Know.mp3 88 Path=D:\KuGou\ 89 Lily Allen、p!nk - True Love.mp3 90 Path=D:\KuGou\Lyric\ 91 Adele - Hiding My Heart-5a85ae6773e09c1736460c8884b61287.krc 92 Path=D:\KuGou\Lyric\ 93 Apink - 小指(Promise U)-c1ae5c7d15104fc60101ded19839d31a.krc 94 Path=D:\KuGou\Lyric\ 95 Bebel Gilberto - All Around-6cdcbfd7d7fe404b8e1ea369c4c2a4dd.krc 96 Path=D:\KuGou\Lyric\ 97 Beyond - 不再犹豫-feb99489257c611ac4b6aa2a7c1ea761.krc 98 Path=D:\KuGou\Lyric\ 99 Beyond - 海阔天空-0278b3fcc9bdeae87298fd7e6eae8715.krc100 Path=D:\KuGou\Lyric\101 Big Sean、justin Bieber - As Long As You Love Me-a8d454a99b2cbd9aeca942b6bb127843.krc102 Path=D:\KuGou\Lyric\103 bigbang - 谎言-40153e25b8761ee0df84e5bd2c333203.krc104 Path=D:\KuGou\Lyric\105 bigbang - 谎言-a7c2d5efa5595a49543047619a0a43ae.krc106 Path=D:\KuGou\Lyric\107 Brandy、Ray J - Another Day In Paradise-42f8ca27763371f48fd3d5800af8c95a.krc108 Path=D:\KuGou\Lyric\109 Cs 战歌-40ce1e39e254925d2ea73c74683e7cec.krc110 Path=D:\KuGou\Lyric\111 Cs-dc6cd4b0f7a6c464319d755f5affd0cd.krc112 Path=D:\KuGou\Lyric\113 Eagles - 加州旅馆-102cab2133ac3eff9e95675a27c39ff7.krc114 Path=D:\KuGou\Lyric\115 F.I.R. - 千军破 - 游戏 主题曲-41141dbeaaf1279e6412e2628ac38369.krc116 Path=D:\KuGou\Lyric\117 Justin Bieber - Never Say Never-a9aa34e9991069ec0e84fcef1f710796.krc118 Path=D:\KuGou\Lyric\119 Justin Bieber - One Time-f461bf7d62d0e3d5763bb5dcec15e8b6.krc120 121 0d84e57621350fc9a75703f48371c669.kgtemp122 Path=D:\KuGou\Temp\123 0e5caab4996a36048aaf70c8e78118d5.kgtemp124 Path=D:\KuGou\Temp\125 0e6280489210d0e79a545e378b19537d.kgtemp126 Path=D:\KuGou\Temp\127 102cab2133ac3eff9e95675a27c39ff7.kgtemp128 Path=D:\KuGou\Temp\129 136f7f534e22e31acfef17bdf9f151a7.kgtemp130 Path=D:\KuGou\Temp\131 153a8f5351a35ab5fb0242eeecaab9f6.kgtemp132 Path=D:\KuGou\Temp\133 164b3e4d96ff8669b78c28a3e7f653f2.kgtemp134 Path=D:\KuGou\Temp\135 169434aa609c10ce9d5ad214f4441f11.kgtemp136 Path=D:\KuGou\Temp\137 19e56aa8033b1fa201177d89cb53c195.kgtemp138 Path=D:\KuGou\Temp\139 1deba691aaad5063d2e14d68e7f5d36b.kgtemp140 Path=D:\KuGou\Temp\141 1f90a59a49fb9bb3a2f9ed5c8643ad59.kgtemp142 Path=D:\KuGou\Temp\143 25819fcfdfa3824a6123d543440b5687.kgtemp144 Path=D:\KuGou\Temp\145 2a5c1d98b7a659d403c4302821af22a0.kgtemp146 Path=D:\KuGou\Temp\147 2bbb2f71ee1079d5f182bc627ed716b4.kgtemp148 Path=D:\KuGou\Temp\149 2be7047ff78e3c2d6f08d0168795896f.kgtemp150 Path=D:\KuGou\Temp\151 2c77b621cd91d40785a668ef0acb4f2a.kgtemp152 Path=D:\KuGou\Temp\153 2dfd540bd03d5c3fa87e71ee04e80f6f.kgtemp154 Path=D:\KuGou\Temp\155 2e01f801090bf4481de825463f1f7e4e.kgtemp156 Path=D:\KuGou\Temp\157 2ecffcc8f55c864d1c2ac75296ff6f2f.kgtemp158 Path=D:\KuGou\Temp\159 30b474b415c15726f7969fa554d2dd8f.kgtemp160 Path=D:\KuGou\Temp\161 35fd740e95cf3b8e7104534739d92361.kgtemp162 Path=D:\KuGou\Temp\163 360939ec19b2f0cf17fdff2c41f0cd5b.kgtemp164 Path=D:\KuGou\Temp\165 376e16c0a09e1d74988ce714202b98bf.kgtemp166 Path=D:\KuGou\Temp\167 391462795935a52e5c0674c766279923.kgtemp168 Path=D:\KuGou\Temp\169 3a57e9de928dd12fb04b6163e0dc9d97.kgtemp170 Path=D:\KuGou\Temp\171 3c0f57cf3cc04db1f5c744059f3d192e.kgtemp172 Path=D:\KuGou\Temp\173 3d307b751877ce9b4b73d30ae2869013.kgtemp174 Path=D:\KuGou\Temp\175 3e33ca79e6966281c5b893c25064d94d.kgtemp176 Path=D:\KuGou\Temp\177 4012fec0cdbd86248d2f1b3ad588fe89.kgtemp178 Path=D:\KuGou\Temp\179 40ce1e39e254925d2ea73c74683e7cec.kgtemp180 Path=D:\KuGou\Temp\181 41141dbeaaf1279e6412e2628ac38369.kgtemp182 Path=D:\KuGou\Temp\183 4fa409eb849ca657ec2c8d6fa9aa4bcb.kgtemp184 Path=D:\KuGou\Temp\185 52a25a4d1354ae2a2dade3ef20a167fa.kgtemp186 Path=D:\KuGou\Temp\187 5579c6490823457c47ba96284c7cd347.kgtemp188 Path=D:\KuGou\Temp\189 5602930f6d2a27f53913f72f80c3cb5b.kgtemp190 Path=D:\KuGou\Temp\191 56ab6da7cff2de7d1fb7699f59806ed2.kgtemp192 Path=D:\KuGou\Temp\193 56ed81ae949a86a55601a0c68c500954.kgtemp194 Path=D:\KuGou\Temp\195 5770119ee402eb6321dadb7dad03a437.kgtemp196 Path=D:\KuGou\Temp\197 5cf8d8822d5481ddb01181c8a5dc744f.kgtemp198 Path=D:\KuGou\Temp\199 65a18b12679b564071cccefd1c601663.kgtemp
从这些结果中,我们会惊喜的发现,我们遍历完了所有的目录,但是也有点不足的地方,就是将歌词和临时文件也加入了进来! 所以接下来我们需要解决的问题也变得单一起来!
--------如何剔除这些杂质,或者说过滤掉这些无用信息 ?
也许我们会发现,哟,不是要找格式嘛? 那就按照我们需要的格式找就ok了嘛! so ...... ! 问题就很容易搞定了! 于是立马修改代码:
1 package Hbut.Gxjun.Util; 2 3 import java.io.File; 4 import java.util.Scanner; 5 6 /** 7 * @author Gxjun 8 */ 9 public class Filer {10 11 12 static void ScannFile(String path){13 14 File Myfile = new File(path); //声明一个文件15 String FileName [] ;16 if(Myfile.isDirectory()){ 17 //判断该文件是否是一个目录,若果是目录18 //取出该目录下所有的文件 19 FileName=Myfile.list();20 for(int i=0;i0&&tm[tm.length-1].equalsIgnoreCase("mp3")){35 for(int i=0 ;i
部分结果显示:
1 Path=D:\KuGou\ 2 Serenata Immortale(游戏《最终幻想7》配乐.mp3 3 Path=D:\KuGou\ 4 T-ara - 不要忘记我.mp3 5 Path=D:\KuGou\ 6 Westlife - My Love - 西域男孩.mp3 7 Path=D:\KuGou\ 8 What Is Dancing.mp3 9 Path=D:\KuGou\10 不潮不花钱 - 副本.mp311 Path=D:\KuGou\12 不潮不花钱.mp313 Path=D:\KuGou\14 东方神起 - Spinning.mp315 Path=D:\KuGou\16 东方神起 - 傻瓜.mp317 Path=D:\KuGou\18 久石让 - 千寻のワルツ - 纯音乐版.mp319 Path=D:\KuGou\20 久石让 - 梦の星空.mp321 Path=D:\KuGou\22 五月天 - 倔强.mp323 Path=D:\KuGou\24 五月天 - 忘词.mp325 Path=D:\KuGou\26 五月天 - 拥抱.mp327 Path=D:\KuGou\28 京田诚一 - 木漏れ日.mp329 Path=D:\KuGou\30 伍佰 - 突然的自我.mp331 Path=D:\KuGou\32 伪物语 白金ディスコ.mp333 Path=D:\KuGou\34 信乐团 - 海阔天空.mp335 Path=D:\KuGou\36 光良 - 第一次.mp337 Path=D:\KuGou\38 冷漠 - 好男人中国造.mp339 Path=D:\KuGou\40 凤凰传奇 - 飞天 - 游戏 龙门客栈 主题曲.mp341 Path=D:\KuGou\42 刀郎 - 冲动的惩罚.mp343 Path=D:\KuGou\44 刘若英 - 后来.mp345 Path=D:\KuGou\46 劲舞团、劲舞团 - Oh My Baby.mp347 Path=D:\KuGou\48 劲舞团、劲舞团、李孝利 - 2008版韩服新歌.mp349 Path=D:\KuGou\50 千与千寻 - 那个夏天.mp351 Path=D:\KuGou\52 卢巧音、王力宏 - 分手快乐.mp353 Path=D:\KuGou\54 后舍男生 - As Long As You Love Me.mp355 Path=D:\KuGou\56 周杰伦 - 最后的战役.mp357 Path=D:\KuGou\58 周杰伦 - 甜甜的.mp359 Path=D:\KuGou\60 周杰伦 - 简单爱.mp361 Path=D:\KuGou\62 周杰伦 - 给我一首歌的时间.mp363 Path=D:\KuGou\64 周杰伦 - 阳光宅男.mp3
介于此,第一个问题得到了很好的解决! 然后可以结合数据库什么的就可以去做很多的事情了。
No 2 . 如何实现下载问题 ? 也就是将服务器的文件,下载到客户端。 首先分析,服务器和客户端所在的区域不同,对于服务器而言可能是大型或者说是分布式的服务器组件
其结构为:
而且,当我们需要保护自己的资源,就是外界可以下载资源,却无法访问资源所在位置。这样我们运用分派action可以实现,具体的代码如下:
/*关于xml的配置,遵循struts.conf配置文件 */
1 /* 2 * Generated by MyEclipse Struts 3 * Template path: templates/java/JavaClass.vtl 4 */ 5 package com.Hbut.Music.struts.action; 6 7 import java.io.File; 8 import java.io.FileInputStream; 9 import java.io.FileNotFoundException; 10 import java.io.FileOutputStream; 11 import java.io.IOException; 12 import java.io.InputStream; 13 import java.io.UnsupportedEncodingException; 14 import java.util.List; 15 16 import javax.servlet.ServletOutputStream; 17 import javax.servlet.http.HttpServletRequest; 18 import javax.servlet.http.HttpServletResponse; 19 import org.apache.struts.action.ActionForm; 20 import org.apache.struts.action.ActionForward; 21 import org.apache.struts.action.ActionMapping; 22 import org.apache.struts.actions.DispatchAction; 23 24 import com.Hbut.Music.Util.SQLHelp; 25 26 /** 27 * MyEclipse Struts 28 * Creation date: 06-01-2015 29 * 30 * XDoclet definition: 31 * @struts.action parameter="Gxjun" validate="true" 32 * @struts.action-forward name="err" path="/404_2.jsp" 33 * @struts.action-forward name="ok" path="/WEB-INF/list.jsp" 34 */ 35 public class ForListAction extends DispatchAction { 36 /* 37 * Generated Methods 38 */ 39 40 /** 41 * Method execute 42 * @param mapping 43 * @param form 44 * @param request 45 * @param response 46 * @return ActionForward 47 */ 48 public ActionForward Main(ActionMapping mapping, ActionForm form, 49 HttpServletRequest request, HttpServletResponse response) { 50 // TODO Auto-generated method stub 51 try { 52 request.setCharacterEncoding("utf-8"); 53 response.setCharacterEncoding("utf-8"); 54 } catch (UnsupportedEncodingException e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 57 } 58 String req = (String)request.getParameter("FilePath"); 59 System.out.println(req); 60 FileInputStream fin=null; 61 try { 62 fin= new FileInputStream(new File(req)); 63 } catch (FileNotFoundException e) { 64 // TODO Auto-generated catch block 65 //e.printStackTrace(); 66 } 67 //设置输出格式 68 response.reset(); 69 response.setContentType("MP3"); 70 String []ss=req.split("\\\\"); 71 response.addHeader("Content-Disposition", "attachment; filename=\"" +ss[ss.length-1]); 72 byte[] str = new byte[1024]; 73 int len =0; 74 try { 75 ServletOutputStream opu =response.getOutputStream(); 76 while((len=fin.read(str))>0){ 77 opu.write(str, 0, len); 78 } 79 } catch (IOException e) { 80 // TODO Auto-generated catch block 81 e.printStackTrace(); 82 }finally{ 83 84 try { 85 if(fin!=null) fin.close(); 86 87 } catch (IOException e) { 88 // TODO Auto-generated catch block 89 e.printStackTrace(); 90 } 91 } 92 93 SQLHelp sql = new SQLHelp(); 94 String hql =" from music"; 95 List
对于jsp文件页面,源码如下:
16 7 8 9 1011 1213 1415 1648 49 5017 <% 18 List46 47lis = (List ) request.getAttribute("File_List");19 20 %>21 26 22 <%23 if(lis!=null){24 for(int i=0;i
42 4527 οnmοuseοver="this.className = 'hover'" οnmοuseοut="this.className = ''" οnclick="play(false,this);sdnClick(12164)">28 40 <% } } %>4129 30 <%=lis.get(i).getNo()%> 31 32"><%=lis.get(i).getName()%> 33 34<%=lis.get(i).getDate() %> 35 360 37 3839
No3 .分页技术,之前已经有总结:
之前总结的的链接:
但是这次是采取*.do?gxjun=***&par=12 ; 这种get方式传输....
所以,在编写过程原道过乱码,被折腾的要死要活,死去活来之后,最后在网上搜到了一篇机智网友的做法,加了过滤器Filter文件之后(当然现在才知道,这货只对
post传输有效,对于get并没有卵用。 然后就去改了下tomcat 下面的 server.xml 将其编码格式改为utf-8)
关于Filter 文件 :
1 /** 2 * 3 */ 4 package com.Hbut.Music.struts.Filter; 5 6 import java.io.IOException; 7 8 import javax.servlet.Filter; 9 import javax.servlet.FilterChain;10 import javax.servlet.FilterConfig;11 import javax.servlet.ServletException;12 import javax.servlet.ServletRequest;13 import javax.servlet.ServletResponse;14 import javax.servlet.http.HttpServlet;15 16 /**17 * @author Administrator18 * 过滤器19 */20 public class InfoFilter extends HttpServlet implements Filter {21 22 /* (non-Javadoc)23 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)24 */25 @Override26 public void doFilter(ServletRequest arg0, ServletResponse arg1,27 FilterChain arg2) throws IOException, ServletException {28 // TODO Auto-generated method stub29 arg0.setCharacterEncoding("utf-8");30 arg2.doFilter(arg0, arg1);31 }32 33 /* (non-Javadoc)34 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)35 */36 @Override37 public void init(FilterConfig arg0) throws ServletException {38 // TODO Auto-generated method stub39 40 }41 42 }
将这个写成这样,然后去web.xml文件下配置一番就OK了!
大体是这样配置:
1 23 4 5 21action 6org.apache.struts.action.ActionServlet 78 11config 9/WEB-INF/struts-config.xml 1012 15debug 133 1416 19detail 173 180 2022 25 26InfoFilter 23com.Hbut.Music.struts.Filter.InfoFilter 2427 30action 28*.do 2931 34InfoFilter 32/* 3335 37index.jsp 36
关于server.xml文件的格式,改成这样就可了!
1 812