这是我的网址
String serverUrl = http://mob.krill.order-line.co.uk/MobileService.svc/UploadFile/721/Universal Image Loader @#&=+-_.,!()~'%20.png
此处Universal Image Loader @#&=+-_.,!()~'%20.png
只是动态赋予字符串的文件名。
HttpPost httpPost = new HttpPost(serverUrl);
我明白了:
java.lang.IllegalArgumentException: Illegal character in path at index 76: http://mob.krill.order-line.co.uk/MobileService.svc/UploadFile/721/Universal Image Loader @#&=+-_.,!()~'%20.png
有什么补救措施?
答案 0 :(得分:4)
您应该对字符串进行URL编码。
String encoded = URLEncoder.encode (
"http://mob.krill.order-line.co.uk/MobileService.svc/UploadFile/721/Universal Image Loader @#&=+-_.,!()~'%20.png",
"UTF-8");
HttpPost httpPost = new HttpPost(encoded);
正如Duncan正确评论的那样编辑,如果您的网址包含查询字符串,其中包含字符?
和&
,则您不希望对其进行网址编码。您只需对包含有问题字符的字符串进行编码。
答案 1 :(得分:1)
根据上述建议,我的代码使用以下解决方案
String fileName = f.getName();
try {
fileName = URLEncoder.encode(fileName,"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String serverUrl = http://mob.krill.order-line.co.uk/MobileService.svc/UploadFile/721/+fileName;