<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="biz.PlatFormBiz"%>
<%@page import="biz.impl.PlatFormBizImpl"%>
<%@page import="entity.Platform"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.FileUploadException"%>
<%@page import="org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException"%>
<%@page import="org.apache.commons.fileupload.FileUploadBase"%>
<%@page import="util.NowDate"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.io.File"%>
<%
final long MAX_SIZE = 4 * 1024 * 1024;// 设置上传文件最大为 4M
// 允许上传的文件格式的列表
final String[] allowedExt = new String[] { "jpg", "jpeg", "gif", "png" };
String company = "";
String qualification = "";
String content = "";
String name = "";
String address = "";
int count = 0;
String tg = "";
String tel = "";
String url = "";
PlatFormBiz platFormBiz = new PlatFormBizImpl();
Platform platform = new Platform();
// 实例化一个硬盘文件工厂,用来配置上传组件ServletFileUpload
DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(4096);// 设置上传文件时用于临时存放文件的内存大小,这里是4K.多于的部分将临时存在硬盘
dfif.setRepository(new File(request.getRealPath("/") + "img"));// 设置存放临时文件的目录,web根目录下的img目录
// 用以上工厂实例化上传组件
ServletFileUpload sfu = new ServletFileUpload(dfif);
// 设置最大上传尺寸
sfu.setSizeMax(MAX_SIZE);
// 从request得到 所有 上传域的列表
List fileList = null;
try {
fileList = sfu.parseRequest(request);
} catch (FileUploadException e) {// 处理文件尺寸过大异常
if (e instanceof FileUploadBase.SizeLimitExceededException) {
out.println("<script>alert('文件尺寸超过规定大小:" + MAX_SIZE
+ "字节');</script>");
return;
}
e.printStackTrace();
}
// 得到所有上传的文件
Iterator fileItr = fileList.iterator();
// 循环处理所有文件
while (fileItr.hasNext()) {
FileItem fileItem = null;
String apath = null;
long size = 0;
// 得到当前文件
fileItem = (FileItem) fileItr.next();
// 获取表单普通域的数据
if (fileItem.isFormField()) {
if ("company".equals(fileItem.getFieldName())) {
company = new String(fileItem.getString().getBytes(
"iso-8859-1"), "utf-8");
}
if ("name".equals(fileItem.getFieldName())) {
name = new String(fileItem.getString().getBytes(
"iso-8859-1"), "utf-8");
}
if("count".equals(fileItem.getFieldName())){
count = Integer.parseInt(fileItem.getString());
}
if ("qualification".equals(fileItem.getFieldName())) {
qualification = new String(fileItem.getString().getBytes(
"iso-8859-1"), "utf-8");
}
if ("tel".equals(fileItem.getFieldName())) {
tel = new String(fileItem.getString()
.getBytes("iso-8859-1"), "utf-8");
}
if ("url".equals(fileItem.getFieldName())) {
url = new String(fileItem.getString()
.getBytes("iso-8859-1"), "utf-8");
}
if ("address".equals(fileItem.getFieldName())) {
address = new String(fileItem.getString().getBytes(
"iso-8859-1"), "utf-8");
}
if ("tg".equals(fileItem.getFieldName())) {
tg = new String(fileItem.getString().getBytes(
"iso-8859-1"), "utf-8");
}
if ("content".equals(fileItem.getFieldName())) {
content = new String(fileItem.getString().getBytes(
"iso-8859-1"), "utf-8");
}
continue;
} else {
// 得到文件的完整路径
apath = fileItem.getName();
// 得到文件的大小
size = fileItem.getSize();
if ("".equals(apath) || size == 0) {
out.print("<script>alert('请选择文件');</script>");
return;
}
// 得到去除路径的文件名
String t_name = apath.substring(apath.lastIndexOf("\\") + 1);
// 得到文件的扩展名(无扩展名时将得到全名)
String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
// 拒绝接受规定文件格式之外的文件类型
int allowFlag = 0;
int allowedExtCount = allowedExt.length;
for (; allowFlag < allowedExtCount; allowFlag++) {
if (allowedExt[allowFlag].equals(t_ext))
break;
}
if (allowFlag == allowedExtCount) {
for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
out.println("<script>alert('请上传以下类型的文件*."
+ allowedExt[allowFlag]
+ "&nbsp;&nbsp;&nbsp;');</script>");
return;
} long now = System.currentTimeMillis();
// 根据系统时间生成上传后保存的文件名
String prefix = String.valueOf(now);
// 保存的最终文件完整路径,保存在web根目录下的img目录下
String u_name = request.getRealPath("/") + "img/" + prefix
+ "." + t_ext;
String pic = "/img/" + prefix + "." + t_ext;
try {
// 保存文件
fileItem.write(new File(u_name));
platform.setAddress(address);
platform.setCompany(company);
platform.setContent(content);
platform.setPic(pic);
platform.setTel(tel);
platform.setUrl(url);
platform.setTgurl(tg);
platform.setName(name);
platform.setCount(count);
platform.setContent(content);
platform.setQualification(qualification);
NowDate nowDate = new NowDate();
String addtime = nowDate.nowDate();
platform.setAddtime(addtime);
int reg = platFormBiz.save(platform);
if (reg == 1) {
out
.print("<script>alert('添加成功');location.href='PlatformList.jsp?action=platlist';</script>");
} else {
out
.print("<script>alert('添加失败');location.href='PlatformList.jsp?action=platlist';</script>");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}%>这个是使用commons.fileupload上传文件的代码..我在本地运行没有错误,上传到虚拟主机后.Iterator fileItr = fileList.iterator();这里报空指针了.请问是什么情况.