Request之URL地址编码
2010-10-15 09:33
当传的数据是通过Get方法传的时候,url的地址如果是中文就会出现乱码现象 这时就需要用de(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 ServletContext().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<File> it=list.iterator();
while(it.hasNext()){
File ();
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");
url编码处理
request.setCharacterEncoding("gb2312");
ServletContext ServletContext();
String Parameter("filename");
String fname=new Bytes("iso8859-1"),"gb2312");
System.out.println("filename="+fname);
OutputStream OutputStream();
response.setHeader("Content-Disposition", "attachment; filename="+de(fname, "UTF-8"));
//1,得到与资源相关的流对象
InputStream ServletContext().getResourceAsStream("/download/"+fname);
//2.通过流对象下载资源
byte [] b=new byte[1024];
int len=0;
//3.把读到的数据写给客户
while((ad(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);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。