org.apache.commons.fileupload 应用时FORM 怎么提得非文件的值?

J2EE 码拜 9年前 (2015-05-11) 621次浏览 0个评论
 

最近在写一个大文件上传的东西选来选去 用上了org.apache.commons.fileuplad包,现在遇到一个问题请过来人指点一二 ,当一个FORM中有文件 ,文本的时候,我想得到FORM提交上来的非文件时应该怎么取得啊?  一般在commons.fileupload里我们用if (fileItem.isFormField())判断提示上来的FORM里是不是有FILE的东西 但我要怎么才能得到非文件的东西啊

FileItem fi = (FileItem) it.next();
     if (!fi.isFormField()) {//忽略file类型以外的form类型元素(诸如text)
          //是html的file元素,这里处理
     }
     else {//html页面除file之外的其它元素,在这里处理
          String fieldName = fi.getFieldName();
          System.out.println(“The form field name is :” + fieldName);
          if (fieldName != null && fieldName.trim().equals(“fileDec”)) {
                String fileDec = fi.getString();//通过getString得到元素的value值
          }

查了下贴子 上面的写法对吧?

70分
不明白,如果你不是要上传文件的话,那form里面的映射类型就不能是FormFile类性的了。处理就麻烦一些了,因为struts标签将上传的那个字符串当作文件处理的路径。如果你要是上传文件的话,可以看看struts自带的例子,struts-upload.war。希望对你有所帮助。
还可以啊 有些用啊  做完这项目自己再用struts.upload试试吧 form还分得清是文件还是非文件了 上面说的也对  还有没有人发言啊 怎么一问点问题真的很少就有回贴的啊 CSDN真的不如以前了
30分
String field = “”;
Iterator iter = fileItems.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();

if (item.isFormField())
{
if (item.getFieldName().equals(“field”))
{
field = item.getString(“GBK”);
}
}
else
{
String name = item.getName();
long size = item.getSize();
if((name==null || name.equals(“”)) && size==0)
{
continue;
}
Pattern p = Pattern.compile(“.+\\(.+)$”);
Matcher m = p.matcher(name);
if(m.find())
{
//获取文件名(包括扩展名)
name = m.group(1);

//检查文件类型
if (!m.group(1).toLowerCase().endsWith(“jpg”))
{
%>
<script>alert(“请上传jpg图片!”);history.back();</script>
<%
return;
}

}
//保存上传的文件到指定的目录
try
{
item.write(new File(path + “upload_test/” + name));
}
catch(Exception e)
{
out.println(e);
}
}
}


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明org.apache.commons.fileupload 应用时FORM 怎么提得非文件的值?
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!