jsp上传图片代码如下:
String img=ImgForm.getImg().getFileName();
String imgok="upload/"+temp+"."+(img.split("\\.")[1])+"";
String path=this.getServlet().getServletContext().getRealPath("/")+"img\\"+"."+(img.split("\\.")[1]);上传imgok为空时,出现以下异常:
java.lang.ArrayIndexOutOfBoundsException: 1
请教大家解决的方法?
String img=ImgForm.getImg().getFileName();
String imgok="upload/"+temp+"."+(img.split("\\.")[1])+"";
String path=this.getServlet().getServletContext().getRealPath("/")+"img\\"+"."+(img.split("\\.")[1]);上传imgok为空时,出现以下异常:
java.lang.ArrayIndexOutOfBoundsException: 1
请教大家解决的方法?
如果你是struts提交的话,有没有把form里的这个input与action form绑定呢?
检查下form标签有没有"enctype="multipart/form-data"
看下ImgForm的属性有没有和上传那的name一致.
萎缩点把img print 出来
打印出来看看你得到的是什么?下边这句报错吧,它从零开始的
String path=this.getServlet().getServletContext().getRealPath("/")+"img\\"+"."+(img.split("\\.")[1]);
String imgok="upload/"+temp+"."+(img.split("\\.")[0])+"";
先判断 img是否包含 . 符号 有了在分割,取值是img.split("\\.")[0])
而不是img.split("\\.")[1])
alert(img.split("\\.").length);//取到的文件名被 . 分隔后的数组
2. String[] arr=img.split("\\.");
String str=arr.length>1?arr[1]:arr[0];
以后用str 取代img.split("\\.")[1]
String[] str;
String string="";
if(picName!=null&&picName.length()>0){
str=picName.split("\\.");
if(str.length>2){
string=picName.split("\\.")[1];
}
}
}
不就可以了
2. String[] arr=img.split("\\.");
String str=arr.length>1?arr[1]:arr[0];
以后用str 取代img.split("\\.")[1]
在执行split之前判断一下就可以了。
String imgok="upload/"+temp+"."+img.substring(img.indexOf("."))+"";