4

我有一个包含这段代码的页面:

<form action="Servlet" enctype="multipart/form-data">
<input type="file" name="file">
<input type="text" name="text1">
<input type="text" name="text2">
</form>

当我request.getParameter("text1");在我的 Servlet 中使用时,它显示为空。如何让我的 Servlet 接收参数?

4

4 回答 4

6

所有请求参数都嵌入到多部分数据中。您必须使用 Commons File Upload 之类的方法来提取它们:http: //commons.apache.org/fileupload/

于 2012-03-12T12:45:40.040 回答
2

Pleepleus is right, commons-fileupload is a good choice.
If you are working in servlet 3.0+ environment, you can also use its multipart support to easily finish the multipart-data parsing job. Simply add an @MultipartConfig on the servlet class, then you can receive the text data by calling request.getParameter(), very easy.

Tutorial - Uploading Files with Java Servlet Technology

于 2013-07-15T13:35:40.813 回答
2

使用getParts()

于 2012-03-12T12:45:10.123 回答
1

您需要像这样发送参数:

writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"" + urlParameterName + "\"" )
                .append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF);
writer.append(urlParameterValue).append(CRLF);
writer.flush();

在 servlet 方面,处理 Form 元素:

items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
       item = (FileItem) iter.next();
       if (item.isFormField()) {
          name = item.getFieldName(); 
          value = item.getString();

   }}
于 2013-11-07T15:31:32.300 回答