Java Ruby:在heroku的几个小时内,上传的文件突然变得找不到了
我有以下Ruby Sinatra上传图像文件的邮政编码:Java Ruby:在heroku的几个小时内,上传的文件突然变得找不到了,java,ruby,heroku,sinatra,Java,Ruby,Heroku,Sinatra,我有以下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; priv
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 = "\r\n";
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中访问它
但问题是:几个小时后,当我检查url以查看myimage.png时,出现了“未找到”错误(heroku日志中的404错误)
有什么想法吗
对不起,我的英语不好:|您不应该将文件存储到heroku的本地文件系统。从他们的: 临时文件系统 每个dyno都有自己的临时文件系统,具有 最近部署的代码的新副本。在dyno的 其运行进程可以将文件系统用作临时文件系统 草稿行,但中的进程看不到写入的文件 任何其他dyno和写入的任何文件都将在 dyno停止或重新启动
建议将文件上载到AWS S3或其他云存储系统,而不是本地存储文件。您不应将文件存储到heroku的本地文件系统。从他们的: 临时文件系统 每个dyno都有自己的临时文件系统,具有 最近部署的代码的新副本。在dyno的 其运行进程可以将文件系统用作临时文件系统 草稿行,但中的进程看不到写入的文件 任何其他dyno和写入的任何文件都将在 dyno停止或重新启动
建议将文件上载到AWS S3或其他云存储系统,而不是本地存储文件。谢谢您的帮助:)谢谢您的帮助:)