通过 HttpServlet 即时处理输入流到输出流

发布于 2024-10-27 21:33:27 字数 1577 浏览 4 评论 0原文

我正在尝试通过 HttpServlet (tomcat) 处理大型文本文件。

由于此文件可能很大并且过程应该非常快,因此我不想将文件上传到服务器上,并且我使用了方法 HttpRequest.getInputStream 来动态处理输入。例如,我想使用下面的代码将输入转换为大写:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class EchoServlet extends HttpServlet
    {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
        OutputStream out=null;
        InputStream in=req.getInputStream();
        if(in==null) throw new IOException("input is null");
        try
            {
            resp.setContentType("text/plain");
            out=resp.getOutputStream();
            int c;
            while((c=in.read())!=-1)
                {
                out.write((char)Character.toUpperCase(c));
                }
            }
        catch(IOException err)
            {
            //ignore
            }
        finally
            {
            if(out!=null) out.flush();
            if(out!=null) out.close();
            in.close();
            }
        }
    }

我使用 CURL 调用我的 servlet:

curl -s -d @big.file.txt  "http://localhost:8080/test/toupper"

1)通过 servlet 动态处理输入,这是一个好的/常见的做法吗?

2)我的代码似乎删除了回车符 ('\n') 。为什么 ?

谢谢

I'm trying to process a large text file through a HttpServlet (tomcat).

As this file can be large and the process should be very fast, I don't want to upload the file on the server and I've used the method HttpRequest.getInputStream to process the input on the fly. For example, I want to transform the input to upper-case with the code below:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class EchoServlet extends HttpServlet
    {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
        {
        OutputStream out=null;
        InputStream in=req.getInputStream();
        if(in==null) throw new IOException("input is null");
        try
            {
            resp.setContentType("text/plain");
            out=resp.getOutputStream();
            int c;
            while((c=in.read())!=-1)
                {
                out.write((char)Character.toUpperCase(c));
                }
            }
        catch(IOException err)
            {
            //ignore
            }
        finally
            {
            if(out!=null) out.flush();
            if(out!=null) out.close();
            in.close();
            }
        }
    }

I invoked my servlet with CURL:

curl -s -d @big.file.txt  "http://localhost:8080/test/toupper"

1) processing the input on the fly through a servlet, is it a good/common practice ?

2) my code seems to remove the carriage return ('\n') . Why ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放赐 2024-11-03 21:33:27

1)通过 servlet 动态处理输入,这是一种好的/常见的做法吗?

取决于功能需求。我个人会使用一个接受 HTTP multipart/form-data 请求而不是原始请求主体的 servlet。这样它就可以在普通的 HTML 表单上重用。

2) 我的代码似乎删除了回车符 ('\n') 。为什么?

给定的代码示例没有这样做。也许您过于简单化了,您最初使用的是 BufferedReader#readLine(),而不是使用 PrintWriter#println()。 readLine() 确实会吃掉 CRLF。

给定的代码片段中也存在更多问题/效率低下,但我无法对此给出合适的答案,因为您似乎没有实际上运行您在问题中发布的代码。

1) processing the input on the fly through a servlet, is it a good/common practice ?

Depends on the functional requirement. I would personally have used a servlet which accepts HTTP multipart/form-data requests instead of raw request bodies. This way it's reuseable on normal HTML forms.

2) my code seems to remove the carriage return ('\n') . Why ?

The given code example doesn't do that. Maybe you've oversimplified it and you was originally using BufferedReader#readLine() while not using PrintWriter#println(). The readLine() indeed eats CRLFs.

There are more issues/inefficiencies in the given code snippet as well, but I can't give a decent answer on that as you seem not to actually be running the code as you posted in the question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文