我隐藏了输入类型,其中我存储了所选的ID
JSP文件
<form method="post" enctype="multipart/form-data" action="./PropertyImage">
<input type="hidden" name="property_id" value="<%=bean.getproperty_id%>"/>
</form>
现在在servlet中我必须访问这个隐藏的输入类型
Sevlet
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload( factory );
// upload.setSizeMax(yourMaxRequestSize);
// Parse the request
List<FileItem> uploadItems;
try
{
uploadItems = upload.parseRequest( request );
Dictionary dict = new Hashtable();
for( FileItem uploadItem : uploadItems )
{
if( uploadItem.isFormField() )
{
String fname = "";
String fieldName = uploadItem.getFieldName();
String value = uploadItem.getString();
dict.put(fieldName, value);
}
System.out.println("Property id == "+dict.get("property_id"));
final String UPLOAD_DIRECTORY="E:/parag/project/New folder/sam/WebContent/images/agentimg";
String image_name="";
String imagepath="";
if(ServletFileUpload.isMultipartContent(request))
{
try
{
for(FileItem item : uploadItems)
{
if(!item.isFormField())
{
//get the value for select images to upload .
image_name = new File(item.getName()).getName();
System.out.println("image name == "+image_name);
String imagename=new Date().getTime()+image_name;
item.write( new File(UPLOAD_DIRECTORY + File.separator +imagename));
request.setAttribute("image", UPLOAD_DIRECTORY + File.separator + image_name);
//create images path to forward database.
imagepath="images/agentsimg/"+imagename;
System.out.println("path == "+imagepath);
}
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
PropertyBean bean = new PropertyBean();
// bean.setProperty_id(property_id);
bean.setProperty_image(imagepath);
int status = PropertyModel.propertyImage(bean);
if(status>0)
{
String message = "Image added to database";
request.setAttribute("message", message);
request.getRequestDispatcher("/properties.jsp").forward(request, response);
}
else
{
String message1 = "Image not added to database";
request.setAttribute("message1", message1);
request.getRequestDispatcher("/properties.jsp").forward(request, response);
}
}
else
{
String message2 = "Image upload to database";
request.setAttribute("message2", message2);
request.getRequestDispatcher("/agents.jsp").forward(request, response);
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
但输出是
Property id == null
请帮我这个
多部分时如何获取隐藏输入类型的请求参数???
答案 0 :(得分:0)
首先,无论你做什么都是完全错误的......
如果您使用multipart/form-data
加密表单字段,则需要在servlet端解密您的请求。
首先,您需要common-io-version.jar
和commons-fileupload-version.jar
然后在你 Servlet
,只需将代码放在下面,您就可以完成它....
if(ServletFileUpload.isMultipartContent(request))
{
try
{
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : multiparts)
{
//check if the item that we get is a file or just a form data
//go in if condition if file found else goto else for form data
if (!item.isFormField())
{
//gets the file name from request
String name = item.getName();
// At the place of UPLOAD_DIRECTORY you can replace your path to the folder where you want to save your image
File userdir = new File(UPLOAD_DIRECTORY);
//create user directory if not exists
if (!userdir.exists())
{
userdir.mkdir();
}
boolean exist=true;
String photoURL=UPLOAD_DIRECTORY + File.separator + name;
File photo=new File(photoURL);
item.write(photo);
System.out.println("Photo Number"+number);
}
else
{
// Here you will get each and every field that you defined within your form tag
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
System.out.println(fieldname + " : " + fieldvalue);
}
}
}
catch (Exception ex)
{
request.setAttribute("message", "File Upload Failed due to " + ex);
ex.printStackTrace();
}
}
else
{
request.setAttribute("message", "Sorry this Servlet only handles file upload request");
}
在 JSP
中,只需将代码放在
<form action="uploadtodb" method="post" enctype="multipart/form-data">
User Name:
<input type="text" name="userName" id="userName" placeholder="123">
<br>
<input type="hidden" name="property_id" value="<%=bean.getproperty_id%>"/>
<input type="file" name="file" accept="image/*">
<br>
<input type="submit" value="upload">
</form>
如果您不想上传任何文件,请输入以下代码
<强> Servlet
强>
if(ServletFileUpload.isMultipartContent(request))
{
try
{
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : multiparts)
{
if (item.isFormField())
{
// Here you will get each and every field that you defined within your form tag
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
System.out.println(fieldname + " : " + fieldvalue);
}
}
}
catch (Exception ex)
{
request.setAttribute("message", "File Upload Failed due to " + ex);
ex.printStackTrace();
}
}
else
{
request.setAttribute("message", "Unable to parse your request...");
}
只需从 type="file"
文件中删除带有JSP
的输入代码,您就完成了。
你正在做的是非多部分请求你不能用多部分请求这样做,因为它是加密的...