损坏的图像上传在CPANEL中,有404错误

发布于 2025-02-13 12:34:33 字数 2243 浏览 0 评论 0原文

我在春季和角度技术的生产环境中将图像上传到FTP服务器(使用CPANEL)时有问题。 将文件上传到指定的文件夹TMP,但上传的图像已损坏,并且无法使用此错误404(找不到)显示。 当我从CPANEL手动上传相同的图像时,它可以很好地工作。 请任何命题:) 这是源代码:

public类ftpservice {

static FTPClient ftp = new FTPClient();
static String TMP_UPLOAD_FOLDER = "/home/myproject/public_html/tmp/";
static String SERVER_DOMAIN = " XX.XXX.XX.XX";
static String SERVER_USERNAME = "username1";
static String SERVER_PASSWORD = "myPassword";

public static String uFileUpload(MultipartFile file, String TypePhoto, Long clientId) throws IOException {
    if (file.isEmpty()) {
        System.out.println("Empty File");
        return "Empty File";
    } else {
        // dossier TMP
        File f1 = new File(TMP_UPLOAD_FOLDER);
        boolean bool = f1.mkdir();
        byte[] bytes = file.getBytes();
        Path path = Paths.get(TMP_UPLOAD_FOLDER + file.getOriginalFilename());
        System.out.println(path.toString());
        Files.write(path, bytes);
        System.out.println("File successfully uploaded to local storage : " + file.getOriginalFilename());
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        int reply;
        ftp.connect(SERVER_DOMAIN);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.out.println("Exception in connecting to FTP Server");
        }
        ftp.login(SERVER_USERNAME, SERVER_PASSWORD);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        try {
            InputStream input = new FileInputStream(new File(TMP_UPLOAD_FOLDER + file.getOriginalFilename()));
            System.out.println(input);
            ftp.storeFile("/home/myproject/public_html/tmp/" + file.getOriginalFilename().toString()+"", input);
            ftp.logout();
            ftp.disconnect();
            System.out.println("File Uploaded !");
            input.close();
            Files.delete(path);
            System.out.println("File deleted");
        } catch (Exception e) {
            System.out.println("Error uploading file to remote server");
        }
    }
    return "";
}

I have a problem while uploading an image in an FTP server (using Cpanel) in production env with spring and angular technology.
The file is uploaded to the specified folder tmp but the image uploaded is corrupted and cannot be shown with this error 404 (Not Found).
When i upload the same image manually from cPanel, it works perfectly.
Any propositions please :)
Here is the source code :

public class FTPService {

static FTPClient ftp = new FTPClient();
static String TMP_UPLOAD_FOLDER = "/home/myproject/public_html/tmp/";
static String SERVER_DOMAIN = " XX.XXX.XX.XX";
static String SERVER_USERNAME = "username1";
static String SERVER_PASSWORD = "myPassword";

public static String uFileUpload(MultipartFile file, String TypePhoto, Long clientId) throws IOException {
    if (file.isEmpty()) {
        System.out.println("Empty File");
        return "Empty File";
    } else {
        // dossier TMP
        File f1 = new File(TMP_UPLOAD_FOLDER);
        boolean bool = f1.mkdir();
        byte[] bytes = file.getBytes();
        Path path = Paths.get(TMP_UPLOAD_FOLDER + file.getOriginalFilename());
        System.out.println(path.toString());
        Files.write(path, bytes);
        System.out.println("File successfully uploaded to local storage : " + file.getOriginalFilename());
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        int reply;
        ftp.connect(SERVER_DOMAIN);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            System.out.println("Exception in connecting to FTP Server");
        }
        ftp.login(SERVER_USERNAME, SERVER_PASSWORD);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        try {
            InputStream input = new FileInputStream(new File(TMP_UPLOAD_FOLDER + file.getOriginalFilename()));
            System.out.println(input);
            ftp.storeFile("/home/myproject/public_html/tmp/" + file.getOriginalFilename().toString()+"", input);
            ftp.logout();
            ftp.disconnect();
            System.out.println("File Uploaded !");
            input.close();
            Files.delete(path);
            System.out.println("File deleted");
        } catch (Exception e) {
            System.out.println("Error uploading file to remote server");
        }
    }
    return "";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文