Java 表单参数未传递,但已处理同一表单中的文件?
我在JSP上有一个webform,它有几个字符串值和一个通过servlet上传到服务器的文件。奇怪的是,我可以将文件上传到服务器上,但无法使用Java 表单参数未传递,但已处理同一表单中的文件?,java,forms,file,jsp,servlets,Java,Forms,File,Jsp,Servlets,我在JSP上有一个webform,它有几个字符串值和一个通过servlet上传到服务器的文件。奇怪的是,我可以将文件上传到服务器上,但无法使用request.getParameter(“someString”)获取servlet中的值 我的代码有什么问题,或者指导我 编辑: 通过一些研究,我知道如果我在表单标记中使用enctype=“multipart/form data”,我将无法使用request.getParameter()在servlet中获取参数。现在的问题可能是,如何将文件和其他值发
request.getParameter(“someString”)
获取servlet中的值
我的代码有什么问题,或者指导我
编辑:
通过一些研究,我知道如果我在表单标记中使用enctype=“multipart/form data”
,我将无法使用request.getParameter()
在servlet中获取参数。现在的问题可能是,如何将文件和其他值发送到servlet进行处理
webform.jsp:
<form method="POST" enctype="multipart/form-data" action="/cassino/uploadFile" >
<fieldset>
<div class="form-group">
<label >*ID riparazione</label>
<input type="text" name="idRiparazione" />
</div>
<div class="form-group">
<label>*ID mandrino smontato</label>
<input type="text" name="idMandrinoSmontato" />
</div>
<div class="form-group">
<label>*Service livello(SL)</label>
<input type="text" name="serviceLivello" />
</div>
<div class="form-group">
<label>Attachment</label>
<input type="file" name="attachment" class="" id="attach" />
</div>
</fieldset>
</div>
<p class="text-right">
<input type="submit" value="Salva" name="newMacchina" />
<input type="reset" value="Cancella" />
</p>
</form>
在这种情况下,您不能再使用多部分表单使用
request.getParameter()
,这是正确的。通过FileItem
类提供对非文件表单字段的访问。在迭代项时,根据要查找的名称检查字段名称
...
String idRiparazione = null;
String idMandrinoSmontato = null;
String serviceLivello = null;
try {
List formItems = upload.parseRequest(request);
Iterator it = formItems.iterator();
// iterates over form's fields
while(it.hasNext()) {
FileItem item = (FileItem) it.next();
// processes only fields that are not form fields
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
// saves the file on disk
item.write(storeFile);
}
else
{
if ("idRiparazione".equals(item.getFieldName()))
idRiparazione = item.getString();
else if ("idMandrinoSmontato".equals(item.getFieldName()))
idMandrinoSmontato = item.getString();
else if ("serviceLivello".equals(item.getFieldName()))
serviceLivello = item.getString();
}
}
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello Servlet Get</h1>");
out.println(idMandrinoSmontato);
out.println(serviceLivello);
out.println("</body>");
out.println("</html>");
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
。。。
字符串idRiparazione=null;
字符串idmandrinosmanto=null;
字符串serviceLivello=null;
试一试{
List formItems=upload.parseRequest(请求);
迭代器it=formItems.Iterator();
//迭代窗体的字段
while(it.hasNext()){
FileItem=(FileItem)it.next();
//仅处理非表单字段的字段
如果(!item.isFormField()){
字符串文件名=新文件(item.getName()).getName();
字符串filePath=uploadPath+File.separator+fileName;
文件存储文件=新文件(文件路径);
//将文件保存在磁盘上
item.write(存储文件);
}
其他的
{
if(“idRiparazione”.equals(item.getFieldName()))
idRiparazione=item.getString();
else if(“idmandrinosmonto”.equals(item.getFieldName()))
IDMandrinosmanto=item.getString();
else if(“serviceLivello.equals(item.getFieldName()))
serviceLivello=item.getString();
}
}
PrintWriter out=response.getWriter();
out.println(“”);
out.println(“”);
println(“Hello Servlet Get”);
out.println(idmandrinosmonto);
out.println(serviceLivello);
out.println(“”);
out.println(“”);
}捕获(文件上载异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
Hello Servlet Get
null null
...
String idRiparazione = null;
String idMandrinoSmontato = null;
String serviceLivello = null;
try {
List formItems = upload.parseRequest(request);
Iterator it = formItems.iterator();
// iterates over form's fields
while(it.hasNext()) {
FileItem item = (FileItem) it.next();
// processes only fields that are not form fields
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
// saves the file on disk
item.write(storeFile);
}
else
{
if ("idRiparazione".equals(item.getFieldName()))
idRiparazione = item.getString();
else if ("idMandrinoSmontato".equals(item.getFieldName()))
idMandrinoSmontato = item.getString();
else if ("serviceLivello".equals(item.getFieldName()))
serviceLivello = item.getString();
}
}
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello Servlet Get</h1>");
out.println(idMandrinoSmontato);
out.println(serviceLivello);
out.println("</body>");
out.println("</html>");
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}