讀取本地圖片上次圖片服務器

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;

public static void main(String[] args) throws IOException {
        // 讀取本地文件夾圖片上傳
        String host = "127.0.0.1";
        Integer port = 21;
        String username = "user";
        String password = "123456";
        String basePath = "";

     //本地圖片路徑
        String url = "C:/Users/Administrator/Desktop/上傳圖片";
        File file = new File(url);
        File[] files = file.listFiles();
        for (File f : files) {
            try {
                // 讀取圖片轉換爲流
                FileInputStream fis = new FileInputStream(f);
                // 得到上傳的文件
                MultipartFile mFiles = ImageUtil.base64Convert(getBase64FromInputStream(fis));
                if (null != mFiles) {
                    String saveUrls = "";
                    String fileName = f.getName();
                    String prefix = "";
                    String savePath = "/mingwen" + "/" + "wenxuanBook" + "/20200616";
                    FTPUtils.uploadFile(host, port, username, password, basePath, savePath, fileName + prefix, mFiles);
                    saveUrls += savePath + "/" + fileName + prefix;
                    // 輸出流寫到本地
                    writeUrls(saveUrls, "C:/Users/Administrator/Desktop/urls.txt");
                }

            } catch (Exception e) {
                continue;
            }

        }

    }

 

 

// 寫入到本地
    public static void writeUrls(String url, String outPath) throws IOException {
        File txt = new File(outPath);
        url = url + "\r\n";
        byte[] bytes = new byte[512];
        bytes = url.getBytes();
        int length = bytes.length;

        FileOutputStream fos = new FileOutputStream(txt, true);
        fos.write(bytes, 0, length);
        fos.flush();
        fos.close();
    }

    public static String getBase64FromInputStream(InputStream in) {
        // 將圖片文件轉化爲字節數組字符串,並對其進行Base64編碼處理
        byte[] data = null;
        // 讀取圖片字節數組
        try {
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = in.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            data = swapStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return new String(Base64.encodeBase64(data));
    }

 

 

 

package com.mingwen.common.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
public class FTPUtils {
    /**
     * @description 向FTP服務器上傳文件
     * @param host     FTP服務器hostname
     * @param port     FTP服務器端口
     * @param username FTP登錄賬號
     * @param password FTP登錄密碼
     * @param basePath FTP服務器基礎目錄
     * @param filePath FTP服務器文件存放路徑。例如分日期存放:/2015/01/01。文件的路徑爲basePath+filePath
     * @param filename 上傳到FTP服務器上的文件名
     * @param input    輸入流
     * @return 成功返回true,否則返回false
     */
    public static boolean uploadFile(String host, Integer port, String username, String password, String basePath, 
            String filePath, String filename, MultipartFile file) {
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(host, port);// 連接FTP服務器
            // 如果採用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器
            ftp.login(username, password);// 登錄
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            String str = basePath + filePath;
            // 切換到上傳目錄
            if (!ftp.changeWorkingDirectory(str)) {
                // 如果目錄不存在創建目錄
                String[] dirs = filePath.split("/");
                String tempPath = basePath;
                for (String dir : dirs) {
                    if (null == dir || "".equals(dir)) {
                        continue;
                    }
                    tempPath += "/" + dir;
                    if (!ftp.changeWorkingDirectory(tempPath)) {
                        if (!ftp.makeDirectory(tempPath)) {
                            return result;
                        } else {
                            ftp.changeWorkingDirectory(tempPath);
                        }
                    }
                }
            }
             ftp.enterLocalPassiveMode();
            // 設置上傳文件的類型爲二進制類型
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            // 上傳文件
            if (!ftp.storeFile(filename, file.getInputStream())) {
                return result;
            }
            file.getInputStream().close();
            ftp.logout();
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }

    /**
     * @description 從FTP服務器下載文件
     * @param host       FTP服務器hostname
     * @param port       FTP服務器端口
     * @param username   FTP登錄賬號
     * @param password   FTP登錄密碼
     * @param remotePath FTP服務器上的相對路徑
     * @param fileName   要下載的文件名
     * @param localPath  下載後保存到本地的路徑
     * @return
     */
    public static boolean downloadFile(String host, Integer port, String username, String password, 
            String remotePath, String fileName, String localPath) {
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(host, port);
            // 如果採用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器
            ftp.login(username, password);// 登錄
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            ftp.changeWorkingDirectory(remotePath);// 轉移到FTP服務器目錄
            
            FTPFile[] fs = ftp.listFiles();
            for (FTPFile ff : fs) {
                if (ff.getName().equals(fileName)) {
                    File localFile = new File(localPath + "/" + ff.getName());
                    OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }
            ftp.logout();
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }
    
    public static boolean uploadFile(String host, Integer port, String username, String password, String basePath, 
            String filePath, String filename, InputStream io) {
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(host, port);// 連接FTP服務器
            // 如果採用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器
            ftp.login(username, password);// 登錄
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            String str = basePath + filePath;
            // 切換到上傳目錄
            if (!ftp.changeWorkingDirectory(str)) {
                // 如果目錄不存在創建目錄
                String[] dirs = filePath.split("/");
                String tempPath = basePath;
                for (String dir : dirs) {
                    if (null == dir || "".equals(dir)) {
                        continue;
                    }
                    tempPath += "/" + dir;
                    if (!ftp.changeWorkingDirectory(tempPath)) {
                        if (!ftp.makeDirectory(tempPath)) {
                            return result;
                        } else {
                            ftp.changeWorkingDirectory(tempPath);
                        }
                    }
                }
            }
             ftp.enterLocalPassiveMode();
            // 設置上傳文件的類型爲二進制類型
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            // 上傳文件
            if (!ftp.storeFile(filename, io)) {
                return result;
            }
            io.close();
            ftp.logout();
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }
    
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章