1、前端代码

1.1、请求参数添加以下代码
responseType: 'blob'
1.2、请求后端代码,返回结果的处理
downloadFtp('\/wangzhan\/Snipaste_2022-07-26_14-04-58.png').then((response) => {
let blob = new Blob([response]);
const downUrl = URL.createObjectURL(blob);
const link = document.createElement("a");
link.style.display = "none";
link.href = downUrl;
link.target = "_blank";
link.download = "Snipaste_2022-07-26_14-04-58.png";
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(downUrl);
})
2、后端代码
2.1、工具类 - FTPUtils
package com.evimage.web.evgis.ftp;
import com.evimage.common.utils.uuid.UUID;
import lombok.Data;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.charset.StandardCharsets;
@Component
@Data
public class FTPUtils {
@Value("${ftp.host}")
private String host;
@Value("${ftp.port}")
private Integer port;
@Value("${ftp.username}")
private String username;
@Value("${ftp.password}")
private String password;
private FTPClient ftpClient = null;
public void getFtpConnect(){
ftpClient = new FTPClient();
try {
ftpClient.setControlEncoding("GBK");
ftpClient.connect(host, port);
ftpClient.login(username, password);
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
closeConnect();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void closeConnect() {
try {
ftpClient.logout();
if (ftpClient.isConnected()) {
ftpClient.disconnect();
}
this.ftpClient = null;
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isDirExist(String path){
boolean flag = false;
try {
flag = ftpClient.changeWorkingDirectory(path);
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public void mkdirAndChangeWorkingDirectory(String path){
for (String str : path.split("/")) {
if(StringUtils.isBlank(str)) {
continue;
}
try {
if (!ftpClient.changeWorkingDirectory(str)) {
ftpClient.makeDirectory(str);
ftpClient.changeWorkingDirectory(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public File download(String path) throws IOException {
InputStream inputStream = null;
File file = new File(String.valueOf(UUID.randomUUID()));
try {
ftpClient.setControlEncoding(StandardCharsets.UTF_8.name());
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
inputStream = ftpClient.retrieveFileStream(path);
FileUtils.copyInputStreamToFile(inputStream, file);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
public String upload(MultipartFile file, String path, String fileName){
try {
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
boolean flag = ftpClient.storeFile(fileName, file.getInputStream());
if (!flag){
throw new RuntimeException("FTP上传失败!");
}else {
System.out.println(fileName+"上传成功");
}
} catch (IOException e) {
e.printStackTrace();
}
return path + "/" + fileName;
}
}
2.2、工具类 - HttpsUtils
package com.evimage.web.evgis.ftp;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
public class HttpsUtils {
public static void HttpsDownload(HttpServletResponse response, FileInputStream fileInputStream, String fileName){
try{
String[] split = fileName.split("/");
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(split[split.length - 1], "UTF-8"));
response.addHeader("Content-Length", ""+fileInputStream.available());
response.addHeader("Access-Control-Allow-Origin", "*");
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream;charset=UTF-8");
byte[] buffer = new byte[2048];
int bytesRead = -1;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileInputStream.close();
outputStream.write(buffer);
outputStream.flush();
}catch (IOException e){
e.printStackTrace();
}
}
public static void HttpsReview(HttpServletResponse response, InputStream inputStream,String filename){
try{
response.reset();
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.addHeader("Content-Length", "" + inputStream.available());
response.addHeader("Access-Control-Allow-Origin", "*");
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
byte[] buffer = new byte[2048];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
}catch (IOException e){
e.printStackTrace();
}
}
}
2.3、配置文件 - application.yml

ftp:
host:
port: 21
username: evFTP
password: evFTP
2.4、controller代码

@Autowired
private FTPUtils ftpUtils;
@PostMapping("downloadFtp")
public void downloadFtp(@RequestParam("fileName") String fileName, HttpServletResponse response){
try {
ftpUtils.getFtpConnect();
File file = ftpUtils.download(fileName);
FileInputStream fileInputStream = new FileInputStream(file);
HttpsUtils.HttpsDownload(response, fileInputStream, fileName);
fileInputStream.close();
file.delete();
ftpUtils.closeConnect();
}catch (Exception e){
System.out.println("异常信息:" + e.getMessage());
}
}