从搭建简单的ftp服务器,到实现服务器文件上传下载,和解决上传中文文件,下载中文文件乱码。的随记。

1,Quick Easy Ftp Server服务器的简单配置

1,服务器的配置
在这里插入图片描述

2,用户界面的设置
在这里插入图片描述

3,启动服务器
在这里插入图片描述

4,简单的测试
在这里插入图片描述

这里的url是你本机的ip,该服务器会自动获取的。

2,文件上传

前端代码就不贴了,一个简单的from表单即可,跳转路径对口就好。

1,ftp工具类

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 = 0;
            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)) {
                            System.out.println("[失败]ftp创建目录:"+tempPath.toString());
                            return result;
                        } else {
                            ftp.changeWorkingDirectory(tempPath);
                            System.out.println("[成功]创建ftp目录:"+tempPath.toString());
                        }
                    }
                }
            }
            ftp.makeDirectory("/associated/a");
            //开通传输的端口权限
            ftp.enterLocalPassiveMode();
            //设置上传文件的类型为二进制类型
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.setControlEncoding("GBK");
            //上传文件
            if (!ftp.storeFile(new String(filename.getBytes("GBK"),"iso-8859-1"), input)) {
                return result;
            }
            input.close();
            ftp.logout();
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
        return result;
    }
    ```
我做的时候创建目录出问题了,暂时还没解决,所以最好把要上传到的目录 事先创建好。
如果那个大佬解决了,请帮帮可怜的孩子吧!

# 从ftp服务器下载文件

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.setFileType(FTPClient.BINARY_FILE_TYPE);
// ftp.setControlEncoding(“uft-8”);
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
String localFileName=new String(ff.getName().getBytes(“ISO-8859-1”),“GBK”);
if (localFileName.equals(fileName)) {
File localFile = new File(localPath + “/” + localFileName);
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) {
ioe.printStackTrace();
}
}
}
return result;
}
```

下载前端页面也很简单,一个简单的js(基本功)

String localFileName=new String(ff.getName().getBytes(“ISO-8859-1”),“GBK”);

这是一行神奇的代码,从服务器获取的中文文件会产生乱码,你ftp.setControlEncoding(“uft-8”);会出异常。这时候他就会出来拯救水深火热的你。

maven所需要的依赖

            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.0.3</version>
        </dependency>

        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.0.3</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值