以迅雷不及掩耳响叮当之势 ,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<FileName.length; i++){ 21 ScannFile(path+"\\"+FileName[i]); 22 } 23 } 24 else{ 25 //然后对于path进行解析 我们知道路径的格式为T:\Hopg\ds 26 String PathName []=path.split("\\\\"); 27 String Path="" ; 28 for(int i=0 ;i<PathName.length-1;i++){ 29 Path+=PathName[i]+"\\"; 30 } 31 System.out.println("Path="+Path+"\n"+" "+PathName[PathName.length-1]); 32 } 33 34 } 35 36 public static void main(String args []){ 37 38 Scanner read = new Scanner(System.in); 39 while(read.hasNext()){ 40 String path =read.next(); 41 ScannFile(path); 42 } 43 } 44 }
部分结果显示:
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.krc 100 Path=D:\KuGou\Lyric\ 101 Big Sean、justin Bieber - As Long As You Love Me-a8d454a99b2cbd9aeca942b6bb127843.krc 102 Path=D:\KuGou\Lyric\ 103 bigbang - 谎言-40153e25b8761ee0df84e5bd2c333203.krc 104 Path=D:\KuGou\Lyric\ 105 bigbang - 谎言-a7c2d5efa5595a49543047619a0a43ae.krc 106 Path=D:\KuGou\Lyric\ 107 Brandy、Ray J - Another Day In Paradise-42f8ca27763371f48fd3d5800af8c95a.krc 108 Path=D:\KuGou\Lyric\ 109 Cs 战歌-40ce1e39e254925d2ea73c74683e7cec.krc 110 Path=D:\KuGou\Lyric\ 111 Cs-dc6cd4b0f7a6c464319d755f5affd0cd.krc 112 Path=D:\KuGou\Lyric\ 113 Eagles - 加州旅馆-102cab2133ac3eff9e95675a27c39ff7.krc 114 Path=D:\KuGou\Lyric\ 115 F.I.R. - 千军破 - 游戏 主题曲-41141dbeaaf1279e6412e2628ac38369.krc 116 Path=D:\KuGou\Lyric\ 117 Justin Bieber - Never Say Never-a9aa34e9991069ec0e84fcef1f710796.krc 118 Path=D:\KuGou\Lyric\ 119 Justin Bieber - One Time-f461bf7d62d0e3d5763bb5dcec15e8b6.krc 120 121 0d84e57621350fc9a75703f48371c669.kgtemp 122 Path=D:\KuGou\Temp\ 123 0e5caab4996a36048aaf70c8e78118d5.kgtemp 124 Path=D:\KuGou\Temp\ 125 0e6280489210d0e79a545e378b19537d.kgtemp 126 Path=D:\KuGou\Temp\ 127 102cab2133ac3eff9e95675a27c39ff7.kgtemp 128 Path=D:\KuGou\Temp\ 129 136f7f534e22e31acfef17bdf9f151a7.kgtemp 130 Path=D:\KuGou\Temp\ 131 153a8f5351a35ab5fb0242eeecaab9f6.kgtemp 132 Path=D:\KuGou\Temp\ 133 164b3e4d96ff8669b78c28a3e7f653f2.kgtemp 134 Path=D:\KuGou\Temp\ 135 169434aa609c10ce9d5ad214f4441f11.kgtemp 136 Path=D:\KuGou\Temp\ 137 19e56aa8033b1fa201177d89cb53c195.kgtemp 138 Path=D:\KuGou\Temp\ 139 1deba691aaad5063d2e14d68e7f5d36b.kgtemp 140 Path=D:\KuGou\Temp\ 141 1f90a59a49fb9bb3a2f9ed5c8643ad59.kgtemp 142 Path=D:\KuGou\Temp\ 143 25819fcfdfa3824a6123d543440b5687.kgtemp 144 Path=D:\KuGou\Temp\ 145 2a5c1d98b7a659d403c4302821af22a0.kgtemp 146 Path=D:\KuGou\Temp\ 147 2bbb2f71ee1079d5f182bc627ed716b4.kgtemp 148 Path=D:\KuGou\Temp\ 149 2be7047ff78e3c2d6f08d0168795896f.kgtemp 150 Path=D:\KuGou\Temp\ 151 2c77b621cd91d40785a668ef0acb4f2a.kgtemp 152 Path=D:\KuGou\Temp\ 153 2dfd540bd03d5c3fa87e71ee04e80f6f.kgtemp 154 Path=D:\KuGou\Temp\ 155 2e01f801090bf4481de825463f1f7e4e.kgtemp 156 Path=D:\KuGou\Temp\ 157 2ecffcc8f55c864d1c2ac75296ff6f2f.kgtemp 158 Path=D:\KuGou\Temp\ 159 30b474b415c15726f7969fa554d2dd8f.kgtemp 160 Path=D:\KuGou\Temp\ 161 35fd740e95cf3b8e7104534739d92361.kgtemp 162 Path=D:\KuGou\Temp\ 163 360939ec19b2f0cf17fdff2c41f0cd5b.kgtemp 164 Path=D:\KuGou\Temp\ 165 376e16c0a09e1d74988ce714202b98bf.kgtemp 166 Path=D:\KuGou\Temp\ 167 391462795935a52e5c0674c766279923.kgtemp 168 Path=D:\KuGou\Temp\ 169 3a57e9de928dd12fb04b6163e0dc9d97.kgtemp 170 Path=D:\KuGou\Temp\ 171 3c0f57cf3cc04db1f5c744059f3d192e.kgtemp 172 Path=D:\KuGou\Temp\ 173 3d307b751877ce9b4b73d30ae2869013.kgtemp 174 Path=D:\KuGou\Temp\ 175 3e33ca79e6966281c5b893c25064d94d.kgtemp 176 Path=D:\KuGou\Temp\ 177 4012fec0cdbd86248d2f1b3ad588fe89.kgtemp 178 Path=D:\KuGou\Temp\ 179 40ce1e39e254925d2ea73c74683e7cec.kgtemp 180 Path=D:\KuGou\Temp\ 181 41141dbeaaf1279e6412e2628ac38369.kgtemp 182 Path=D:\KuGou\Temp\ 183 4fa409eb849ca657ec2c8d6fa9aa4bcb.kgtemp 184 Path=D:\KuGou\Temp\ 185 52a25a4d1354ae2a2dade3ef20a167fa.kgtemp 186 Path=D:\KuGou\Temp\ 187 5579c6490823457c47ba96284c7cd347.kgtemp 188 Path=D:\KuGou\Temp\ 189 5602930f6d2a27f53913f72f80c3cb5b.kgtemp 190 Path=D:\KuGou\Temp\ 191 56ab6da7cff2de7d1fb7699f59806ed2.kgtemp 192 Path=D:\KuGou\Temp\ 193 56ed81ae949a86a55601a0c68c500954.kgtemp 194 Path=D:\KuGou\Temp\ 195 5770119ee402eb6321dadb7dad03a437.kgtemp 196 Path=D:\KuGou\Temp\ 197 5cf8d8822d5481ddb01181c8a5dc744f.kgtemp 198 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;i<FileName.length; i++){ 21 ScannFile(path+"\\"+FileName[i]); 22 } 23 } 24 else{ 25 //然后对于path进行解析 我们知道路径的格式为T:\Hopg\ds 26 String PathName []=path.split("\\\\"); 27 String Path="" ; 28 String tmpName=PathName[PathName.length-1]; 29 //按照我们想要的格式解析 30 // enum tmps{mp3 ,mp4, mp5,}; 31 //就简单的以mp3为类吧! 32 33 String tm [] =tmpName.split("\\."); 34 if(tm.length>0&&tm[tm.length-1].equalsIgnoreCase("mp3")){ 35 for(int i=0 ;i<PathName.length-1;i++){ 36 Path+=PathName[i]+"\\"; 37 } 38 System.out.println("Path="+Path+"\n"+" "+tmpName); 39 } 40 } 41 42 } 43 44 public static void main(String args []){ 45 46 Scanner read = new Scanner(System.in); 47 while(read.hasNext()){ 48 String path =read.next(); 49 ScannFile(path); 50 } 51 } 52 }
部分结果显示:
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 不潮不花钱 - 副本.mp3 11 Path=D:\KuGou\ 12 不潮不花钱.mp3 13 Path=D:\KuGou\ 14 东方神起 - Spinning.mp3 15 Path=D:\KuGou\ 16 东方神起 - 傻瓜.mp3 17 Path=D:\KuGou\ 18 久石让 - 千寻のワルツ - 纯音乐版.mp3 19 Path=D:\KuGou\ 20 久石让 - 梦の星空.mp3 21 Path=D:\KuGou\ 22 五月天 - 倔强.mp3 23 Path=D:\KuGou\ 24 五月天 - 忘词.mp3 25 Path=D:\KuGou\ 26 五月天 - 拥抱.mp3 27 Path=D:\KuGou\ 28 京田诚一 - 木漏れ日.mp3 29 Path=D:\KuGou\ 30 伍佰 - 突然的自我.mp3 31 Path=D:\KuGou\ 32 伪物语 白金ディスコ.mp3 33 Path=D:\KuGou\ 34 信乐团 - 海阔天空.mp3 35 Path=D:\KuGou\ 36 光良 - 第一次.mp3 37 Path=D:\KuGou\ 38 冷漠 - 好男人中国造.mp3 39 Path=D:\KuGou\ 40 凤凰传奇 - 飞天 - 游戏 龙门客栈 主题曲.mp3 41 Path=D:\KuGou\ 42 刀郎 - 冲动的惩罚.mp3 43 Path=D:\KuGou\ 44 刘若英 - 后来.mp3 45 Path=D:\KuGou\ 46 劲舞团、劲舞团 - Oh My Baby.mp3 47 Path=D:\KuGou\ 48 劲舞团、劲舞团、李孝利 - 2008版韩服新歌.mp3 49 Path=D:\KuGou\ 50 千与千寻 - 那个夏天.mp3 51 Path=D:\KuGou\ 52 卢巧音、王力宏 - 分手快乐.mp3 53 Path=D:\KuGou\ 54 后舍男生 - As Long As You Love Me.mp3 55 Path=D:\KuGou\ 56 周杰伦 - 最后的战役.mp3 57 Path=D:\KuGou\ 58 周杰伦 - 甜甜的.mp3 59 Path=D:\KuGou\ 60 周杰伦 - 简单爱.mp3 61 Path=D:\KuGou\ 62 周杰伦 - 给我一首歌的时间.mp3 63 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<Object> list=null; 96 list=sql.DataQuery(hql, null); 97 /*for(int i=0 ; i<list.size(); i++){ 98 System.out.println(((music)list.get(i)).getName()); 99 }*/ 100 request.setAttribute("File_List",list); 101 102 return mapping.findForward("ok"); 103 } 104 }
对于jsp文件页面,源码如下:
1 <div class ="nav_mid_right"> 2 <form action="" method="post"> 3 <input type="text" class="search_text"/> 4 </form> 5 </div> 6 </div><!--nav_mid结束--> 7 <div class="nav_right"></div> 8 </div><!--nav结束--> 9 10 <div class="ad" id="picBox" style="height:400px"> 11 <img src="images/ad.jpg" /> 12 </div> <!--ad结束--> 13 14 <div class="list_main"> 15 16 <div class="list_box"> 17 <% 18 List<music> lis = (List<music>) request.getAttribute("File_List"); 19 20 %> 21 <table cellpadding="0" cellspacing="0" width="100%" id="ul"> 22 <% 23 if(lis!=null){ 24 for(int i=0;i<lis.size() ;i++){ 25 %> 26 <tr height="10"></tr> 27 <tr height="25" title=<%=lis.get(i).getName()%> onmouseover="this.className = 'hover'" onmouseout="this.className = ''" onclick="play(false,this);sdnClick(12164)"> 28 <td align="center"></td> 29 <!-- 名次 --> 30 <td align="center"><%=lis.get(i).getNo()%></td> 31 32 <td class="t"><span><a href="forList.do?Gxjun=Main&FilePath=<%=lis.get(i).getPath()+"\\"+lis.get(i).getName()%>"><%=lis.get(i).getName()%></a></span></td> 33 <!-- 上榜天数 --> 34 <td align="center"><%=lis.get(i).getDate() %></td> 35 <!-- 昨天排行 --> 36 <td align="center">0</td> 37 <!-- 最高排行 --> 38 <td align="center"></td> 39 </tr> 40 <% } } %> 41 </table> 42 <div class="page"> 43 <a href="#">首页</a><a href="#"><</a><a href="#">1</a><a href="#">2</a><a href="#">3</a><a href="#">...</a><a href="#">></a><a href="#">末页</a> 44 </div> 45 </div> 46 <!--news_list结束--> 47 </div> 48 <!--list_box结束--> 49 </div> 50 <!--list_main结束-->
No3 .分页技术,之前已经有总结:
之前总结的的链接: http://www.cnblogs.com/gongxijun/p/4210250.html
但是这次是采取*.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 Administrator 18 * 过滤器 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 @Override 26 public void doFilter(ServletRequest arg0, ServletResponse arg1, 27 FilterChain arg2) throws IOException, ServletException { 28 // TODO Auto-generated method stub 29 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 @Override 37 public void init(FilterConfig arg0) throws ServletException { 38 // TODO Auto-generated method stub 39 40 } 41 42 }
将这个写成这样,然后去web.xml文件下配置一番就OK了!
大体是这样配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 3 <display-name /> 4 <servlet> 5 <servlet-name>action</servlet-name> 6 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 7 <init-param> 8 <param-name>config</param-name> 9 <param-value>/WEB-INF/struts-config.xml</param-value> 10 </init-param> 11 <init-param> 12 <param-name>debug</param-name> 13 <param-value>3</param-value> 14 </init-param> 15 <init-param> 16 <param-name>detail</param-name> 17 <param-value>3</param-value> 18 </init-param> 19 <load-on-startup>0</load-on-startup> 20 </servlet> 21 <filter> 22 <filter-name>InfoFilter</filter-name> 23 <filter-class>com.Hbut.Music.struts.Filter.InfoFilter</filter-class> 24 </filter> 25 26 <servlet-mapping> 27 <servlet-name>action</servlet-name> 28 <url-pattern>*.do</url-pattern> 29 </servlet-mapping> 30 <filter-mapping> 31 <filter-name>InfoFilter</filter-name> 32 <url-pattern>/*</url-pattern> 33 </filter-mapping> 34 <welcome-file-list> 35 <welcome-file>index.jsp</welcome-file> 36 </welcome-file-list> 37 </web-app>
关于server.xml文件的格式,改成这样就可了!
1 <!-- A "Connector" represents an endpoint by which requests are received 2 and responses are returned. Documentation at : 3 Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) 4 Java AJP Connector: /docs/config/ajp.html 5 APR (HTTP/AJP) Connector: /docs/apr.html 6 Define a non-SSL HTTP/1.1 Connector on port 8080 7 --> 8 <Connector URIEncoding="utf-8" 9 port="8080" protocol="HTTP/1.1" 10 connectionTimeout="20000" 11 redirectPort="8443" /> 12 <!-- A "Connector" using the shared thread pool-->