Link ftp to upload files or images to FTP-specified folders

Source: Internet
Author: User
Tags ftp server path

/********************************************************************
*
*
* Filename:. java
* Author:
* Date:2015 Year June 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 Address
*/
public static String ftpurl= "";

/**
* FTP Port
*/
public static String port= "21";


/**
* FTP user Name
*/
public static String username= "";

/***
* FTP Password
*/
public static String password= "";

/**
* File path
*/
public static String filepath= "/home/web/teams/file/";

/**
* Image upload
*/
public static String imgpath= "/home/web/teams/img/";


public static String newpath= "";
/**
*
* @Title: GETDATEYYYMMDD
* @Description: Get date
* @return
* @return String return type
* @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 Create folder
* @param dirname
* @return void return type
* @author
* @since 1.0.0
*/
/**
* Create Folder
* @param dir
* @param ftpclient
* @throws Exception
*/
/***
* @ Upload Folder
* @param localdirectory
* Local Folder
* @param Remotedirectorypath
* FTP server path with directory "/" End
* */
Then use the FtpClient Makedirectory method to create the folder
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 ("successfully established a folder on the target server:" + path);
return path;
}catch (Exception ex) {
System.out.println (Ex.getmessage ());
}
return path;
}
/**
*
* @Title: UploadFile
* @Description: TODO (here is a word describing the effect of this method)
* @param path
* @param filename
* @param input
* @return
* @throws Exception
* @return Boolean return type
* @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));
Log in to 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;
}
Picture folder
Ftp.changeworkingdirectory (path);
Path= createdir (path);
Uploaddirectory ();//Create Folder
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;
}

/**
* How to upload pictures or files
* @Title: Uploadftpflie
* How to upload pictures or files
* @param istype 0 upload file 1 upload image
* @param file
* @param fileName
* @return
* @return String return type
* @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 automatically generates catch blocks

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

Link ftp to upload files or images to FTP-specified folders

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.