[英]Issue with multipart/form-data
I am not able to get values from both files and text input in a servlet when my form includes multipart/form-data. 当我的表单包含multipart / form-data时,我无法从servlet中获取文件和文本输入的值。 I am using the apache.commons.fileuploads for help with the uploads.
我正在使用apache.commons.fileuploads来帮助上传。 Any suggestions.
有什么建议么。 Also in the code below there are some things that I feel should be more efficient.
同样在下面的代码中,有些事情我认为应该更有效率。 Is there a better way to store these multiple files in a db.
有没有更好的方法将这些多个文件存储在数据库中。
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
{
boolean promo = false;
Database db = new Database();
Homepage hp = db.getHomePageContents();
String part = ParamUtils.getStringParameter(request, "part", "");
if(part.equals("verbage"))
{
String txtcontent = (String)request.getParameter("txtcontent");
String promoheader = (String)request.getParameter("promoheader");
String promosubheader = (String)request.getParameter("promosubheader");
hp.setBodyText(txtcontent);
hp.setPromoHeader(promoheader);
hp.setPromoSubHeader(promosubheader);
System.err.println(txtcontent);
}
else
{
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart)
{
}
else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
//System.err.print(items);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if(item.getFieldName().equals("mainimg1"))
{
if(item.getName() !="") hp.setMainImg1(item.getName());
}
if(item.getFieldName().equals("mainimg2"))
{
if(item.getName() !="") hp.setMainImg2(item.getName());
}
if(item.getFieldName().equals("mainimg3"))
{
if(item.getName() !="") hp.setMainImg3(item.getName());
}
if(item.getFieldName().equals("promoimg1"))
{
promo = true;
if(item.getName() !="")
{
hp.setPromoImg1(item.getName());
try {
File savedFile = new File("/Library/resin-4.0.1/webapps/ROOT/images/promoImg1.jpg");
item.write(savedFile);
//System.err.print(items);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
if(item.getFieldName().equals("promoimg2"))
{
if(item.getName() !="")
{
hp.setPromoImg2(item.getName());
try {
File savedFile = new File("/Library/resin-4.0.1/webapps/ROOT/images/promoImg2.jpg");
item.write(savedFile);
//System.err.print(items);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
if(item.getFieldName().equals("promoimg3"))
{
if(item.getName() !="")
{
hp.setPromoImg3(item.getName());
try {
File savedFile = new File("/Library/resin-4.0.1/webapps/ROOT/images/promoImg3.jpg");
item.write(savedFile);
//System.err.print(items);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
System.err.println("FNAME =" + item.getFieldName() + " : " + item.getName());
if (item.isFormField()) {
}
else {
try {
if(!promo)
{
String itemName = item.getName();
File savedFile = new File("/Library/resin-4.0.1/webapps/ROOT/images/"+itemName);
item.write(savedFile);
}
//System.err.print(items);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
}
}
db.updateHomePageContent(hp);
When using multipart/form-data
, the normal input field values are not available by request.getParameter()
because the standard Servlet API prior to version 3.0 doesn't have builtin facilities to parse them. 使用
multipart/form-data
, request.getParameter()
不能使用正常输入字段值,因为3.0版之前的标准Servlet API没有内置工具来解析它们。 That's exactly why Apache Commons FileUpload exist. 这正是Apache Commons FileUpload存在的原因。 You need to check if
FileItem#isFormField()
returns true
and then gather them from the FileItem
. 您需要检查
FileItem#isFormField()
返回true
,然后从FileItem
收集它们。
Right now you're ignoring those values in the code. 现在你忽略了代码中的那些值。 Admittedly, the
FileItem
is a misleading name, if it was me, I'd called it MultipartItem
or just Part
representing a part of a multipart/form-data
body which contains both uploaded fields and normal parameters. 诚然,
FileItem
是一个误导性的名称,如果是我,我把它叫做MultipartItem
或只是Part
代表的部分multipart/form-data
同时包含上传字段和正常参数的身体。
Here's a kickoff example how you should parse a multipart/form-data
request properly: 这是一个启动示例,您应该如何正确解析
multipart/form-data
请求:
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process normal fields here.
System.out.println("Field name: " + item.getFieldName());
System.out.println("Field value: " + item.getString());
} else {
// Process <input type="file"> here.
System.out.println("Field name: " + item.getFieldName());
System.out.println("Field value (file name): " + item.getName());
}
}
Note that you also overlooked a MSIE misbehaviour that it sends the full client side path along the file name. 请注意,您还忽略了MSIE错误行为,即它沿文件名发送完整的客户端路径。 You would like to trim it off from the
item.getName()
as per the FileUpload FAQ : 您希望根据FileUpload FAQ从
item.getName()
它:
String fileName = item.getName();
if (fileName != null) {
filename = FilenameUtils.getName(filename);
}
I have had similar problems in the past. 我过去也遇到过类似的问题。 The only way i could find round the problem was to put the fileupload into its own form.
我能找到解决问题的唯一方法是将fileupload放入自己的形式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.