`
yangyangmyself
  • 浏览: 229924 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Apache ftp tools 图片下载支持中文

    博客分类:
  • Java
阅读更多
写道
Apache Commom net:
1) 递归path,调用changeWorkingDirectory改变工作目录并验证是否存在
 然后直接调用retrieveFileStream(filename),filename不用带路径,path经过编码后,filename带全路径存在问题;
2)编码方式统一用new String(filename.getBytes("UTF-8"),"iso-8859-1")
3)ftpClient.getSystemType()获取FTP服务器操作系统,动态设置编码

 

写道
问题:1)原下载图片时直接返回InputStream,然后在业务代码位置获取数据时阻塞无响应,错误信息:toString() unavailable - no suspended threads

对于网络流InputStream available经常返回0,即网络存在延时,方法执行完后立刻关闭连接,数据还未接收完或正在接收中时被关闭,倒致InputStream read方法阻塞。

 

public static InputStream getBytes(URL url){
		FTPClient ftpClient = null;
		InputStream inputStream = null;
		String filename = null;
		String pathname = null;
		String fullpath = null;
		String host = url.getHost();
		String user = "anonymous";
		String pass ="";
		int port = url.getPort();
		String path = url.getPath();
		String userInfo = url.getUserInfo();
		try {
			if (userInfo != null) { // get the user and password
				int delimiter = userInfo.indexOf(':');
				if (delimiter != -1) {
					user = userInfo.substring(0, delimiter++);
					pass = userInfo.substring(delimiter);
				}
			}
			ftpClient = getClient(host, user, pass, port);
			if(!path.endsWith("/")){
				int i = path.lastIndexOf('/');
				if(i>0){
					filename=path.substring(i+1,path.length());
					pathname=path.substring(0,i);
				}else{
					filename=path;
					pathname=null;
				}
			} else {
				pathname=path.substring(0,path.length()-1);
				filename=null;
			}
			if(pathname!=null){
				fullpath=pathname+"/"+(filename!=null?filename:"");
			}else{
				fullpath = filename;
			}
			// 编码后的pathname
			StringBuffer sb = new StringBuffer();			
			ftpClient.setControlEncoding("UTF-8");
			String encodeFileName = new String(filename.getBytes("UTF-8"),FTP_ENCODE);
			StringTokenizer token = new StringTokenizer(path,"/");
			long a = System.currentTimeMillis();
			while (token.hasMoreTokens()){
				String fold = token.nextToken();
				// 中文编码
				String encodeFold = new String(fold.getBytes("UTF-8"),FTP_ENCODE);
				boolean flag = ftpClient.changeWorkingDirectory(encodeFold);
				if(!flag){
					System.out.println("目录不存在:"+fold);
					System.out.println("指定的目录不存在或ftp无法打开,路径为:"+pathname);
					break;
				}
				sb.append(encodeFold);
				if(token.hasMoreTokens()){
					sb.append("/");
				} else {
					sb.append("/");
					sb.append(encodeFileName);
				}
			}
			long b = System.currentTimeMillis();
			inputStream=ftpClient.retrieveFileStream(encodeFileName);
		} catch (FTPConnectionClosedException e) {
			System.err.println("Server closed connection.");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			
			if (ftpClient.isConnected()) {
				try {
					ftpClient.disconnect();
				} catch (IOException f) {
					// do nothing
				}
			}
		}
		return inputStream;
	}

 

写道
数据获取完后,再关闭连接。升级后正常

 

package com.sunshine.app.util.ftp;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.StringTokenizer;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPReply;

public abstract class FtpTools {

	private static String FTP_ENCODE = "iso-8859-1";
	
	private static byte[] EMPTY_BYTE = new byte[0];
	
	public static FTPClient getClient(String host, String user, String pass, int port){
		FTPClient ftpClient = null;
		ftpClient = new FTPClient();
		try {
			ftpClient.connect(host, port);
			int defaultTimeout = 30 * 60 * 1000;
			ftpClient.setConnectTimeout(3 * 1000);
			ftpClient.setControlKeepAliveTimeout(defaultTimeout);
			ftpClient.setControlKeepAliveReplyTimeout(defaultTimeout);			
			int reply = ftpClient.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftpClient.disconnect();
				System.err.println("FTP server refused connection.");
			}
		} catch (IOException e) {
			if (ftpClient.isConnected()) {
				try {
					ftpClient.disconnect();
				} catch (IOException f) {
					// do nothing
				}
			}
			System.err.println("Could not connect to server.");
			e.printStackTrace();
		}
		try
        {
            if (!ftpClient.login(user, pass)){
                ftpClient.logout();
            }
            System.out.println("Remote system is " + ftpClient.getSystemType());
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);        
            // Use passive mode as default because most of us are
            // behind firewalls these days.
            //ftpClient.enterLocalActiveMode();
            ftpClient.enterLocalPassiveMode();
            ftpClient.setUseEPSVwithIPv4(false);
        }catch(FTPConnectionClosedException e){
            System.err.println("Server closed connection.");
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
		} finally {
			if (ftpClient.isConnected()) {
				try {
					ftpClient.disconnect();
				} catch (IOException f) {
					// do nothing
				}
			}
		}
		return ftpClient;
	}
	
	public static byte[] getBytes(URL url){
		FTPClient ftpClient = null;
		InputStream inputStream = null;
		String filename = null;
		String pathname = null;
		String fullpath = null;
		String host = url.getHost();
		String user = "anonymous";
		String pass ="";
		int port = url.getPort();
		String path = url.getPath();
		String userInfo = url.getUserInfo();
		try {
			if (userInfo != null) { // get the user and password
				int delimiter = userInfo.indexOf(':');
				if (delimiter != -1) {
					user = userInfo.substring(0, delimiter++);
					pass = userInfo.substring(delimiter);
				}
			}
			ftpClient = getClient(host, user, pass, port);
			if(!path.endsWith("/")){
				int i = path.lastIndexOf('/');
				if(i>0){
					filename=path.substring(i+1,path.length());
					pathname=path.substring(0,i);
				}else{
					filename=path;
					pathname=null;
				}
			} else {
				pathname=path.substring(0,path.length()-1);
				filename=null;
			}
			if(pathname!=null){
				fullpath=pathname+"/"+(filename!=null?filename:"");
			}else{
				fullpath = filename;
			}
			// 编码后的pathname
			StringBuffer sb = new StringBuffer();			
			ftpClient.setControlEncoding("UTF-8");
			String encodeFileName = new String(filename.getBytes("UTF-8"),FTP_ENCODE);
			StringTokenizer token = new StringTokenizer(path,"/");
			long a = System.currentTimeMillis();
			while (token.hasMoreTokens()){
				String fold = token.nextToken();
				// 中文编码
				String encodeFold = new String(fold.getBytes("UTF-8"),FTP_ENCODE);
				boolean flag = ftpClient.changeWorkingDirectory(encodeFold);
				if(!flag){
					System.out.println("目录不存在:"+fold);
					System.out.println("指定的目录不存在或ftp无法打开,路径为:"+pathname);
					break;
				}
				sb.append(encodeFold);
				if(token.hasMoreTokens()){
					sb.append("/");
				} else {
					sb.append("/");
					sb.append(encodeFileName);
				}
			}
			long b = System.currentTimeMillis();
			inputStream=ftpClient.retrieveFileStream(encodeFileName);
			long c = System.currentTimeMillis();
			System.out.println("CD--"+(b-a) + " RF--" + (c-b));
			if(inputStream == null)
				return EMPTY_BYTE;
			return transferToByteArray(inputStream);
		} catch (FTPConnectionClosedException e) {
			System.err.println("Server closed connection.");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			
			if (ftpClient.isConnected()) {
				try {
					ftpClient.disconnect();
				} catch (IOException f) {
					// do nothing
				}
			}
		}
		return EMPTY_BYTE;
	}
	
	public static byte[] transferToByteArray(InputStream input){
		BufferedInputStream bi = null;
		ByteArrayOutputStream barray = null;
		try {
			byte[] buffer = new byte[1024];
			bi = new BufferedInputStream(input); 
			barray = new ByteArrayOutputStream();
			int len = 0;
			while((len=bi.read(buffer))!=-1){
				barray.write(buffer, 0, len);
			}
			barray.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				if(barray != null)
					barray.close();
				if(bi != null)
					bi.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return barray.toByteArray();
	}
}

 

 

0
0
分享到:
评论

相关推荐

    hadoop0.23.9离线api

    org.apache.hadoop.fs.ftp org.apache.hadoop.fs.http.client org.apache.hadoop.fs.http.server org.apache.hadoop.fs.kfs org.apache.hadoop.fs.permission org.apache.hadoop.fs.s3 org.apache.hadoop.fs....

    APACHE 2.2.9+TOMCAT6.0.18配置负载均衡

    TOMCAT6.0.20下载:apache-tomcat-6.0.20.zip直接解压。http://tomcat.apache.org/download-60.cgi Jdk安装目录下D:\toots\Java\jdk1.6.0_18\bin\msvcr71.dll复制到C:\WINDOWS\system32下 二、 安装过程 设置环境...

    pro_apache_third_edition..pdf

    Networking Tools...............................................................................................26 Server Hardware..........................................................................

    httpd-2.4.33安装(附安装包,亲测好用)

    #./configure --prefix=/home/webapp/apache_tools/extra/apr-util --with-apr=/home/webapp/apache_tools/extra/apr #make&&make; install cd pcre-8.42 ./configure --prefix=/home/webapp/apache_tools/httpd-...

    08内存及存储管理(下)

    [意见反馈][官方博客] Apache AXIS 开发 Web Services 收藏 http://blog.csdn.net/tonyzhangcn/archive/2006/10/31/1358207.aspx < type="text/javascript">function StorePage(){d=document;t=d.selection?(d....

    Ubuntu.15.04.Server.with.systemd.Administration.and.Reference.epub

    Key servers are examined, including Web (Apache), FTP (vsftpd), printing (CUPS), NFS, and Samba (Windows). Network support servers and applications covered include the Squid proxy server, the Domain ...

    nginx+tomcat高可用、高性能jsp集群

    Development Tools Editors Text-based Internet ./init_system.sh #此脚本参见http://kerry.blog.51cto.com/172631/555535 三、LVS+keeplived #关于LVS+keeplived的配置请参考我的另一篇博文《CentOS5.5环境下布署...

    Linux操作系统实验四.doc

    rpm -ivh httpd-tools*. Rpm rpm -ivh mailcap-2.1.31-2.el6.noarch.rpm rpm - ivh httpd-2*. rpm rpm -ivh httpd-manual-2.*.rpm ●重新启动/停止/启动Apache服务: systemctl restart/stop/start httpd.service 或...

    R软件代码转换为matlab-Tools:专业所需工具

    Github出品的代码编辑器,原生支持Markdown。 Code::Blocks 非常好用的开源C/C++ IDE,与mingw搭配使用。替代vc6.0的免费解决方案。 Chrome 一款快速、简单且安全的浏览器,谷歌出品。 Feeddemon RSS聚合新闻阅读器...

    cyberduck:Cyber​​duck是适用于Mac和Windows的FTP,SFTP,WebDAV,Amazon S3,Backblaze B2,Microsoft Azure和OneDrive和OpenStack Swift文件传输客户端

    choco install visualstudio2019buildtools -y choco install wixtoolset -y choco install visualstudio2019-workload-manageddesktopbuildtools --params "--add Microsoft.Net.Component.4.7.TargetingPack" -y ...

    Sams.Ubuntu.Unleashed.Aug.2006.part1

    Chapter 20 Apache Web Server Management Chapter 21 Administering Database Services Chapter 22 File and Print Chapter 23 Remote File Serving with FTP Chapter 24 Handling Electronic Mail Chapter ...

    Sams.Ubuntu.Unleashed.Aug.2006.part2

    Chapter 21 Administering Database Services Chapter 22 File and Print Chapter 23 Remote File Serving with FTP Chapter 24 Handling Electronic Mail Chapter 25 Proxying and Reverse ...

    Magento 2 Cookbook

    Chapter 1, Installing Magento 2 on Apache and NGINX, is a totally different ballgame compared to Magento 1. Where Magento 1 could be installed through FTP or SSH, Magento 2 is installable only via the...

    Magento2 CookBook

    Chapter 1, Installing Magento 2 on Apache and NGINX, is a totally different ballgame compared to Magento 1. Where Magento 1 could be installed through FTP or SSH, Magento 2 is installable only via the...

    Sams.Publishing.Ubuntu.Unleashed.2008.Edition.pdf

    18 Remote File Serving with FTP.........439 19 Handling Electronic Mail..........471 20 Proxying and Reverse Proxying ........489 21 Administering Database Services ........499 22 LDAP............525 ...

    CodeLobster.PHP.Edition.Pro.4.3.2

    FTP crash Indent guides for mixed code Resize for status bar Autodetect for Yii database CSS breakdown in File Window Changes in French translation Joomla database connection Joomla syntax ...

    CodeLobster_PHP_Edition_Pro_4.1.0

    FTP crash Indent guides for mixed code Resize for status bar Autodetect for Yii database CSS breakdown in File Window Changes in French translation Joomla database connection Joomla syntax ...

    CodeLobster PHP Edition Pro 4.0.1

    FTP crash Indent guides for mixed code Resize for status bar Autodetect for Yii database CSS breakdown in File Window Changes in French translation Joomla database connection Joomla syntax errors ...

    asp.net知识库

    鼠标放在一个连接上,会显示图片(类似tooltip) 使用microsoft.web.ui.webcontrols的TabStrip与IFame组件,达到页的切换效果 HttpModule 实现 ASP.Net (*.aspx) 中文简繁体的自动转换,不用修改原有的任何代码,直接部署...

    Linux命令大全完整版

    apachectl(Apache control interface) 171 smbclient(samba client) 171 pppsetup 172 10. linux电子邮件与新闻组命令 173 archive 173 ctlinnd(control the internet news daemon) 173 elm 173 getlist 174 ...

Global site tag (gtag.js) - Google Analytics