上傳文件到FTP服務器

在做一個項目的時候需要將文件上傳到FTP服務器上面,該項目使用Spring MVC,在上傳的過程中使用到了Spring中的MultipartFile類,在此記錄下:
(如何搭建VSFTPD服務器參見http://www.itdecent.cn/p/3614585a00e9

1.首先是前臺頁面,文件的傳輸主要利用了表單的enctype屬性,一般會取3個值
1.application/x-www-form-urlencoded::默認類型
2.multipart/form-data:上傳文件時使用
3.text/plain:普通文本內容

<form name="form1" action="/manage/product/upload.do" method="post" enctype="multipart/form-data">
    <input type="file" name="upload_file"/>
    <input type="submit" value="springmvc上傳文件"/>
</form>

上面中的form表單將會發(fā)送到upload.do接口中,其中文件會以二進制的形式進行傳輸
接下來在后臺中寫業(yè)務邏輯
首先是引入Maven依賴,需要導入幾個Jar包

        <!--FTP上傳時依賴的Jar包-->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.1</version>
        </dependency>
        
        <!--文件上傳時用到的兩個Jar包-->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.0.1</version>
        </dependency>
      
        <!--谷歌的Guava包,里面對Java中容器類進行了封裝-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>

2.在配置文件中注入Bean
因為MultipartFile是一個接口,需要在Spring配置文件中注冊其實現(xiàn)類CommonsMultipartResolver,注意bean的id是固定值multipartResolver

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="102400"></property>
        <property name="maxInMemorySize" value="4096"></property>
        <property name="defaultEncoding" value="UTF-8"></property>
</bean>

3.將文件上傳到Tomcat服務器,利用MultipartFile中的transferTo進行上傳

  public void upload(@RequestParam(value = "upload_file") MultipartFile file){
        String path = System.getProperty("catalina.home");
        path = path + "\\webapps\\upload";
        String fileName = file.getOriginalFilename();
        String extensionName = fileName.substring(fileName.lastIndexOf("." ) + 1);
        File dir = new File(path);
        if (!dir.exists()){
            dir.setWritable(true);
            dir.mkdirs();
        }
        File targetFile = new File(path,fileName);
        try {
            file.transferTo(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

4.然后在從Tomcat服務器上傳到VSFTPD服務器,這其中需要連接到FTP服務器然后進行上傳,需要先編寫FTP相關工具類
(1)FTP服務器的工具類

public class FTPUtil {

    private static String ip = "xx.xx.xx.xx";
    private static Integer port = 21;
    private static String userName = "xxxx";
    private static String password = "xxxx";
    private static FTPClient ftpClient;

    public static boolean connection(String ip,String userName,String password){
        boolean isSuccess = false;
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip);
            isSuccess = ftpClient.login(userName,password);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    public static boolean upload(List<File> fileList){
        boolean result = upload(fileList,"test");
        return result;
    }

    public static boolean upload(List<File> fileList,String path){
        boolean result = true;
        FileInputStream fis = null;
        if (connection(ip,userName,password)){
            try {
                ftpClient.changeWorkingDirectory(path);
                ftpClient.setBufferSize(1024);
                ftpClient.setControlEncoding("UTF-8");
                ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode();
                for (File item: fileList) {
                    fis = new FileInputStream(item);
                    ftpClient.storeFile(item.getName(),fis);
                }
            } catch (IOException e) {
                e.printStackTrace();
                result = false;
            }finally {
                try {
                    fis.close();
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

(2)進行上傳

//上傳到tomcat目錄下
file.transferTo(targetFile);
//接著上傳到FTP文件服務器上面
FTPUtil.upload(Lists.newArrayList(targetFile));
//最后刪除tomcat下的文件
targetFile.delete();
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容