提交Multipart / form-data表单

时间:2013-03-18 14:15:32

标签: java html http servlets

在IE9中,当我将带有文件的表单(enc-type:multipart / form-data)提交给java servlet时出错了。我无法找到错误并找到解决方案。当Content-Length不工作时,它是0。这是请求标头中的唯一区别。并且请求消息没有区别。

我如何尝试在java serlvet中检索表单数据(使用org.apache.commons):

ServletFileUpload upload = new ServletFileUpload();
if (!ServletFileUpload.isMultipartContent(request)
    throw new Exception("Invalid parameters");
FileItemIterator itr = upload.getItemIterator(request);
while(itr.hasNext()
{
    // This never gets run when the error occurs (Content-Length: 0).. 
}

我认为表单提交有问题?有谁知道什么可能是错的?在Chrome中,它始终有效。

更新:html格式的基本部分:

<form name='uploadparticipants' action='ParticipantUploader' method='post' encoding='multipart/form-data' enctype='multipart/form-data' target='upload_target' onsubmit='admin.uploadCourseParticipants()'>
// Some input fields inside a table, among them a file input //
<input type='submit' value='somevalue'>
</form>

1 个答案:

答案 0 :(得分:0)

  1. 请确保您在表单创建过程中明确添加了method="post"
  2. 此外,在使用Javascript在Internet Explorer(6和7)中动态创建表单时,您还需要设置编码属性,出于某种原因。因此,尽管该属性称为enctype,但设置enctype不会执行任何操作。请尝试添加以下代码:

    form.setAttribute('encoding','multipart / form-data');