红宝石 :上传的文件突然在 heroku 中数小时内找不到



我有以下 Ruby Sinatra 的邮政编码来上传图像文件:

post "/upload" do 
  File.open("public/uploads/" + params["image"][:filename], "wb") do |f|
    f.write(params["image"][:tempfile].read)
  end
end

以及以下Java代码将图像文件上传到 example.com/upload:

private static String boundary;
private static final String LINE_FEED = "rn";
private static HttpURLConnection httpConn;
private static OutputStream outputStream;
private static PrintWriter writer;
public static void upload(String requestURL, String fieldName, File
uploadFile) throws IOException {
    boundary = "===" + System.currentTimeMillis() + "===";
    URL url = new URL(requestURL);
    httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setUseCaches(false);
    httpConn.setDoOutput(true);
    httpConn.setDoInput(true);
    httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    outputStream = httpConn.getOutputStream();
    writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
    String fileName = uploadFile.getName();
    writer.append("--" + boundary).append(LINE_FEED);
    writer.append("Content-Disposition: form-data; name="" + fieldName + ""; filename="" + fileName + """).append(LINE_FEED);
    writer.append("Content-Type: "+ URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
    writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
    writer.append(LINE_FEED).flush();
    FileInputStream inputStream = new FileInputStream(uploadFile);
    byte[] buffer = new byte[4096];
    int bytesRead = -1;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
    outputStream.flush();
    inputStream.close();
    writer.append("--" + boundary + "--").append(LINE_FEED);
    writer.close();
    httpConn.getInputStream();
    httpConn.disconnect();
}
File uploadFile = new File("C:/myimage.png");
upload("http://www.example.com/upload", "image", uploadFile);

我的网站托管在 heroku .

调用 upload() 后,图像文件上传成功,我可以在 example.com/upload/myimage.png 中访问它

但问题是:几个小时后,当我检查网址查看myimage.png时,出现"未找到"错误(heroku日志中的404错误)

有什么想法吗?

对不起,我的英语不好:|

你不应该将文件存储到 heroku 的本地文件系统中。从他们的文档中:

临时文件系统

每个测功机都有自己的临时文件系统,具有 最近部署的代码的新副本。在测功机期间 其运行的进程可以将文件系统用作临时生存期 暂存器,但写入的文件对 中的进程不可见 任何其他测功机和写入的任何文件都将在 测功机已停止或重新启动。

建议不要在本地存储文件,而是将文件上传到 AWS S3 或其他云存储系统。

最新更新