所用的依赖包 common-net/io
自己整理的工具类
自己整理的工具类
package com.safein.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import javax.imageio.stream.FileImageInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.util.Base64;
import org.apache.log4j.Logger;
/**
* ftp连接工具类
* @author julong
* @date 2016-7-16 上午09:41:31
*/
public class FtpUtil {
/**
* 日志
* @author julong
* @date 2016-7-16 上午10:07:04
*/
private static Logger logger = Logger.getLogger(FtpUtil.class);
/**
* ftp用户名
* @author julong
* @date 2016-7-16 上午09:41:55
*/
private static String username = MessageUtil.getInstance().getProperty("username");
/**
* ftp用户密码
* @author julong
* @date 2016-7-16 上午09:42:07
*/
private static String password = MessageUtil.getInstance().getProperty("password");
/**
* 登录地址
* @author julong
* @date 2016-7-16 上午09:45:37
*/
private static String hostname = MessageUtil.getInstance().getProperty("hostname");
/**
* 连接端口号
* @author julong
* @date 2016-7-16 上午09:45:51
*/
private static String port = MessageUtil.getInstance().getProperty("port");
/**
* ftp连接对象
* @author julong
* @date 2016-7-16 上午10:09:17
*/
private final static FTPClient ftpClient = new FTPClient();
/**
* byte[]转Base64String
* @param bytes
* @return String
* @author julong
* @date 2016-7-16 上午09:47:57
*/
public static String encodeBase64String(byte[] bytes){
logger.debug("【FtpUtil】byte[]转Base64String");
return Base64.encodeBase64String(bytes);
}
/**
* base64String 转byte[]
* @param base64String
* @return byte[]
* @author julong
* @date 2016-7-16 上午09:49:25
*/
public static byte[] encodeBase64String(String base64String){
logger.debug("【FtpUtil】base64String 转byte[]");
return Base64.decodeBase64(base64String);
}
/**
* byte[]转base64Byte[]
* @param binaryData
* @return
* @author julong
* @date 2016-7-16 上午09:53:30
*/
public static byte[] encodeBase64(byte[] binaryData){
logger.debug("【FtpUtil】byte[]转base64Byte[]");
return Base64.encodeBase64(binaryData);
}
/**
* 建立ftp连接对象
* @return 是否连接成功 true /false
* @throws NumberFormatException
* @throws SocketException
* @throws IOException
* @author julong
* @date 2016-7-16 上午10:12:22
*/
public static boolean connection() throws NumberFormatException, SocketException, IOException{
logger.debug("【FtpUtil】-开始建立ftp连接对象");
//建立ftp连接
ftpClient.connect(hostname, Integer.valueOf(port));
ftpClient.setAutodetectUTF8(true);
//A constant used to indicate the file(s) being transfered should be treated as a binary image, i.e., no translations should be performed.
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//设置以二进制传送
return ftpClient.login(username, password);
}
/**
* ftp是否已连接
* @return true/false
* @author julong
* @date 2016-7-16 上午10:15:01
*/
public static boolean isConnected(){
logger.debug("【FtpUtil】-ftp连接是否已经打开"+ftpClient.isConnected());
return ftpClient.isConnected();
}
/**
* 根据文件URL获取OutputStream
* @param remote 文件绝对URL
* @return OutputStream
* @throws IOException
* @author julong
* @date 2016-7-16 上午10:19:14
*/
public static OutputStream getFtpFileOutputStream(String remote) throws IOException{
logger.debug("【FtpUtil】-根据文件URL获取OutputStream");
return ftpClient.storeFileStream(remote);
}
/**
* 根据文件URL获取InputStream
* @param remote 文件绝对URL
* @return InputStream
* @throws IOException
* @author julong
* @date 2016-7-16 上午10:19:14
*/
public static InputStream getFtpFileInputStream(String remote) throws IOException{
logger.debug("【FtpUtil】-根据文件URL获取InputStream");
return ftpClient.retrieveFileStream(remote);
}
/**
* InputStream 转byte[]
* @param inputStream 输入流
* @return byte[]
* @throws IOException
* @author julong
* @date 2016-7-16 上午10:22:50
*/
public static byte[] getInputStreamToByte(InputStream inputStream) throws IOException{
logger.debug("【FtpUtil】-InputStream 转byte[]");
return IOUtils.toByteArray(inputStream);
}
/**
* ftp文件的上传
* @param remote 上传路径
* @param local 文件流
* @return boolean
* @throws IOException
* @author julong
* @date 2016-7-17 下午01:18:51
*/
public static boolean storeFile(String remote, InputStream local) throws IOException{
logger.debug("【FtpUtil】-文件上传");
return ftpClient.storeFile(remote, local);
}
/**
* ftp文件的上传
* @param remote 上传路径
* @param local 文件流
* @return boolean
* @throws IOException
* @author julong
* @date 2016-7-17 下午01:18:51
*/
public static boolean storeUniqueFile(String remote, InputStream local) throws IOException{
logger.debug("【FtpUtil】-文件唯一性上传");
return ftpClient.storeUniqueFile(remote, local);
}
/**
* 关闭ftp
* @return boolean
* @throws IOException
* @author julong
* @date 2016-7-16 上午10:30:50
*/
public static boolean logout() throws IOException{
logger.debug("【FtpUtil】-关闭ftp");
return ftpClient.logout();
}
/**
* @param args
* @author julong
* @date 2016-7-15 下午06:03:49
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建ftp对象
FTPClient ftpClient = new FTPClient();
try {
//创建连接端口和地址
//ftpClient.connect("192.168.1.254", FTPClient.DEFAULT_PORT);
ftpClient.connect("192.168.2.103", 21);
//创建连接登录是否正常
// boolean result = ftpClient.login("ftpdata", "ftpdata");
boolean result = ftpClient.login("userftp", "userftp");
System.out.println(ftpClient.getStatus());
System.out.println(result);
//是否处于连接状态
ftpClient.isConnected();
System.out.println("是否处于连接状态"+ftpClient.isConnected());
//获取文件流
//OutputStream outputStream = ftpClient.storeFileStream("/home/FTPfile//b.jpg");
//outputStream.wait(5000)
//System.out.println(outputStream);
// InputStream inputStream = ftpClient.retrieveFileStream("/home/FTPfile//b.jpg");
// byte[] b = FileUtils.readFileToByteArray(new File("D://b.jpg"));
// System.out.println(b.length);
// FileInputStream in= new FileInputStream(new File("D:/index.html"));
// FileImageInputStream fileImageInputStream = new FileImageInputStream(new File("D:/b.jpg"));
// InputStream inputStream2 = new ByteInputStream(b, b.length);
// boolean result1 = ftpClient.storeUniqueFile("/home/userftp/ftpdir/index.html",in);
// boolean result1 = ftpClient.storeFile("/home/userftp/ftpdir/index.html", in);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
boolean result1 = ftpClient.storeFile("/home/userftp/ftpdir/index.jpg", FileUtils.openInputStream(new File("D:/b.jpg")));
System.out.println(result1);
//转换为字节流
//byte[] bytes = IOUtils.toByteArray(inputStream);
//FileUtils.writeByteArrayToFile(new File("D:/12341111.jpg"), bytes);
System.out.println("文件写完毕");
// BufferedOutputStream
//转换为64位
//String str = Base64.encodeBase64String(bytes);
//System.out.println(str);
// if(result){
// ftpClient.disconnect();
// }
//登出
//ftpClient.logout();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
经测试,一切正常,上传文件 和图片都OK 特此一记,以备后用