tomcat下载文件时,URL含有文字参数与空格参数出错(java.io.FileNotFoundException错误) 解决方法(转)
2012-08-21 13:48
1451 查看
像这个例子 URL="http://10.0.2.2:8080/AndroidServer/Music/陈奕迅 - 浮夸.mp3 "这样的URL就无法通过,所以在“陈奕迅 - 浮夸.mp3”加上URL之前要经过一些处理。
一般情况下,URL参数为文字和空格都会出现乱码,需经过编码后才能执行通过。
下面介绍解决方法:
1.修改tomcat里面的/conf/server.xml文件 找到<Connector port="8080" 节点。在里面
加入URIEncoding="UTF-8" 属性编码 默认编码为"ISO-8859-1"一般情况下是不支持文字参数。
修改后如下:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"/>
2.参数处理,URL="http://10.0.2.2:8080/AndroidServer/Music/陈奕迅 - 浮夸.mp3 "
在参数 陈奕迅 - 浮夸.mp3 加入URL之前先进行处理,先对其进行编码
例如:String mm="陈奕迅 - 浮夸.mp3";
String ss=URLEncoder.encode(mm, "UTF-8"); //这里是讲String编码成UTF-8
System.out.println("ss="+ss);
//输出结果 ss=%E9%99%88%E5%A5%95%E8%BF%85%+-+E6%B5%AE%E5%A4%B8.mp3
//其中空格被编码成 + ;这样编码后空格编码还是有问题,需在处理,下面继续
ss=ss.replaceAll("\\+","%20"); //因为 + 符号在java是关键字符需要转义,不能直接用
// %20 为空格的编码,这里替换掉,URL才不会报错
URL="http://10.0.2.2:8080/AndroidServer/Music/"+ss
这样URL的文字 空格参数问题就解决了。
其实总的就两句话,修改tomcat配置,参数传之前先处理。
转自:http://www.eoeandroid.com/thread-115195-1-1.html
一般情况下,URL参数为文字和空格都会出现乱码,需经过编码后才能执行通过。
下面介绍解决方法:
1.修改tomcat里面的/conf/server.xml文件 找到<Connector port="8080" 节点。在里面
加入URIEncoding="UTF-8" 属性编码 默认编码为"ISO-8859-1"一般情况下是不支持文字参数。
修改后如下:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8"/>
2.参数处理,URL="http://10.0.2.2:8080/AndroidServer/Music/陈奕迅 - 浮夸.mp3 "
在参数 陈奕迅 - 浮夸.mp3 加入URL之前先进行处理,先对其进行编码
例如:String mm="陈奕迅 - 浮夸.mp3";
String ss=URLEncoder.encode(mm, "UTF-8"); //这里是讲String编码成UTF-8
System.out.println("ss="+ss);
//输出结果 ss=%E9%99%88%E5%A5%95%E8%BF%85%+-+E6%B5%AE%E5%A4%B8.mp3
//其中空格被编码成 + ;这样编码后空格编码还是有问题,需在处理,下面继续
ss=ss.replaceAll("\\+","%20"); //因为 + 符号在java是关键字符需要转义,不能直接用
// %20 为空格的编码,这里替换掉,URL才不会报错
URL="http://10.0.2.2:8080/AndroidServer/Music/"+ss
这样URL的文字 空格参数问题就解决了。
其实总的就两句话,修改tomcat配置,参数传之前先处理。
转自:http://www.eoeandroid.com/thread-115195-1-1.html
相关文章推荐
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- 关于写入文件缓存时报"java.io.FileNotFoundException open failed (is a director)"错误的解决方法
- java.io.FileNotFoundException: Permission denied 错误解决方法
- 关于HttpURLConnection遇到ava.io.FileNotFoundException的解决方法
- java.io.FileNotFoundException: 下载项目路径下的文件
- GraphicsMagick java.io.FileNotFoundException: gm 错误解决办法
- 如何解决java.io.FileNotFoundException: mysql.ini (系统找不到指定的文件。)
- tomcat启动报java.io.NotSerializableException或者WriteAbortedException错误解决方法
- 用jdbc中加载配置文件是出现java.io.FileNotFoundException的解决之道
- Android下载文件提示文件不存在。。。 java.io.FileNotFoundException
- Android异常--java.io.FileNotFoundException下载文件时异常
- 文件路径正确,报java.io.FileNotFoundException异常的原因及解决办法
- tomcat启动报java.io.NotSerializableException或者WriteAbortedException错误解决方法
- 【Bug解决】Spring配置错误:java.io.FileNotFoundException: class path resource [cxd/entity/User.hbm.xml] canno
- cloudera-scm-server启动时出现Caused by: java.io.FileNotFoundException: /var/lib/cloudera-scm-server/.keystore (No such file or directory)问题解决方法(图文详解)
- tomcat错误:java.io.FileNotFoundException: Could not resolve XML resource [null] with public ID [null]
- java.io.FileNotFoundException: jndi:(综合网上各大解决方法,最终解决)com.sun.faces.config.AnnotationScanner
- Struts tomcat下java.io.NotSerializableException错误的解决方法