首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

简单文件上传代码解决办法

2012-01-23 
简单文件上传代码为什么下面的代码有第7行就报错:1SmartUploadsunewSmartUpload()2su.initialize(pageCo

简单文件上传代码
为什么下面的代码有第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();
java.util.Date d = new java.util.Date();
FileName = d.getTime()+ ". "+strExt;
myFile.saveAs( "../webblog/images/upload/articleImage/ " +FileName);


result= "success ";

//以下生成缩略图
String strRealPath = request.getRealPath( "/ ")+ "\\images\\upload\\articleImage\\ "+FileName; //原图路径
java.io.File file = new java.io.File(strRealPath); //读入刚才上传的文件
String newUrl=request.getRealPath( "/ ")+ "\\images\\upload\\articleImage\\small\\ "+FileName; //新的缩略图保存地址
Image src = javax.imageio.ImageIO.read(file); //构造Image对象
float tagsize=300;
int old_w=src.getWidth(null); //得到源图宽
int old_h=src.getHeight(null); //得到源图长
int tempsize;
float tempdouble = 1;
if(old_w > 300 || old_h> 300){
if(old_w> old_h){
tempdouble=old_w/tagsize;
}else{
tempdouble=old_h/tagsize;
}
}
int new_w =Math.round(old_w/tempdouble);
int new_h=Math.round(old_h/tempdouble);//计算新图长宽
BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图
FileOutputStream newimage=new FileOutputStream(newUrl); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag); //近JPEG编码
newimage.close();
}
else
{
System.out.println( "文件不存在! ");
//result= "error ";
}
}
else
{
System.out.println( "没上传! ");
//result= "error ";
}
}
catch(Exception ex)
{
System.out.println(ex);
//result= "error ";
}
finally
{
//存储表单信息
String Atitle = mySmartUpload.getRequest().getParameter( "Atitle ");
String Acontent = mySmartUpload.getRequest().getParameter( "Acontent ");
String Atype = mySmartUpload.getRequest().getParameter( "Atype ");
String Astate = mySmartUpload.getRequest().getParameter( "Astate ");
request.setAttribute( "Atitle ",Atitle);
request.setAttribute( "Acontent ",Acontent);
request.setAttribute( "Atype ",Atype);
request.setAttribute( "Astate ",Astate);
request.setAttribute( "FileName ",FileName);
request.setAttribute( "Result ",result);
}
%>
<jsp:forward page= "../insertarticlesrv "/>
</BODY>
</HTML>

热点排行