如何在Heroku上创建临时文件?问题尚未解决。

huangapple go评论65阅读模式
英文:

How do I make a temporary file on heroku? Not resolved problem

问题

我在Heroku上托管了一个应用。
它有一个简单的功能,可以将文件上传到FTP服务器。
在本地,它运行得很完美,
但是我在部署在Heroku上的同样的应用中遇到了问题。
在尝试保存文件时,它抛出一个错误。

实际上,它也确实保存了文件,但文件大小为0字节。
以下是代码部分:

public boolean fileUpload(MultipartFile file, String fileName) {
    try {
        FtpClient ftp = new FtpClient(ftpHost, ftpPort, ftpUser, ftpPassword);
        ftp.open();

        File tempFile = File.createTempFile(System.currentTimeMillis() + "tmp", "jpg");
        file.transferTo(tempFile);
        ftp.putFileToPath(tempFile, fileName);
        ftp.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

我认为问题可能出在这一行:

File tempFile = File.createTempFile(System.currentTimeMillis() + "tmp", "jpg");
file.transferTo(tempFile);

我已经根据StackOverflow上关于这个问题的所有问题进行了尝试,
但是我没有得到有效的答案。
除了日志记录之外,我在Heroku上没有添加任何插件。

以下是与该问题相关的日志:

Oct 08 01:53:04.527pm info app web.1 200 PORT command successful (xxx.xx.xx.xxx:xxxxx).
Oct 08 01:53:04.528pm info app web.1 STOR 1602157983845A2.jpg
Oct 08 01:53:04.636pm info app web.1 150 Opening BINARY mode data connection for '1602157983845A2.jpg'.
Oct 08 01:53:33.706pm error heroku router req=0a0bbc10… at=error code=H12 desc="Request timeout" 
method=POST path="/add-graphic" host=xxxxxxx.herokuapp.com request_id=xxxxxxxx-xxxx-xxxx-xxxx-
xxxxxxxxxxxx fwd="xx.xxx.xx.xxx" dyno=web.1 connect=0ms service=30889ms status=503 bytes=0 
protocol=https

经过进一步的问题研究,
似乎Heroku的dyno在这一行上卡住了:

ftp.putFileToPath(tempFile, fileName);

putFileToPath方法的实现如下:

public void putFileToPath(File file, String path) throws IOException {
    ftp.storeFile(path, new FileInputStream(file));
}
英文:

I have an app hosted on Heroku.<br>
It has a simple functionality to upload files on a ftp server.<br>
Locally it works perfectly, <br>
but I have a problem with the same app deployed on heroku.<br>
It throws an error when trying to save a file.<br>

Actually it also saves it, but the file's size is 0 bytes<br>
here is the code:<br>

    public boolean fileUpload(MultipartFile file, String fileName) {
    try {
        FtpClient ftp = new FtpClient(ftpHost, ftpPort, ftpUser, ftpPassword);
        ftp.open();

        File tempFile = File.createTempFile(System.currentTimeMillis() + &quot;tmp&quot;, &quot;jpg&quot;);
        file.transferTo(tempFile );
        ftp.putFileToPath(tempFile , fileName);
        ftp.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

I think that the problem is with this line:

File tempFile = File.createTempFile(System.currentTimeMillis() + &quot;tmp&quot;, &quot;jpg&quot;);
        file.transferTo(tempFile );

I have passed through all the questions according to the problem on StackOverflow<br>
but I didn't get the working answer<br>

I din't add any add-ons on heroku instead of logging.<br>

Here is a log from that problem:

> Oct 08 01:53:04.527pm info app web.1 200 PORT command successful (xxx.xx.xx.xxx:xxxxx).
>
> Oct 08 01:53:04.528pm info app web.1 STOR 1602157983845A2.jpg
>
> Oct 08 01:53:04.636pm info app web.1 150 Opening BINARY mode data connection for '1602157983845A2.jpg'.
>
> Oct 08 01:53:33.706pm error heroku router req=0a0bbc10… at=error code=H12 desc="Request timeout"
> method=POST path="/add-graphic" host=xxxxxxx.herokuapp.com request_id=xxxxxxxx-xxxx-xxxx-xxxx-
> xxxxxxxxxxxx fwd="xx.xxx.xx.xxx" dyno=web.1 connect=0ms service=30889ms status=503 bytes=0
> protocol=https

After some more rearch of the problem, <br>
it seems that heroku dyno stucks on that line:

ftp.putFileToPath(tempFile , fileName);

the methot putFileToPath looks like that:

public void putFileToPath(File file, String path) throws IOException {
    ftp.storeFile(path, new FileInputStream(file));
}

答案1

得分: 1

ftp.storeFile(path, new FileInputStream(file));

之前添加

ftp.enterLocalPassiveMode();

背景信息:https://stackoverflow.com/questions/19935068/ftp-timing-out-on-heroku

英文:

Add

ftp.enterLocalPassiveMode()

before

ftp.storeFile(path, new FileInputStream(file));

Background: https://stackoverflow.com/questions/19935068/ftp-timing-out-on-heroku

huangapple
  • 本文由 发表于 2020年10月8日 20:01:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64262107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定