日期:2014-05-18  浏览次数:20766 次

简单文件上传代码
为什么下面的代码有第7行就报错:

1                 SmartUpload   su   =new   SmartUpload();
2                 su.initialize(pageContext);        
3                 su.setMaxFileSize(10000);
4                 su.setTotalMaxFileSize(20000);
5                 su.setAllowedFilesList( "doc,txt ");
6                 su.setDeniedFilesList( "exe,bat,jsp,htm,html,, ");
7                 su.upload();
8                 int   count=su.save( "/upload ");
9                 out.println(count+ "个文件上传成功! <br> ");
10                 out.println( "TEST= "+su.getRequest().getParameter( "TEST ")+ " <BR> <BR> ");

报错是:
java.lang.NegativeArraySizeException
com.jspsmart.upload.SmartUpload.upload(SmartUpload.java:218)
org.apache.jsp.do_005fupload_jsp._jspService(do_005fupload_jsp.java:60)
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)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)


知道的帮忙下,顺便解释下。我是菜鸟```


------解决方案--------------------
su.setDeniedFilesList( "exe,bat,jsp,htm,html,, ");不时笔误吧?
------解决方案--------------------
java.lang.NegativeArraySizeException

数组大小为负数, 估计没找到文件, 取文件大小时返回-1, 看看文件路径

doc,txt 逗号, 笔误吧``
------解决方案--------------------
<%@ page contentType= "text/html;charset=GBK " %>
<%@ page language= "java " import= "com.jspsmart.upload.* "%>
<%@ page import= "java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.* " %>
<jsp:useBean id= "mySmartUpload " scope= "page " class= "com.jspsmart.upload.SmartUpload " />

<HTML>
<BODY BGCOLOR= "white ">
<%
//初始化文件名和上传结果
String FileName = "noImage ";
String result = "error ";
try
{
// 执行初始化操作
mySmartUpload.initialize(pageContext);
//限制文件上传格式
mySmartUpload.setAllowedFilesList( "jpg,gif,bmp,jpeg,JPG,BMP,GIF ");
// 上传文件到服务器
mySmartUpload.upload();

//如果有文件上传
if(mySmartUpload.getFiles().getCount()> 0)
{
// 取出文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
//
//如果文件存在
if(myFile.getSize()> 0)
{
String strExt = myFile.getFileExt();