Hello i have a probleme with my spring boot api. That's work fine in local for getfile in my external FTP. But when i pull it on Docker (vps debian) i have a 200 response with no data and upload return false.
my code:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@CrossOrigin
@RestController
public class FileController {
FtpUtil ftpUtil;
@Scheduled(fixedDelay = 5000)
@PostMapping("/api/uploadfile")
public boolean uploadFile(@RequestParam(value ="file") MultipartFile file) throws IOException {
InputStream inputStream = file.getInputStream();
return ftpUtil.uploadFile("ftp.host.fr",21,"user","password","./assets/images/jej", "/",file.getOriginalFilename(), inputStream);
}
@Scheduled(fixedDelay = 5000)
@GetMapping("/api/getfile")
public List<String> getFileList() {
return ftpUtil.udownLoadFile("host",21,"user","pass","./assets/images/jej");
}
}
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class FtpUtil {
public static boolean uploadFile(String host, int port, String username, String password,
String basePath, String filePath, String filename, InputStream input) {
FTPClient ftp = new FTPClient();
Boolean result = false;
try {
ftp.connect(host, port);
ftp.login(username, password);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
String path = basePath + filePath;
if (!ftp.changeWorkingDirectory(path)) {
String[] dirs = path.split("/");
StringBuffer tempPath = new StringBuffer();
for (String dir : dirs) {
if (null == dir || "".equals(dir)) {
continue;
}
tempPath.append("/").append(dir);
if (!ftp.changeWorkingDirectory(tempPath.toString())) {
if (!ftp.makeDirectory(tempPath.toString())) {
return result;
} else {
ftp.changeWorkingDirectory(tempPath.toString());
}
}
}
}
ftp.setFileType(FTP.BINARY_FILE_TYPE);
if (!ftp.storeFile(filename, input)) {
return result;
}
input.close();
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
public static List<String> udownLoadFile(String host, int port, String username, String password,
String remotePath) {
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
ftp.changeWorkingDirectory(remotePath);
FTPFile[] fs = ftp.listFiles();
return Arrays.stream(fs)
.map(FTPFile::getName)
.collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return null;
}
and my docker run:
docker container run --network jejnet --name je2 -p 8080:8080 -v slaateejej-volume -d slaatee/jej:trya