java ftp上傳檔案 工具類

來源:互聯網
上載者:User

標籤:判斷   dir   path   form   str   ima   type   col   .text   

package com.learning.spboot.utils;import com.jcraft.jsch.*;import org.apache.commons.net.ftp.FTPClient;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Properties;import java.util.Vector;public class FTPUtils {    //遠程伺服器位址    private final static String homeName = "47.104.99.125";    //ftp連接埠    private final static int homePort = 22;    private final static String userename = "****";    private final static String password = "*****";        public static String init(String dirPaht,String filepath)    {        ChannelSftp sftp = null;        Channel channel = null;        Session sshSession = null;        JSch jscj=new JSch();        try {            jscj.getSession(userename,homeName,homePort);            sshSession=jscj.getSession(userename,homeName,homePort);            sshSession.setPassword(password);            Properties sshConfig = new Properties();            sshConfig.put("StrictHostKeyChecking", "no");            sshSession.setConfig(sshConfig);            sshSession.connect();            if(sshSession.isConnected()) {                System.out.println("串連成功!");                System.out.println("Session connected!");                /*sftp.setFilenameEncoding("UTF-8");                sftp.*/                channel = sshSession.openChannel("sftp");                channel.connect();                sftp = (ChannelSftp) channel;                //判斷當前上傳路徑是否存在                try {                    Vector <?> vector = sftp.ls(dirPaht);//首次路徑不存在,會報錯                    if (vector == null) {                        //sftp.rmdir(dirPaht);                        sftp.mkdir(dirPaht);                    }                }catch (Exception e){                    e.getMessage();                    sftp.mkdir(dirPaht);//建立上傳路徑                }                //切換到目錄上傳目錄下                sftp.cd(dirPaht);                File file=new File(filepath);                String filetype=file.getName().substring(file.getName().lastIndexOf(".")+1);                String newFileName= new SimpleDateFormat("yyyymmdd").format(new Date())+"."+filetype;                InputStream ins = new FileInputStream(file);                //中文名稱的                sftp.put(ins, new String(newFileName.getBytes(),"UTF-8"));            } else {                System.out.println("串連失敗!");            }        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    public static void main(String []args ){        new FTPUtils().init("/java/image/","D:\\開發工具軟體\\ideaImg\\011.jpg");    }}

 

java ftp上傳檔案 工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.