/********************************************************************

*
* 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();
}
}
}

链接ftp,把文件或图片上传到ftp指定的文件夹中的更多相关文章

  1. 【应用】:shell crontab定时生成oracle表的数据到txt文件,并上传到ftp

    一.本人环境描述      1.oracle服务端装在win7 32位上,oracle版本为10.2.0.1.0      2.Linux为centos6.5 32位,安装在Oracle VM Vir ...

  2. Jquery图片上传组件,支持多文件上传

    Jquery图片上传组件,支持多文件上传http://www.jq22.com/jquery-info230jQuery File Upload 是一个Jquery图片上传组件,支持多文件上传.取消. ...

  3. Retrofit 2.0 超能实践(三),轻松实现文件/多图片上传/Json字符串

    文:http://blog.csdn.net/sk719887916/article/details/51755427 Tamic 简书&csdn同步 通过前两篇姿势的入门 Retrofit ...

  4. 图片上传怎么用File接受文件

    xl_echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.——这才是真正的堪称强大!! - ...

  5. FileReader()读取文件、图片上传预览

    前言 FileReader 对象允许Web应用程序异步读取存储在用户计算机上的文件(或原始数据缓冲区)的内容,使用 File 或 Blob 对象指定要读取的文件或数据. 其中File对象可以是来自用户 ...

  6. aps.net mvc webapi 实现文件或图片上传

    前几天看到网上有很多复杂的实现方式,觉得没必要,所以就写个简单的实现. 一:首先来看看Api Controller里面的代码: HttpContext.Current.Request.Files  这 ...

  7. (文件)图片上传,Spring或SpringMVC框架

    spring或springMVC框架图片(文件)上传 页面部分,用一个简单的form表单提交文件,将图片或文件提交到服务端.一个输入框,用于输入图片的最终名称,一个file文件选择,用于选择图片. 页 ...

  8. ifram+form方式实现文件、图片上传、预览

    1.前端代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  9. 图片上传利用request.getInputStream()获取文件流时遇到的问题

    图片上传功能是我们web里面经常用到的,获得的方式也有很多种,这里我用的是request.getInputStream()获取文件流的方式.想要获取文件流有两种方式,附上代码 int length = ...

随机推荐

  1. 每天一个linux命令(网络):【转载】ifconfig命令

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  2. Linux下编写动态链接库

    下面通过一个例子来介绍如何生成一个动态库.这里有一个头文件:so_test.h,三个.c文件:test_a.c.test_b.c.test_c.c,我们将这几个文件编译成一个动态库:libtest.s ...

  3. git源站安装

    ##下载源站 wget https://www.kernel.org/pub/software/scm/git/git-2.15.0.tar.xz ##安装依赖组件 yum install curl- ...

  4. python random模块(随机数)详解

    使用前要先导入random模块 import random random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random ...

  5. hdu 4336 Card Collector——最值反演

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 点集中最早出现的元素的期望是 min ,最晚出现的元素的期望是 max :全部出现的期望就是最晚出现 ...

  6. How to Configure Eclipse for Python --- 在eclipse中如何配置pydev

    From: http://www.rose-hulman.edu/class/csse/resources/Eclipse/eclipse-python-configuration.htm Pytho ...

  7. FPGA--数字芯片之母

    这个世界先有鸡还是先有蛋?没有人知道答案.但是如果有人问ess9018.ak4497.cs43198这些高端SIGMADELTA架构DAC的妈妈是谁?我们可以回答您:它们都有一个同样的妈,名字叫做FP ...

  8. 汇编_指令_INC

    加1指令 INC指令功能:目标操作数+1 INC指令只有1个操作数,它将指定的操作数的内容加1,再将结果送回到该操作数.INC指令将影响SF,AF,ZF,PF,OF标志位,但是不影响CF标志位. IN ...

  9. 利用MessageFormat实现短信模板的匹配

    其实没什么技术含量,因为老是想不起来,所以在此文做下记录. 通常我们的应用系统中都会有很多短信的发送,或者是信息邮件等的推送,而这些信息却有着相同的共性,比如只是用户名换了下. 像下面这条,除了红色字 ...

  10. tensorflow-base_operations

    # -*- coding: utf-8 -*-import tensorflow as tf# 基本的常量操作,通过构造函数返回值 定义值的操作operationsa = tf.constant(2) ...