0

So I pushed my java app to a server, pretty excited about that.

Now I want to test something, how can I save the posted data to my servlet to a file, and the filename should be a unique guid.

I have this so far:

public class TestServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException, IOException {

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException, IOException {

        PrintWriter printWriter = response.getWriter();

        printWriter.print("hello, world from testservlet!");


    }
}

So assuming the http posted data (say around 50K) will be posted to the field 'payload', how can I grab the posted text, and save it to a file, with the filename being a GUID.

Does java have a construct to clean up an open file, like in c#:

using(var file = new ....)
{
  // write to file
}

That closes the connection and cleans up memory etc.

Also, do I need to set special permissions for tomcat to save this file?

I just set things up by default right now (just playing around on a VPS) using ubuntu 11, installed tomcat6.

Thanks.

Blankman
  • 259,732
  • 324
  • 769
  • 1,199

3 Answers3

5

You can user request to read the "payload", see the API doc for ServletRequest:

request.getParameter("payload");

You can use File to create the file, see AP doc:

File newFile = new File("fileName");
boolean isCreated = newfile.createNewFile();

You can write to the file as follows,

BufferedWriter out = new BufferedWriter(new FileWriter(newFile));
out.write(payLoad);
out.close();

For GUID you see this Create a GUID in Java

And for the clean up, you don't have to worry about it in Java, it's Garbage Collector ( What is the garbage collector in Java? ) does it for you automatically when the reference goes out of scope.

But you should close the resources like out.close to release it back to the system when you are done with it.

Also, do I need to set special permissions for tomcat to save this file?

You do not need to do that because tomcat is just a server, it's more related to the file system (OS). I use Glassfish on Unix and I don't need to do anything like that to create file.

Community
  • 1
  • 1
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
1

Now I want to test something, how can I save the posted data to my servlet to a file, and the filename should be a unique guid.

Use File#createTempFile() to create a file with an unique ID in the given folder.

File file = File.createTempfile("prefix-", ".ext", "/path/to/files");
// ...

See also:


Does java have a construct to clean up an open file, like in c#: using?

Only in Java 7 which is already been out for some time.

try (FileWriter writer = new FileWriter(file)) {
    writer.write(content);
}

which is equivalent to

FileWriter writer = null;
try {
    writer = new FileWriter(file);
    writer.write(content);
} finally {
    if (writer != null) writer.close();
}

See also:


Also, do I need to set special permissions for tomcat to save this file?

The user who has started Tomcat should indeed have the file write permissions on the given directory.


In the future please ask separate questions in separate SO questions.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Java 7 has a new try with resources construct that will take care of closing the file for you. Otherwise... just close the file; no big deal.

As far as "special permissions", as long as the user Tomcat is running under can access the directory in question, there's no issue. I'd recommend against storing it under the webapp directories, though (and if it's deployed as a war you may not be able to anyway). Keep uploaded files in a known, but separate, directory.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302