美文网首页
上传文件到FTP服务器

上传文件到FTP服务器

作者: RaymondSH | 来源:发表于2020-06-11 23:43 被阅读0次

在做一个项目的时候需要将文件上传到FTP服务器上面,该项目使用Spring MVC,在上传的过程中使用到了Spring中的MultipartFile类,在此记录下:
(如何搭建VSFTPD服务器参见https://www.jianshu.com/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表单将会发送到upload.do接口中,其中文件会以二进制的形式进行传输
接下来在后台中写业务逻辑
首先是引入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配置文件中注册其实现类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();

相关文章

  • FTP上传文件脚本

    使用shell脚本,通过FTP上传文件到服务器:

  • FTP

    Menu 1.创建FTP服务器2.递归删除FTP服务器文件3.递归下载FTP服务器文件4.递归上传FTP服务器文件...

  • scp文件传输命令

    将文件从本地上传到服务器, 或者从服务器复制到本地, 更多的是使用ftp工具或者命令, 但ftp需要配置安装使用,...

  • python生成简单的FTP弱口令扫描

    前言 Ftp这个类实现了Ftp客户端的大多数功能,比如连接Ftp服务器、查看服务器中的文件、上传、下载文件等功能,...

  • 上传文件到FTP服务器

    在做一个项目的时候需要将文件上传到FTP服务器上面,该项目使用Spring MVC,在上传的过程中使用到了Spri...

  • FTP搭建

    什么是FTP ftp是文件传输协议,用于在网络上传输文件的协议,可以简单理解为ftp是一个服务器,我们可以把文件上...

  • 2018-07-03 Spring基础知识之ThreadLoca

    我写了个类用于上传文件到指定的FTP服务器,然而上线后,在多个任务并发调用后发现文件上传出错了。研究了问题之后,发...

  • ftp服务搭建与使用

    一 ftp服务器搭建 安装vsftpd软件(用于文件的上传和下载) 修改配置文件 重启服务 二 ftp客户端使用 ...

  • 阿里云服务器java连接ftp服务器时候530 Login in

    ftp服务器是:vsftpd的搭建的。 在使用ftp用户通过java代码上传文件的时候,本地正常的已上传到阿里云服...

  • Java 实现ftp文件上传

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件、下载文件,本文详细介绍如何利用FTPClie...

网友评论

      本文标题:上传文件到FTP服务器

      本文链接:https://www.haomeiwen.com/subject/cvyfphtx.html