最近一个项目要用到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. php-css外边距

    css 基本语法  selector{declaration1;declaration2;....delecrationN;} (选择器和一条或多条声明)  选择器为需要改变样式的html元素,每条声 ...

  2. 如何使用Service的Context弹出Dialog对话框,即全局性对话框

    在dialog.show()语句前加入: dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 然后在An ...

  3. Proj.4库的编译及使用

    Proj.4库的编译及使用 Proj.4是开源GIS最著名的地图投影库,GRASS GIS, MapServer, PostGIS, Thuban, OGDI, Mapnik, TopoCad, GD ...

  4. 【转】JVM介绍

    1. 什么是JVM? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来 ...

  5. Linux IO模式及 select、poll、epoll详解

    linux的IO调度文章==> https://segmentfault.com/a/1190000003063859?hmsr=toutiao.io&utm_medium=toutia ...

  6. libeventReferenceManual阅读笔记

    一.01_intro.html Example:A low-level ROT13 server with Libevent 这是一个利用event实现的server实例 Example:A simp ...

  7. iOS 8.0后使用UIAlertController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...

  8. currentColor-CSS3非常有用的变量

    一.currentColor-简介 currentColor顾名思意就是“当前颜色”,准确讲应该是“当前的文字颜色”,例如: .xxx { border: 1px solid currentColor ...

  9. bootstrap-datetimepicker 日期控件的开始日期

    今天做日期控件,需求要求设置一个时间范围限制,选择从今天开始的日期才可以选择,今天以前都不可以选择 主要体现在bootstrap-datetimepicker控件下面的2个日期参数 weekStart ...

  10. CentOS7— Redis安装(转和延续)

    Part I. Redis安装(转载部分) 一.安装 wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.t ...