通过 HttpServlet 即时处理输入流到输出流
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取决于功能需求。我个人会使用一个接受 HTTP
multipart/form-data
请求而不是原始请求主体的 servlet。这样它就可以在普通的 HTML 表单上重用。给定的代码示例没有这样做。也许您过于简单化了,您最初使用的是 BufferedReader#readLine(),而不是使用 PrintWriter#println()。 readLine() 确实会吃掉 CRLF。
给定的代码片段中也存在更多问题/效率低下,但我无法对此给出合适的答案,因为您似乎没有实际上运行您在问题中发布的代码。
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.The given code example doesn't do that. Maybe you've oversimplified it and you was originally using
BufferedReader#readLine()
while not usingPrintWriter#println()
. ThereadLine()
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.