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>```