Html 使用multipart/form-data时如何从请求中获取字符串参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10379013/
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
how to get string parameter from request when using multipart/form-data?
提问by Hanaa
i make html page to upload image with text box which has description form it . i have use multipart/form-data ; in the dopost at servlet i get file using ServletFileUpload upload = new ServletFileUpload();
我制作了 html 页面来上传带有描述形式的文本框的图像。我使用了 multipart/form-data ;在 servlet 的 dopost 中,我使用 ServletFileUpload upload = new ServletFileUpload(); 获取文件。
to get string parameter i used request.getparameter(); but it always give me NULL ??? HOw can i get it ??
获取字符串参数我使用 request.getparameter(); 但它总是给我 NULL ???我怎么才能得到它 ??
html ::
html ::
<form name="filesForm" action="/upload" method="post" enctype="multipart/form-data">
File : <input type="file" name="file">
<textarea name = "description"
rows = "4" cols = "30">Enter comments here.</textarea>
<input type="submit" name="Submit" value="Upload File">
at servlet :
在 servlet :
ServletFileUpload upload = new ServletFileUpload();
upload.setSizeMax(500000000);
FileItemIterator iterator = null;
try {
iterator = upload.getItemIterator(req);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // to handle contents or
// instances of request.
FileItemStream item = null;
try {
item = iterator.next();
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // get access each item on iterator.
java.io.InputStream in = item.openStream(); // allows to read the items contents.
Blob imageBlob = new Blob(IOUtils.toByteArray(in)); // store each item on Blob
// object after converting them to Bytes.
/*……….
Note: we are concerned on uploading images files. So the type of files is either (.jpg or gif)
……….
*/
PersistenceManager pm = PMF.get().getPersistenceManager();
String counter="1";
ServletOutputStream outt = resp.getOutputStream();
//match incoming request Content type and invoke appropriate method for handel request
回答by Hardik Mishra
Its because when you have form enctype="multipart/form-data"
. You can not get other form fields by using request.getParameter("paramnName");
. It will always give you NULL.
因为当你有 form 时enctype="multipart/form-data"
。您无法使用 获取其他表单字段request.getParameter("paramnName");
。它总是会给你NULL。
You have to use FormItem
's isFormField()
to check if its regular field or file.
您必须使用FormItem
'sisFormField()
来检查它的常规字段或文件。
Example:
例子:
try {
ServletFileUpload upload = new ServletFileUpload();
response.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Got a form field: " + item.getFieldName() + " " +item);
} else{
System.out.println("Got an uploaded file: " + item.getFieldName() +
", name = " + item.getName());
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
response.getOutputStream().write(buffer, 0, len);
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
回答by CYMA
Adding onto the answer and addressing the question @Sayo Oladeji
添加答案并解决问题@Sayo Oladeji
To get the value of an input field you can use the following:
要获取输入字段的值,您可以使用以下内容:
System.out.println("Got a form field: " + item.getFieldName() + " " + Streams.asString(stream));