当前页面: 开发资料首页 → JSP 专题 → commons.fileupload的奇怪问题
commons.fileupload的奇怪问题
摘要: commons.fileupload的奇怪问题
代码如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置内存区块大小
factory.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(new File(application.getRealPath("//") + "temp//"));
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(1024*1024*100);
List fileItems = upload.parseRequest(request);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator i = fileItems.iterator();
while(i.hasNext()){
// String comment = ((FileItem)i.next()).getString();//存数据库用
FileItem fi = (FileItem)i.next();
// filename on the client
String fileName = fi.getName();
File tempFile = new File(fileName);
fileName = tempFile.getName();
// save comment and filename to database//存入数据库
// write the file
fi.write(new File(application.getRealPath("//") + "temp//", fileName));
}
%>
能上传文件,但是还出错:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Exception in JSP: /kkk.jsp:28
25: FileItem fi = (FileItem)i.next();
26: // filename on the client
27: String fileName = fi.getName();
28: File tempFile = new File(fileName);
29: fileName = tempFile.getName();
30: // save comment and filename to database//存入数据库
31: // write the file
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
java.io.File.
(File.java:194)
org.apache.jsp.kkk_jsp._jspService(kkk_jsp.java:74)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.
--------------------------------------------------------------------------------
Apache Tomcat/5.5.17
NullPointerException 错误,检查下String fileName = fi.getName(); getName()get出来的东西是不是null
大概是你传入的文件名字不存在
我把这行 while(i.hasNext()){ }注释掉了,就可以了,为什么呀
我想大概是你还是除了文件以外的其他参数,你的代码似乎全部把他当文件处理了