想要创建一个servlet,它将基于guid文件名将发布的数据保存到文件中 [英] Want to create a servlet that will save the posted data to a file based on a guid filename

查看:75
本文介绍了想要创建一个servlet,它将基于guid文件名将发布的数据保存到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我将Java应用程序推送到服务器上,对此感到非常兴奋.

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

现在我要测试一些东西,如何将发布到servlet的数据保存到文件中,文件名应该是唯一的guid.

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.

我到目前为止有这个:

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!");


    }
}

因此,假设将http发布的数据(例如约50K)发布到有效载荷"字段中,我该如何获取发布的文本并将其保存到文件中,文件名是GUID.

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.

java是否具有清理打开文件的结构,例如在c#中:

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

这将关闭连接并清除内存等.

That closes the connection and cleans up memory etc.

另外,我需要为tomcat设置特殊权限来保存此文件吗?

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

我现在默认情况下是使用安装了tomcat6的ubuntu 11进行设置(只是在VPS上玩).

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

谢谢.

推荐答案

您可以使用request来读取有效载荷",请参见

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

request.getParameter("payload");

您可以使用File创建文件,请参见

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();

对于GUID,您会看到以下在Java中创建GUID

For GUID you see this Create a GUID in Java

为了进行清理,您不必担心在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.

但是您应该关闭像out.close这样的资源,以在完成后将其释放回系统.

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

另外,我需要为tomcat设置特殊权限来保存此文件吗?

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

您不需要这样做,因为tomcat只是一台服务器,它与文件系统(OS)更相关.我在Unix上使用Glassfish,不需要做任何类似的事情来创建文件.

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.

这篇关于想要创建一个servlet,它将基于guid文件名将发布的数据保存到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆