简单文件上传代码

GavinFj 2007-03-07 07:23:42
为什么下面的代码有第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)


知道的帮忙下,顺便解释下。我是菜鸟```
...全文
1397 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
GavinFj 2007-03-10
  • 打赏
  • 举报
回复
知道答案了,要建个要放文件的文件夹;我建错地方了...
GavinFj 2007-03-10
  • 打赏
  • 举报
回复
请问dr_lou() :
mySmartUpload.upload();
你这个Bean是怎么写的?还有要存的时候要不要先建好文件夹?

----------------------------------------

问题还没解决,谁能在我的代码上找到什么错吗?那个也是从网上找的;
在我机子上运行不能通过...
dr_lou 2007-03-09
  • 打赏
  • 举报
回复
<%@ 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>

GavinFj 2007-03-09
  • 打赏
  • 举报
回复
seavers(泪魂):
java.lang.NegativeArraySizeException

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

-----------------------------------

文件路径要怎么设置吗?不太懂什么意思...能具体修改下代码吗?
大海Online 2007-03-08
  • 打赏
  • 举报
回复
java.lang.NegativeArraySizeException

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

doc,txt 逗号, 笔误吧``
GavinFj 2007-03-08
  • 打赏
  • 举报
回复
这句没错吧```???只是多余罢了....
toiler 2007-03-08
  • 打赏
  • 举报
回复
su.setDeniedFilesList("exe,bat,jsp,htm,html,,");不时笔误吧?
GavinFj 2007-03-08
  • 打赏
  • 举报
回复
up...
GavinFj 2007-03-07
  • 打赏
  • 举报
回复
................

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧