java html 多部分表单中输入文本字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5730532/
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
values of input text fields in a html multipart form
提问by aurel
I use Apache Commons FileUpload in a java server-side app that has a html form with fields :
我在 Java 服务器端应用程序中使用 Apache Commons FileUpload,该应用程序具有带有字段的 html 表单:
a destination fied that will be filled with email address of the destination mailbox
a message text with a message of the sender
- a
<input type=file ...
field for uploading a photo. I can receive uploaded file (as a stream) but how I can access 1) and 2) form values (completed by the user of app)? Many thanks, Aurel
一个目标字段,将填充目标邮箱的电子邮件地址
带有发件人消息的消息文本
<input type=file ...
用于上传照片的字段。我可以接收上传的文件(作为流),但如何访问 1)和 2)表单值(由应用程序用户完成)?非常感谢,奥雷尔
回答by pierrovic
I am guessing you are using a FileItemIterator to iterate the items in the request. The iterators next() method returns a FileItemStream (not a FileItem). Open the stream on that object and turn it into a string like this:
我猜您正在使用 FileItemIterator 来迭代请求中的项目。迭代器 next() 方法返回一个 FileItemStream(不是 FileItem)。打开该对象上的流并将其转换为这样的字符串:
import org.apache.commons.fileupload.util.Streams;
...
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
String name = item.getFieldName();
String value = Streams.asString(stream);
The getString method suggested by other answers is a method on the FileItem interface.
其他答案建议的 getString 方法是 FileItem 接口上的一个方法。
回答by BalusC
You can receive them using the same API. Just hook on when FileItem#isFormField()
returns true
. If it returns false
then it's an uploaded file as you probably already are using.
您可以使用相同的 API 接收它们。FileItem#isFormField()
返回时挂上即可true
。如果它返回,false
那么它是一个上传的文件,因为您可能已经在使用。
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select, etc).
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
// ... (do your job here)
} else {
// Process form file field (input type="file").
String fieldname = item.getFieldName();
String filename = FilenameUtils.getName(item.getName());
InputStream filecontent = item.getInputStream();
// ... (do your job here)
}
}
} catch (FileUploadException e) {
throw new ServletException("Cannot parse multipart request.", e);
}
// ...
}
回答by Ibolit
Here's what I am using for this purpose:
这是我为此目的使用的内容:
public static Hashtable getParamsFromMultipartForm(HttpServletRequest req) throws FileUploadException {
Hashtable ret = new Hashtable();
List items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(req);
for (FileItem item : items) {
if (item.isFormField()) {
ret.put(item.getFieldName(), item.getString());
}
}
return ret;
}
And then, whenever i need the value of any of my params, i just write, say:
然后,每当我需要我的任何参数的值时,我就写,说:
//at the beginning of a servlet
Hashtable multipartParams = TheClassWhereIPutThatMethod.getParamsFromMultipartForm(req);
String myParamFromForm = multipartParams.get("myParamFromForm");
回答by Yash Bansal
So, what I did is to use the instance of fileItem as in:
所以,我所做的是使用 fileItem 的实例,如下所示:
Hashtable incoming = new Hashtable();
fileName = sfu.parseRequest(request);
//iterating over each uploaded file and storing the values of different parameters in the HashTable (incoming)
for(FileItem f:fileName)
{
incoming.put(f.getFieldName(), f.getString());
}
//utilizing that HashTable and getting the value of desired field in the below manner, as in my case i required the value of "permissions" from the jsp page
for(FileItem f:fileName)
{
String role= (String)incoming.get("permission"); //as it is a multipart form request, so need to get using this
}
Thanks
谢谢