[试题]Request之URL地址编码[试题]Request之URL地址编码
Request之URL地址编码
Request之URL地址编码
2010-10-15 09:33
当传的数据是通过Get方法传的时候,url的地址如果是中文就会出现乱码现象 这时就需要用URLEncoder.encode(fname, ecode)) 下面举个电影下载的例子,FileList列出下载的电影名称,当点击电影名称时 将名称通过Get方法传给RequestDemo2 然后实现下载。
首先在工程的webroot下面建立一个download文件夹,里面存放电影文件。
然后...
[试题]Request之URL地址编码
Request之URL地址编码
Request之URL地址编码
2010-10-15 09:33
当传的数据是通过Get方法传的时候,url的地址如果是中文就会出现乱码现象 这时就需要用URLEncoder.encode(fname, ecode)) 下面举个电影下载的例子,FileList列出下载的电影名称,当点击电影名称时 将名称通过Get方法传给RequestDemo2 然后实现下载。
首先在
的webroot下面建立一个download文件夹,里面存放电影文件。
然后建立一个servlet显示所有的电影:
public class FileList extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");
System.out.println("aaa");
String
realpath=this.getServletContext().getRealPath("/download");
System.out.println(realpath);
System.out.println("bbb");
File file=new File(realpath);
File []files=file.listFiles();
List list=Arrays.asList(files);
Iterator
it=list.iterator();
while(it.hasNext()){
File f=it.next();
response.getWriter().write(""+f.getName()+"\r\n");
System.out.println("ccc");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
//实现电影文件的下载servlet
public class ServletDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");
ServletContext context=this.getServletContext();
String name=request.getParameter("filename");
String fname=new
String(name.getBytes("iso8859-1"),"gb2312");
System.out.println("filename="+fname);
OutputStream out=response.getOutputStream();
response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(fname, "UTF-8"));
//1,得到与资源相关的流对象
InputStream
in=this.getServletContext().getResourceAsStream("/download/"+fname);
//2.通过流对象下载资源
byte [] b=new byte[1024];
int len=0;
//3.把读到的数据写给客户
while((len=in.read(b))>0){
out.write(b, 0, len);
System.out.println("hehehe");
}
//4,关闭输出流对象
out.close();
//5,浏览器打开
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
本文档为【[试题]Request之URL地址编码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。