链接ftp,把文件或图片上传到ftp指定的文件夹中
/********************************************************************
*
*
* Filename : .java
* Author :
* Date : 2015年6月5日
* Version : V1.00
* Description :
*
* History : Modify Id | Date | Origin | Description
*******************************************************************/
package com.etCrm.utils.teamUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtils
{
public static FTPClient ftp = null;
/**
*
*
public static String url="";
/**
* FTP地址
*/
public static String ftpurl="";
/**
* FTP 端口
*/
public static String port="21";
/**
* FTP用户名
*/
public static String username="";
/***
* FTP密码
*/
public static String password="";
/**
* 文件路径
*/
public static String filepath="/home/web/teams/file/";
/**
* 图片上传
*/
public static String imgpath="/home/web/teams/img/";
public static String newpath="";
/**
*
* @Title: getdateyyymmdd
* @Description: 获取日期
* @return
* @return String 返回类型
* @author
* @since 1.0.0
*/
public static String getdateyyy() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date date=new Date();
return ""+sdf.format(date);
}
public static String getdateMMdd() {
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
Date date=new Date();
return ""+sdf.format(date);
}
public static String getdateHH() {
SimpleDateFormat sdf = new SimpleDateFormat("HH");
Date date=new Date();
return ""+sdf.format(date);
}
/**
* @Title: CreateDir
* @Description: TODO创建文件夹
* @param dirname
* @return void 返回类型
* @author
* @since 1.0.0
*/
/**
* 创建文件夹
* @param dir
* @param ftpClient
* @throws Exception
*/
/***
* @上传文件夹
* @param localDirectory
* 当地文件夹
* @param remoteDirectoryPath
* Ftp 服务器路径 以目录"/"结束
* */
//然后再利用ftpclient的makeDirectory方法创建文件夹
public static String createDir(String path){
try{
path=path+""+getdateyyy();
path=(path.substring(path.indexOf("teams")-1, path.length()));
ftp.makeDirectory(path);
path=path+"/"+getdateMMdd();
ftp.makeDirectory(path);
path=path+"/"+getdateHH();
ftp.makeDirectory(path);
System.out.println("在目标服务器上成功建立了文件夹: " + path);
return path;
}catch(Exception ex){
System.out.println(ex.getMessage());
}
return path;
}
/**
*
* @Title: uploadFile
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param path
* @param filename
* @param input
* @return
* @throws Exception
* @return boolean 返回类型
* @author
* @since 1.0.0
*/
public static boolean uploadFile(String path,
String filename, InputStream input) throws Exception
{
boolean success = false;
ftp=new FTPClient();
try
{
int reply;
ftp.connect(ftpurl, Integer.valueOf(port));
// 登录FTP
ftp.login(username, password);
ftp.enterLocalActiveMode();
reply = ftp.getReplyCode();
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
System.out.println(reply);
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return success;
}
//图片文件夹
ftp.changeWorkingDirectory(path);
path= createDir(path);
// uploadDirectory();//创建文件夹
// path=path+""+newfile+"/";
newpath=path;
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
}
catch (IOException e)
{
success = false;
throw e;
}
finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException e)
{
throw e;
}
}
}
return success;
}
/**
* 上传图片或者文件的方法
* @Title: uploadFtpFlie
* 上传图片或者文件的方法
* @param istype 0上传文件 1上传图片
* @param file
* @param fileName
* @return
* @return String 返回类型
* @author
* @throws IOException
* @since 1.0.0
*/
public static String uploadFtpFlie(String istype,File file,String fileName) throws IOException
{
FileInputStream in =null;
boolean isbool=false;
try
{
in = new FileInputStream(file);
if(istype.equals("0"))
{
isbool=FtpUtils.uploadFile(filepath,fileName,in);
if(isbool)
{
System.out.println(url+newpath+"/" +fileName);
return url+newpath+"/" +fileName;
}
else
{
return "error";
}
}
else
{
isbool=FtpUtils.uploadFile(imgpath,fileName,in);
if(isbool)
{
System.out.println(url+newpath+"/" +fileName);
return url+newpath+"/" +fileName;
}
else
{
return "error";
}
}
}
catch (Exception e)
{
// TODO 自动生成 catch 块
e.printStackTrace();
return "error";
}
finally
{
in.close();
}
}
public static void main(String agrs[]) {
try {
File file = new File("D:\\000.jpg");
String k= uploadFtpFlie("1", file, "22222.JPG");
System.out.println(k);
} catch (Exception e) {
e.printStackTrace();
}
}
}