使用多部分发送附加数据 [英] Sending additional data with multipart
问题描述
我正在使用 apache-commons-fileupload
从 client
获取文件到 server
.(使用 JSP
和 Servlet
).
I am using apache-commons-fileupload
to get file from client
to the server
.(using JSP
and Servlet
).
JSP/HTML
<form method="POST" action="GetFile" enctype="multipart/form-data">
<input type="file" name="datafile">
<input type="text" name="text1">
<input type="submit" value="Next">
</form>
Servlet:获取文件
System.out.println(request.getParameter("text1"));
我能够将文件上传到服务器,但我无法在 servlet
中获取 text1
的值(我得到 null
值),我需要表单中的这个 servlet
中 text1
的 textfield
来提交一些额外的信息,同时将它上传到服务器
.
I am able to upload the file to the server, but I am not able to get the value of text1
in the servlet
(I am getting null
value of text1
in the servlet
), I need this textfield
in the form to submit some additional information while uploading it to the server
.
- Is
enctype="multipart/form-data"
表单选项不允许其他表单数据要提交?如果它不允许,那么我必须将这个额外的textfield
发送到server
的其他选项是什么. - 或者我的代码还有其他问题吗?
- Is
enctype="multipart/form-data"
option of form doesn't allow other form data to be submited? if it doesn't allow it then what are the other options I have to send this additionaltextfield
to theserver
. - Or is there any other problem in my code?
推荐答案
Is
enctype="multipart/form-data"
表单选项不允许其他表单数据要提交?如果它不允许,那么是什么其他选项我必须将此附加文本字段发送到服务器.
Is
enctype="multipart/form-data"
option of form doesn't allow other form data to be submited? if it doesn't allow it then what are the other options I have to send this additional textfield to the server.
不,使用 enctype="multipart/form-data"
没有问题.您可以获取其他字段,然后以这种形式提交.
No there is no issue with using enctype="multipart/form-data"
. You can get other fields then file in such form.
或者我的代码还有其他问题吗?
Or is there any other problem in my code?
是的,就目前而言.在使用 enctype="multipart/form-data"
时,您不能通过使用 request.getParameter(name);
直接获取参数.使用它时,表单字段不能作为请求的参数使用,它们包含在流中,因此您无法以正常方式获取它.您可以在 使用 commons-fileupload 的文档中找到一种方法#处理上传的项目.
Yes, as for now. While using enctype="multipart/form-data"
you can not directly get parameters by using request.getParameter(name);
. While using it, form fields aren't available as parameter of the request, they are included in the stream, so you can not get it the normal way. You can find a way to do this in the docs of using commons-fileupload#Processing the uploaded items.
这篇关于使用多部分发送附加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!