I am trying to write and deploy a simple web-app at Google Cloud Platform. I installed gcloud and the corresponding libraries/plugins on Eclipse in my Mac. I use the following code to try to open a file specified in a HttpServeletRequest:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
resp.getWriter().println("<p>DEBUG: In Create2.</p>");
// get the file info
// to get the value of each input, always use the name and then
// req.getParameter(name);
String fileName = req.getParameter("file");
// create the file
File file = new File(fileName);
resp.getWriter().println("<p>"+file.getAbsolutePath()+"</p>");
if (file.exists()) {
...
}
else {
...
}
I put a test.txt file under project/src/main/webapp directory, run this project as an app engine and submit "test.txt" to servelet. But
In my local machine (localhost:8080), file.exists() always fails and the absolute path printed is
Applications/Eclipse/Contents/MacOS/test.txt
On the contrary, after I deploy the project to Google App Engine, everything works just fine and the absolute path printed is
/base/data/home/apps/XXXX/20170425t133741.400812218449586767/test.txt
I read a couple discussion threads all saying the file access in GAE/Google Cloud Platform is relative path. But why would my eclipse interpret it as an absolutely path when trying to open the file? Is there a fix on this issue so that I can always use relativ path both locally and on google cloud?
Thank you very much.
If you could set the working directory of the local App Engine dev server correctly, you wouldn't have problems using relative paths locally. Unfortunately, as of now, the Cloud Tools for Eclipse plugin doesn't support changing the working directory. Here is what you can do as a workaround:
Run the local dev server and check the server log in the Console view to see the actual web application root the server should use as a working directory. You'll see a log message like this:
WARNING: Your working directory, (<...your current, out-of-sync local server working directory...>) is not equal to your web application root (<...your workspace...>.metadata/.plugins/org.eclipse.wst.server.core/tmp0/<...your Eclipse project name...>)
Open the Servers view. Stop the server.
-Duser.dir=<...the directory of the web application root from the server log...>
in theVM arguments:
box. Click OK. (The Working directory section below doesn't work unfortunately.)From now on, you won't see the
WARNING
message, and the server will use the directory you provided as a working directory.UPDATE: This has been fixed in recent CT4E versions, which automatically set the working directory properly (unless you run multiple projects in a single local server). Update to the latest CT4E if you encounter this issue.