java 分段上传文件servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2197645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multi part upload file servlet
提问by bhard
How can I upload files and get other paramaters of a form? I want to handle multi part requests in Java servlet.
如何上传文件并获取表单的其他参数?我想在 Java servlet 中处理多部分请求。
回答by BalusC
To browse and select a file for upload you need a <input type="file">field in the form. As stated in the HTML specificationyou need to use the POSTmethod and the enctype attribute of the form has to be set to multipart/form-data.
要浏览并选择要上传的文件,您需要<input type="file">在表单中填写一个字段。正如HTML 规范中所述,您需要使用该POST方法,并且表单的 enctype 属性必须设置为multipart/form-data.
<form action="uploadServlet" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" />
</form>
After submitting such a form the form data is available in multipart format in the HttpServletRequest#getInputStream(). For testing(!) purposes you can read the stream using the following snippet:
提交此类表单后,表单数据将在HttpServletRequest#getInputStream(). 出于测试(!)目的,您可以使用以下代码段读取流:
BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
You however need to parsethe stream byte by byte (instead of char by char). Prior to the fresh new Servlet 3.0API, the standard Servlet API didn't provide any builtin facilities to parse them. The normal form fields are also not available the usual request.getParameter()way, they are included in the multipart form data stream.
但是,您需要逐字节(而不是逐字符)解析流。在全新的Servlet 3.0API 之前,标准的 Servlet API 没有提供任何内置工具来解析它们。普通表单字段也不能以通常的request.getParameter()方式使用,它们包含在多部分表单数据流中。
If you're not on Servlet 3.0 yet (which is only bit less than 2 monts old), then you need to parse the stream yourself. Parsing such a stream requires precise background knowledge of how multipart form data requests are specified and structured. To create a perfect multipart parser you'll have to write a lot of code. But fortunately there's the Apache Commons FileUploadwhich has proven its robustness with years. Carefully read both the User Guideand Frequently Asked Questionsto find code examples and learn how to use it to an optimum degree (take MSIE into account!).
如果您还没有使用 Servlet 3.0(只有不到 2 个月的历史),那么您需要自己解析流。解析这样的流需要关于如何指定和构造多部分表单数据请求的精确背景知识。要创建完美的多部分解析器,您必须编写大量代码。但幸运的是,Apache Commons FileUpload多年来已经证明了它的健壮性。仔细阅读用户指南和常见问题解答以查找代码示例并学习如何以最佳方式使用它(将 MSIE 考虑在内!)。
回答by Justin Searls
Step 1
步骤1
Read adatapost's post.
阅读 adatapost 的帖子。
Step 2
第2步
Check out the Apache Commons FileUploadproject.
查看Apache Commons FileUpload项目。
There's a similarly workable solution by O'Reily, but its license of use requires you buy a book, and even that requirement is so poorly articulated that I won't benefit it with yet another link.
O'Reily 有一个类似可行的解决方案,但它的使用许可要求您购买一本书,即使该要求也没有明确表达,我不会再通过另一个链接从中受益。
回答by adatapost
Step-1
步骤1
set enctype form tag attribute.
设置 enctype 表单标签属性。
<form enctype="multipart/form-data" ....>
<input type="file" id="file1" name="file"/>
.... other stuff
</form>
Step-2
第2步
Read Justin's post.
阅读贾斯汀的帖子。
回答by Himanshu Vinchhi
To deal with enctype="multipart/form-data"we can not use request.getParameter() directly
处理enctype="multipart/form-data"我们不能直接使用 request.getParameter()
Now to deal with the problem
现在来处理问题
Now, for uploading a file to the server, there can be various ways. But, I am going to use MultipartRequest class provided by oreilly. For using this class you must have cos.jarfile.
现在,要将文件上传到服务器,可以有多种方式。但是,我将使用 oreilly 提供的 MultipartRequest 类。要使用这个类,你必须有cos.jar文件。
public class UploadServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
MultipartRequest m=new MultipartRequest(request,"d:/new");
out.print("successfully uploaded");
}
}
this will upload your file to d:/new
这会将您的文件上传到 d:/new
Now to retrive parameter of multipart requestyou have to use FilenameUtilsclass and getOriginalFileName()method of MultipartRequestclass.
现在要检索多部分请求的参数,您必须使用FilenameUtils类和类的getOriginalFileName()方法MultipartRequest。
String file = FilenameUtils.getName(req.getOriginalFileName("myfile"))+"\";
String message = req.getParameter("message");
回答by user2384271
This does not work for IE7 and below. Apparently you need to add another attribute to your form encoding ="multipart/form-data"
这不适用于 IE7 及以下。显然你需要在你的表单编码中添加另一个属性 ="multipart/form-data"

