最近一个项目要用到FTP做上传下载,我访问ftp的url中有中文名称,结果每次都报如下错:

1 Exception in thread "main" java.lang.IllegalArgumentException
2 at sun.net.www.ParseUtil.decode(Unknown Source)
3 at sun.net.www.protocol.ftp.FtpURLConnection.decodePath(Unknown Source)
4 at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(Unknown Source)
5 at URLConnectionDownloader.download(URLConnectionDownloader.java:33)
6 at URLConnectionDownloader.main(URLConnectionDownloader.java:15)

本来可以用apache的FtpClient解决这个问题,但是我不太想用那种方式,我想省点步骤。所以坚持用

URL url = new URL("ftp://xxxx:1234@192.168.1.101:21/测试/测试.jpg");这种方式,网上搜了一大堆资料,基本都是说编码问题的。用了java.net.xxx自带转码的工具也没用,尝试了各种iso8859-1、GBK、UTF-8编码,依然无效。

最后发现windows的ftp服务器,是用gbk来处理的,linux的是utf-8,原来只需要在访问ftp前设置一下系统编码就OK了,下面放代码:

System.setProperty("file.encoding", "GBK");
URL url = new URL("ftp://xxxx:1234@192.168.1.101:21/测试/测试.jpg");
is =url.openConnection().getInputStream();
output = response.getOutputStream();
byte[] buffer = new byte[4096];
int count = 0;
while ((count = is.read(buffer)) > 0) {
output.write(buffer, 0, count);
}
output.flush();
response.flushBuffer();
is.close();

到此就算OK了,用了这个就不要再去给你的路径转码了,不然会导致读取失败!

2015-04-08  PS:时隔三年回来看这篇博客,感觉自己简直是个逗比,直接把FTP的用户和密码暴漏给了前台,如果我现在写的话,会经过服务器中转后才展示出来。当年刚入行,年轻啊。。。。

解决FTP的URL访问不能有中文名称的问题,报java.lang.IllegalArgumentException的更多相关文章

  1. jdk1.8+SpringAOP注解报java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut select错误的不知原因的解决办法[仅供参考]

    先说办法:如果Aspectweaver-1.*.*jar这三个包版本比较低, 比如1.5.0这一层次的,可以找版本高一点的包替换低版本的包,问题可以得到解决 jar包的下载地址:https://mvn ...

  2. Android用Intent来启动Service报“java.lang.IllegalArgumentException: Service Intent must be explicit”错误的解决方法

    今天没事来写个播放器,照搬书上的原句,其中一句 //用于启动和停止service的Intent final Intent it = new Intent("android.mu.action ...

  3. tomcat使用cookies缓存的时候中文报错解决办法 java.lang.IllegalArgumentException: Control character in cookie value or attribute.

    报错出现 java.lang.IllegalArgumentException: Control character in cookie value or attribute. at org.apac ...

  4. Cookie存储中文报错:java.lang.IllegalArgumentException: Control character in cookie value or attribute.(转)

    项目中做自动登录和保存密码时,Cookie报错Java.lang.IllegalArgumentException,上google查了下 在http://hi.baidu.com/xtxycy/blo ...

  5. java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie解决方法

    当项目中使用单点登录功能时,通常会使用cookie进行信息的保存,这样就可以在多个子域名上存取用户信息. 比如有三个domain分别为test.com,cml.test.com,b.test.com这 ...

  6. IDEA报错: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"

    运行审核流模块: 在ActivitiServiceApplication模块日志报错: Error starting ApplicationContext. To display the auto-c ...

  7. java.lang.IllegalArgumentException: Invalid 'log4jConfigLocation 解决办法

    MyEclipse 启动tomcat 报错: java.lang.IllegalArgumentException: Invalid 'log4jConfigLocation' parameter: ...

  8. java.lang.IllegalArgumentException: Illegal character in query at index ...解决办法

    今天在写智能机器人问答实现的时候遇到了一个问题,就是我发送消息不能输入空格 给我报了一个错误java.lang.IllegalArgumentException: Illegal character ...

  9. JMeter学习-027-JMeter参数文件(脚本分发)路径问题:jmeter.threads.JMeterThread: Test failed! java.lang.IllegalArgumentException: File distributed.csv must exist and be readable解决方法

    前些天,在进行分布式参数化测试的时候,出现了如题所示的错误报错信息.此文,针对此做一个简略的重现及分析说明. JMX脚本线程组参数配置如下所示: 参数文件路径配置如下所示: 执行JMX脚本后,服务器对 ...

随机推荐

  1. Nginx/Apache发大招

    导读 网站程序的上传目录通常是不需要PHP执行解释权限,通过限制目录的PHP执行权限可以提网站的安全性,减少被攻击的机率. 下面和大家一起分享下如何在Apache和Nginx禁止上传目录里PHP的执行 ...

  2. Python自动化之一对多

    一对多 建立一对多关系之后(也就是加了外键),会在字表里面多一个"外键字段_id"这个字段 查询 #_*_coding:utf-8_*_ from django.db import ...

  3. JavaScript刷新页面n种方法

    window.location.href 属性 window.location.href=window.location.href;//刷新当前页面 asp.net 或 asp 利用此功能刷新页面 R ...

  4. destoon二次开发基础代码

    标签调用规则 http://help.destoon.com/develop/22.html 数据字典 http://help.destoon.com/dict.php destoon各类调用汇总 h ...

  5. C#通过事件跨类调用WPF主窗口中的控件

    xaml.cs文件: using System; using System.Timers; using System.Windows; using System.Windows.Forms; name ...

  6. Hibernate POJO在序列化(JSON)时遇到的若干问题

    假设某 POJO 有属性如下: private Set<User> users = new HashSet<>(0); @OneToMany(fetch = FetchType ...

  7. snprintf 使用注意

    #include <iostream> #include <cstdio> // 包含的头文件 using namespace std; int main(int argc, ...

  8. Received an invalid response. Origin 'null' is therefore not allowed access

    Received an invalid response. Origin 'null' is therefore not allowed access. 今天在做二级联动,使用ajax请求xml数据, ...

  9. 在shell 上执行mongo 查询

    需求 在写小工具的时候,经常遇到需要从mongodb 里面查东西来用,因为要跟其他bash 工具链结合在一起用,所以最理想的方法是能够在shell 上执行查询,然后pipe 给接下来的工具做处理. 方 ...

  10. Angular2表格/可排序/table

    Angular2表格 1. 官网下载Angular2开发环境,以及给出的quickstart代码示例demo(地址如下),具体步骤不在详述. https://github.com/angular/qu ...