开发者社区> 问答> 正文

本地Window7系统下没问题,传到Linux系统上出现越界异常,求告知怎么解决?报错

在本地Tomcat下运行,无任何问题,传到服务器Linux上就报错,这样的

java.lang.ArrayIndexOutOfBoundsException: 1
	com.FingersDance.core.controller.BasicController.toImg(BasicController.java:296)

 

展开
收起
爱吃鱼的程序员 2020-06-08 17:06:16 568 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    publicstaticStringclassPath=BasicController.class.getClassLoader().getResource("/").getPath(); 
       /**
       *图片处理并返回服务器路径存储路径给页面
       * @return
       **/
       @RequestMapping("/toImg.do")
       @ResponseBody
       publicResulttoImg(StringBase64,HttpServletRequestreq,HttpServletResponseres){
          //ImageBase64base=newImageBase64();
          //时间戳图片名
          Resultresult=newResult();
          Stringtimename=newSimpleDateFormat("yyyyMMddHHmmss").format(newDate());
          StringrootPath;
          Stringfile;
          if("\\".equals(File.separator)){ 
             //绝对路径
             //Windows
                file=req.getSession().getServletContext().getRealPath("\\")+"upload\\image\\"+timename+".jpeg";
                Stringlastpath=file.split(":")[1];
                file=file.split(":")[0]+":\\"+lastpath;
                //适用通用判断类型,以上舍弃
                rootPath=classPath.substring(1,classPath.indexOf("/WEB-INF/classes")); 
                 rootPath=rootPath.replace("/","\\");
                 file=rootPath+"/upload/image/"+timename+".jpeg";
          }else{
             //Linux
             rootPath=classPath.substring(0,classPath.indexOf("/WEB-INF/classes")); 
             rootPath=rootPath.replace("\\","/"); 
             file=rootPath+"/upload/image/"+timename+".jpeg";
          }

     

    /***

    已解决,谢谢各位,以上是实际代码

    */

    判断操作系统,只有Windows才有盘符的概念。

    只有windows的盘符才有 :这个字符。

    “\”反斜线好像只有windows上才支持,建议全部使用“/”,这个应该是多平台都支持的

    java的url类里应该有seperator常量可以用

    应该是\反斜线的问题,使用/吧(Win和Linux都支持)。以后所有的路径都用/表示就对了

    File.separator分隔符用这个有系统差异性

    我擦

    系统差异吧,但java却又是跨平台的。

    把错误debug下,或log下,应该可以找到原因。不存在:所以分割失败,数组1就索引不到了,越界

    2020-06-08 17:06:27
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载
ECS运维指南 之 Linux系统诊断 立即下载