java中StringIndexOutOfBoundsException异常问题

在java中遇到StringIndexOutOfBoundsException异常问题:

如以下异常:

`java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.csi.servlet.DispatcherServlet.service(DispatcherServlet.java:32)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.lang.String.substring(String.java:1927)
	at com.csi.controller.UserInfoController.update(UserInfoController.java:370)
	... 25 more
  • 这个问题就是字符串下标越界。
  • 下面这个是出现异常的代码
String realPath = request.getServletContext ().getRealPath ( "/front/upload" );

                File realDir = new File ( realPath );

                if (!realDir.exists ()) {
                    realDir.mkdirs ();
                }

                String fileName = fileItem.getName ();

                    String substring = fileName.substring ( fileName.lastIndexOf ( "." ) );

                    IdWorker idWorker = new IdWorker ( 0 , 0 );

                    String newName = idWorker.nextId () + substring;

                    File uploadFile = new File ( realPath , newName );
                    try {
                        fileItem.write ( uploadFile );
                    } catch (Exception e) {
                        e.printStackTrace ();
                    }
                    userInfo.setUserHead ( "upload/" + newName );
                }

当:String fileName = fileItem.getName ();fileItem这个值是空值时执行这个语句:String substring = fileName.substring ( fileName.lastIndexOf ( "." ) );就会抛出:java.lang.StringIndexOutOfBoundsException: String index out of range: -1这个异常,因为fileItem是空值substring ( fileName.lastIndexOf ( "." ) )截取字符串语句执行失败就抛出异常。

解决方式:

 if (fileItem.getName ().length ()>0) {
                String realPath = request.getServletContext ().getRealPath ( "/front/upload" );

                File realDir = new File ( realPath );

                if (!realDir.exists ()) {
                    realDir.mkdirs ();
                }

                String fileName = fileItem.getName ();

                    String substring = fileName.substring ( fileName.lastIndexOf ( "." ) );

                    IdWorker idWorker = new IdWorker ( 0 , 0 );

                    String newName = idWorker.nextId () + substring;

                    File uploadFile = new File ( realPath , newName );
                    try {
                        fileItem.write ( uploadFile );
                    } catch (Exception e) {
                        e.printStackTrace ();
                    }
                    userInfo.setUserHead ( "upload/" + newName );
                }
            }

在上面添加判断语句:

if (fileItem.getName ().length ()>0) {

如果传过来的的字符串长度大于0时才执行以下的语句,否则就跳过下面这些语句,防止下标越界,就是字符串为空时不能对字符串执行任何操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值