1. 程式人生 > >解決使用FTPClient物件或FtpUtil工具類上傳檔案為空(搭建Nginx圖片伺服器

解決使用FTPClient物件或FtpUtil工具類上傳檔案為空(搭建Nginx圖片伺服器

一、使用FTPClient上傳檔案為空

JAVA使用FTPClient上傳檔案時總是為空,有些資料說防火牆設定問題,但是本機防火牆已設定過。 
這裡寫圖片描述

後來查了下資料,FTP伺服器有被動模式和主動模式。 
在JAVA中將FTPClient設定為被動模式即可解決問題。 
這裡寫圖片描述

**FTPTest.java**

package com.taotao.controller;

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.junit.Test;

import com.taotao.common.utils.FtpUtil;

public class FTPTest {
    @Test
    public void testFtp() throws Exception {
        //1、連線ftp伺服器,建立FtpClient物件
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect("192.168.20.60", 21);
        //2、登入ftp伺服器,使用使用者名稱和密碼
        ftpClient.login("ftpuser", "ftpuser");
        //3、讀取本地檔案
        FileInputStream inputStream = new FileInputStream(new File("D:\\PicturesJavaEE\\javaweb.JPG"));
        //4、上傳檔案
        //設定為被動模式!!!
        ftpClient.enterLocalPassiveMode();
        //1)指定上傳目錄
        ftpClient.changeWorkingDirectory("/home/ftpuser/www/images");
        //2)修改上傳檔案的格式,二進位制格式
        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        //第一個引數:檔案在遠端伺服器的名稱
        //第二個引數:上傳文件的檔案流
        ftpClient.storeFile("hello3.JPG", inputStream);
        //5、退出登入
        ftpClient.logout();
    }




}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

這裡寫圖片描述

這裡寫圖片描述

二、使用FtpUtil工具類上傳檔案為空

之後加入Ftp工具類,結果出現了上傳檔案為空同樣的結果 
嘗試了用xshell6修改伺服器端資料夾的許可權後,還是上傳檔案為0 
winscp下顯示檔案大小為0 
這裡寫圖片描述 
這裡寫圖片描述

思考用一下應該是FtpUtil.java 中缺少了相同的設定!將FTPClient設定為被動模式

!!! 
這裡寫圖片描述

三、FtpUtil工具類

測試程式碼,上傳D盤檔案到伺服器端,命名為ftphello3.JPG


    @Test
    public void testFtpUtil() throws Exception{
        FileInputStream inputStream = new FileInputStream(new File("D:\\PicturesJavaEE\\php.JPG"));
        FtpUtil.uploadFile("192.168.20.60", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images", "/2018/5", "ftphello3.JPG", inputStream);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

這裡寫圖片描述

這裡寫圖片描述
FtpUtil.java

package com.taotao.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;

/**
 * ftp上傳下載工具類
 */
public class FtpUtil {

    /** 
     * 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, int port, String username, String password, String basePath,
            String filePath, String filename, InputStream input) {
        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;
            }
            //切換到上傳目錄
            if (!ftp.changeWorkingDirectory(basePath+filePath)) {
                //如果目錄不存在建立目錄
                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, input)) {
                return result;
            }
            input.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, int 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 void main(String[] args) {
        try {  
            FileInputStream in=new FileInputStream(new File("D:\\temp\\image\\gaigeming.jpg"));  
            boolean flag = uploadFile("192.168.25.133", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in);  
            System.out.println(flag);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  
    }
}

相關推薦

解決使用FTPClient物件FtpUtil工具檔案搭建Nginx圖片伺服器

一、使用FTPClient上傳檔案為空 JAVA使用FTPClient上傳檔案時總是為空,有些資料說防火牆設定問題,但是本機防火牆已設定過。  後來查了下資料,FTP伺服器有被動模式和主動模式。  在JAVA中將FTPClient設定為被動模式即可解決問題。  **FT

解決FTPClient檔案,顯示0位元組

JAVA使用FTPClient上傳檔案時總是為空 分析:Port模式,是客戶端C在本地開啟一個埠等服務端S去連線建立資料連線;而Pasv模式就是服務端S開啟一個埠等待客戶端C去建立一個數據連線。 解決:預設情況下,FTPCLIENT用的是port模式,可以在FTPCLIE

springboot --- 不同執行環境下FileUtils工具檔案的位置

不同執行環境下FileUtils工具類上傳檔案的位置 1.下邊這個是我經常用的一個java自帶的檔案上傳方法,其實這個主要用於臨時檔案的上傳,挺好用。但我在專案中,從來沒關注過它的路徑位置。本文主要測試檢視專案在不同執行方式下,tempFile的絕對路徑位

XML cannot be the whole program及ajaxFileUpload檔案解決方式

今天很是鬱悶,遇到了這個問題查了好幾個小時, 最終問題還是被解決了。在JSP中用ajaxFileUpload做上傳檔案時,用Firebug除錯是遇到了這個錯誤:XML cannot be the whole program,在IE下測試時,直接報錯。在網上查了很長時間,還是這

工具---頭像

- 呼叫系統相簿 protected void gotoXC() { Intent intent=new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEG

阿里雲 javascript檔案圖片、視訊、壓縮包等檔案)到 物件儲存 OSS ,返回檔案圖片、音訊、視訊等URL路徑

目的:前端上傳檔案(圖片、視訊、音訊等)到阿里雲伺服器裡面,並且獲得上傳檔案的URL路徑 前提:首先要買一個阿里雲伺服器,自己百度不會; 第一步:登入阿里雲賬號,點選管理控制檯-->物件儲存 OSS 第二步:新建儲存空間(圖一、圖二) (圖一) (圖二

nginx檔案大小限制413 Request Entity Too Large錯誤解決

nginx預設上傳最大值是1M 在nginx.conf中新增配置client_max_body_size即可,如下上傳最大為20M client_max_body_size  20m; (修改nginx.conf檔案操作如不會,請參考:  https://blog

【問題解決】利用Eclipse,在hadoop檔案到hdfs沒有內容

本文適用於一些在網上找了半天答案都沒解決問題的人群,因為我也是在按網上說道弄了一天,最後才解決的。如果你是剛遇到問題,還沒有深入,建議你檢視這篇文章 http://f.dataguru.cn/hadoop-208802-1-1.html 將問題一步一步排除後仍沒有解決的話,可以試試我

解決python傳送multipart/form-data請求檔案的問題

    #coding=utf8 import requests from uuid import uuid4 import os file_name='test' url= boundary=uuid4().hex header={'Content-Typ

阿里雲物件儲存OSS--實現隨時隨地檔案到阿里雲

需求背景:消費者多批次回饋我司生產的車載智慧後視鏡出現宕機、連不上伺服器等問題,因產品已經出到全國各地不方便去取異常log,也不可能要求消費者把log傳給我們分析。 需求目標:公司內部實現遠端後臺上傳問題機型的log。 必備條件:後視鏡有SIM卡且能夠聯網(

H5用FormData物件提交表單及檔案

1、如何使用 use multipart/form-data when your form includes any <input type="file"> elements. 2、怎麼

java FTPClient 檔案內容

如果你在本地的 java 專案裡面去用 FtpClient 上傳檔案,然後雖然檔案上傳上去了,但是檔案裡面沒有內容,那麼這說明你的機器防火牆設定有問題如下圖  為了可以正常測試,你需要把下面紅線圈起來

使用FormData物件新增欄位方式檔案

<input type="file" id="file"> var formData = new FormData()

Windows如何連線linux和檔案到linuxsecurcrt)

一般開發在Windows,部署專案在linux這個時候就要選一個方便的軟體可以將專案扔到linux上去了,securcrt.這個軟體很好實用 網上很多免安裝版的,開啟即用,首先是連線linux 這個就不說  輸入公網ip 使用者名稱 密碼就可以   SecureCRTPo

webAPI 檔案 404錯誤轉載) webAPI檔案檔案過大404錯誤的問題

webAPI檔案上傳時檔案過大404錯誤的問題  來源:https://www.cnblogs.com/dzhengyang/p/9149157.html 背景:最近公司有個需求,外網希望自動儲存資料到內網,內網有2臺伺服器可以相互訪問,其中一臺伺服器外網可以訪問,於是想在

檔案Base64格式React)

     記錄一下上傳檔案時將檔案資料轉為Base64的方法      通過 FileReader物件建立一個例項,然後使用 readAsDataURL方法將資料轉為Base64格式      注意: 讀取

百度UEditor自定義檔案儲存路徑補充)

上一篇百度UEditor自定義上傳檔案儲存路徑發表後,再解決了線上管理、線上圖片和線上附件功能不能使用的問題。 需要修改FileManager類: 註釋掉的程式碼是原來jar包的程式碼,不再需要,可以刪除掉。 //private String di

ftp檔案顯示553錯誤,以及linux伺服器檔案修改777許可權

這裡我用到了putty終端軟體。 putty的用法網上有,百度就行。 安裝完putty,用使用者名稱和密碼登入。注意這裡的使用者名稱寫root。比如我的ftp使用者名稱是yumon,那麼這裡不要寫ymon,而要寫root。 密碼正常輸入,注意密碼是不顯示的,輸入直接

ubuntu下使用filezilla檔案許可權問題open for write: permission denied)

今天在使用filezilla連線虛擬機器中的ubuntu的時候出現上次出錯,錯誤詳情為: open for write: permission denied 看完錯誤大概知道和許可權有問題,

jQuery實現附件檔案格式校驗

HTML程式碼片段 <th>正式文字:</th> <td> <input type="file" name="f_zswb" id="f_zswb"