java中StringIndexOutOfBoundsException异常问题

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时才执行以下的语句,否则就跳过下面这些语句,防止下标越界,就是字符串为空时不能对字符串执行任何操作

你可能感兴趣的:(java错误分析,java,servlet,jvm,后端,spring)