简体   繁体   English

使用servlet将文件上传到Openshift服务器

[英]Uploading file to Openshift server with servlet

I'm not sure if this question has been asked before, but I could not find any resources on the internet answering my specific problem. 我不确定是否曾经问过这个问题,但是我在互联网上找不到任何资源可以回答我的特定问题。

I am trying to upload a file from an Android app to my Openshift server/gear, where it will be stored. 我正在尝试将文件从Android应用程序上传到我的Openshift服务器/齿轮,将在此存储文件。 However, the issue I am facing is that whilst the file is being created at the Openshift side (I have checked using FTP), no data is being written to it. 但是,我面临的问题是,虽然文件是在Openshift端创建的(我已经使用FTP进行过检查),但没有数据写入其中。

The code snippet from the servlet that writes the data to the file is here: 来自servlet的将数据写入文件的代码片段在这里:

int BUFFER_LENGTH = 4096;
DataInputStream din = new DataInputStream(req.getInputStream());
String fileName = din.readUTF();
String path = System.getenv("OPENSHIFT_DATA_DIR") + "/uploads/" + fileName + ".txt";
File f = new File(path);
FileOutputStream fos = new FileOutputStream(f);

byte[] buffer = new byte[BUFFER_LENGTH];
int length = 0;
while ((length = din.read(buffer, 0, BUFFER_LENGTH)) != -1) {
    fos.write(buffer, 0, length);
}

fos.close();
din.close();

It all seems to be correct, to me at least, and it worked when I tested it on a local tomcat server. 至少对我而言,这一切似乎都是正确的,并且当我在本地tomcat服务器上对其进行测试时,它可以正常工作。 For some reason, however, it doesn't work with Openshift, so there must be something I am missing. 但是由于某种原因,它不适用于Openshift,因此必定有我缺少的东西。

Luckily there is a help center article for just this issue: https://forums.openshift.com/how-to-upload-and-serve-files-using-java-servlets-on-openshift 幸运的是,有一个针对此问题的帮助中心文章: https : //forums.openshift.com/how-to-upload-and-serve-files-using-java-servlets-on-openshift

It details the code for both uploading, and serving files on OpenShift via a Java Servlet, using the openshift data directory 它详细说明了使用openshift数据目录通过Java Servlet在OpenShift上上传和提供文件的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM