为什么我在jsp上传文件后无法访问请求中的参数?

Ign*_*uin 2 java jsp servlets email-attachments

我正在使用jsp和servlet来执行此操作.

我有一个联系表单,发送包含一些数据(姓名,主题,问题,联系电子邮件等)和文件的电子邮件.

当我提交表单,并获得servlet响应时,只返回第一件事.

String file= fileUpload(request); //upload the client's file and return the absolute      path of the file in the server
//testing the rest of parameters
 out.println("REQUEST LIST"
              "\n"   request.getParameter("name")
              "\n"   request.getParameter("mail")
              "\n"   request.getParameter("subject")
              "\n"   request.getParameter("ask")
              "\n");
Run Code Online (Sandbox Code Playgroud)

按此顺序,文件成功上载,但其他参数(名称,邮件等)为空.


在下面的顺序中,参数是正确的,它们正确地返回数据.但该文件未上传.

//testing the rest of parameters
out.println("REQUEST LIST"
              "\n"   request.getParameter("name")
              "\n"   request.getParameter("mail")
              "\n"   request.getParameter("subject")
              "\n"   request.getParameter("ask")
              "\n");
String file= fileUpload(request); //upload the client's file and return the absolute      path of the file in the server
Run Code Online (Sandbox Code Playgroud)

我怎么能两个都有?谢谢!

Bal*_*usC 5

您应该在提取上载文件时使用相同的 API(例如Apache Commons FileUpload)提取请求参数.这通常不能与调用互换,getParameter()因为请求主体只能被解析一次(最终用户不会发送两次相同的请求,一个要由文件上传解析API解析,另一个要解析getParameter()).

也可以看看: