一:接口API
@RequestMapping("/upload")
public R upload(@RequestParam("file") MultipartFile file) throws Exception {
if (file.isEmpty()) {
throw new EIException("上传文件不能为空");
}
InputStream inputStream = file.getInputStream();
String fileExt = file.getOriginalFilename();
FTPClient connectionFTP = ftpClientUtils.getConnectionFTP();
ftpClientUtils.uploadFile(connectionFTP,fileExt,inputStream);
return R.ok().put("file", "ok");
}
二:FTP工具类
package com.utils;
import org.apache.commons.net.ftp.*;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.UUID;
@Component
public class FtpClientUtils {
private String hostName = "139.2.2.1";
private int port = 21;
private String userName = "root";
private String passWord = "root123";
private String path = "www/wwwroot/wenjian";
public FTPClient getConnectionFTP() {
FTPClient ftp = new FTPClient();
try {
ftp.connect(hostName, port);
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
ftp.login(userName, passWord);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
return ftp;
}
public boolean closeFTP(FTPClient ftp) {
if (ftp.isConnected()) {
try {
ftp.disconnect();
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
public String uploadFile(FTPClient ftp, String fileName, InputStream inputStream) {
try {
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(path);
FTPFile[] fs = ftp.listFiles();
fileName = FtpClientUtils.isFileExist(fileName, fs);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
boolean boo = ftp.storeFile(fileName, inputStream);
if (boo){
return fileName;
}
inputStream.close();
ftp.logout();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean deleteFile(FTPClient ftp, String fileName) {
boolean success = false;
try {
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(path);
success = ftp.deleteFile(fileName);
ftp.logout();
} catch (Exception e) {
e.printStackTrace();
}
return success;
}
public boolean downFile(FTPClient ftp, String path, String fileName, String localPath) {
boolean success = false;
try {
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory(path);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "\\" + ff.getName());
OutputStream outputStream = new FileOutputStream(localFile);
ftp.retrieveFile(new String(ff.getName().getBytes("GBK"), "ISO-8859-1"), outputStream);
outputStream.flush();
outputStream.close();
}
}
ftp.logout();
} catch (Exception e) {
e.printStackTrace();
}
return success;
}
public InputStream downFile(FTPClient ftp,String fileName) {
ftp.enterLocalPassiveMode();
try {
ftp.changeWorkingDirectory(path);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
return ftp.retrieveFileStream(ff.getName());
}
}
ftp.logout();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String isFileExist(String fileName, FTPFile[] fs) {
for (FTPFile ftpFile : fs) {
if (ftpFile.getName().equals(fileName)) {
return UUID.randomUUID().toString()+ fileName.substring(fileName.indexOf("."));
}
}
return fileName;
}
}

- 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
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224