core java--day9(内部类,==与equals()的区别,String,StringBuilder与StringBuffer)

本文详细介绍了Java中的内部类,包括成员内部类、静态内部类、局部内部类和匿名内部类的定义、使用场景及其与外部类的交互。此外,还讨论了`==`和`equals()`的区别,以及`String`、`StringBuilder`和`StringBuffer`在字符串操作中的应用,强调了`String`不可变性和效率上的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

内部类
    7.1: 定义:在一个类中的另外一个类。
        eg: 
            .java          ---> 小区
            public class A{} ---> A栋
            class B{}      ---> B栋
            class C{}      ---> C栋

        内部类:
            .java         ---> 小区
            public classAA{} ---> AA栋
                class AA_home -> AA栋内部的home

            class BB{}      ---> BB栋
                class BB_home-> BB栋内部的home         

            class CC{}      ---> CC栋
            
        class AA_home  class BB_home 就是内部类。
    7.2:内部类的分类
        7.2.1:成员内部类.
        7.2.2:静态内部类.
        7.2.3:局部内部类.
        7.2.4:匿名内部类.
    7.3, 成员内部类
        声明:在类中成员变量/成员方法位置编写。public class AA_home{};
            注:在内部类中可以有成员方法,成员属性。
        7.3.1:在内部类的方法中使用
            7.3.1.1:成员内部类中属性编写: 普通属性。静态属性需要被定义为final
            7.3.1.2:成员内部类中方法编写: 普通方法
            7.3.1.3:成员内部类中方法调用内部类属性:直接调用/this.属性名
            7.3.1.4:成员内部类中方法调用内部类方法:直接调用/this.方法名()

            7.3.1.5:成员内部类中方法调用外部类属性:外部类名.this.属性名
            7.3.1.6:成员内部类中方法调用外部类方法:外部类名.this.方法名()
        7.3.2:在外部类的方法调用成员内部类的属性方法
            前提: 创建内部类对象 AA_home aw  = new AA_home();
            7.3.2.1:外部类方法调用内部类属性: aw.属性
            7.3.2.2:外部类方法调用内部类方法: aw.方法名();
        7.3.3:在main方法中使用内部类:
            前提:先要获得外部类对象 MemberInner mi = new MemberInner();
            7.3.3.1: 获得内部类对象: aw 
                1.3.1.1: AA_home aw = mi.new AA_home();
                1.3.1.2: MemberInner.AA_home aw = mi.new AA_home();
            7.3.3.2:调用内部类属性:aw.属性名;
            7.3.3.3:调用内部类方法:aw.方法名();
    
    7.4, 静态内部类: 即: 静态的成员内部类
        声明:在类中静态成员变量/静态成员方法位置编写。public static class AA_home{};
              静态内部类中可以声明静态成员和静态方法.
        7.4.1:在内部类的方法中使用
            7.4.1.1:静态内部类中属性编写: 可以定义静态属性和非静态属性
            7.4.1.2:静态内部类中方法编写: 可以有静态方法和非静态方法
            7.4.1.3:静态内部类中方法调用内部类普通属性:直接调用/this.属性名
            7.4.1.3:静态内部类中方法调用内部类静态属性:直接调用/this.属性名。会有警告。静态的属性应该用静态的方式类调用
            7.4.1.4:静态内部类中方法调用内部类普通方法:直接调用/this.方法名()
            7.4.1.5:静态内部类中方法调用内部类静态方法:直接调用/this.方法名()

            7.4.1.6:静态内部类中方法调用外部类普通属性: 不可以访问
            7.4.1.7:静态内部类中方法调用外部类静态属性: 外部类名.属性
            7.4.1.8:静态内部类中方法调用外部类普通方法: 不可以访问
            7.4.1.9:静态内部类中方法调用外部类静态方法: 外部类名.方法名
            总结:静态内部类,内部方法只能访问外部类的静态属性/方法。
        7.4.2:在外部类的方法调用静态内部类的属性方法
            普通属性方法:
             前提: 创建内部类对象 AA_home aw  = new AA_home();
             7.4.2.1:外部类方法调用内部类属性: aw.属性
             7.4.2.2:外部类方法调用内部类方法: aw.方法名();
            静态属性方法:
             7.4.2.1:外部类方法调用内部类属性: 静态内部类.属性
             7.4.2.2:外部类方法调用内部类方法: 静态内部类.方法
        7.4.3:在main方法中使用静态内部类:
            原则:如果调用静态资源需要用类名直接调用,非静态的需要new对象。
             AA_home aw  = new AA_home()
             aw.方法();
             
    7.5, 局部内部类: 在局部代码块内部。
        声明:在局部代码块中声明  class CC_home{};
              局部内部类中只能有非静态方法,非静态属性(如果有静态属性需要被定义为final)
        7.5.1:在局部内部类的方法中使用
            7.5.1.1:局部内部类中属性编写: (静态+非静态)属性必须被定义final
            7.5.1.2:局部内部类中方法编写: 普通方法

            7.5.1.3:局部内部类中方法调用内部类属性:直接调用/this.属性名
            7.5.1.4:局部内部类中方法调用内部类方法:直接调用/this.方法名()

            7.5.1.5:局部内部类中方法调用外部类属性:外部类名.this.属性名
            7.5.1.6:局部内部类中方法调用外部类方法:外部类名.this.方法名()
        7.5.2:在外部类的方法调用局部内部类的属性方法
            注意:这个是不可能被访问到的。

        7.5.3:在main方法中使用内部类:
            注意:这个是不可能被访问到的。

        为什么外部类访问不到局部内部类的属性方法?
        
    7.6, 匿名内部类:  没有构造器的类(不能手动写,系统会默认给)
        一般用匿名内部类来实现某接口(使用不多,不需要写一个类实现该接口,可以使用匿名内部类)
        匿名内部类一般就是用来实现接口:
        所以: 重写接口(抽象类)中的方法。
        可以在匿名内部类中写属性。
        
        声明:
        interface A{
            public void show();
        }
        eg:   A a = new A(){
            public void showA(){
            }
              };
        使用情景:面向接口编程,有时候写代码需要写一个类实现某个接口去完成某个功能,eg:集合中的比较器。
              但这个功能只用一次,没有必要写一个类那么久可以使用匿名内部类来实现这个接口完成这个功能。
        ------------------------------
        abstract class B{
            public void showB(){}
            public abstract void shohome();
        } 
        eg:
            B b = new B(){
                //可以重写showB 可以不重写
                public void shohome(){}//抽象方法一定要实现
            };
        解释:这里并不是new了接口, 而是多态, new的是接口的实现类,这个实现类就是匿名内部类充当了。

package com.briup.ch9;
/**
 * 外部类<Br>
 * 1,测试 内部类 能写什么
 * 2,在内部类的方法中怎么访问外部类的资源
 * 3,在外部类中的方法中怎么访问内部类的资源
 * 4,在main方法中怎么访问内部类的资源
 * */
public class Test4 {
  public void show() {
	 int num = 10;
	 /**
	    * 局部内部类
	  * */
	 class Inner_Two{
		 
		 
	 }
  }
  public int age = 10;
  /**
   * 成员内部类<Br>
   * 不能使用静态资源<br>
   * */
  public class Inner_One{
	  public int innage = 10;
	  public void oneShow() {
		  System.out.println("oneShow");
		  //调用外部类的静态方法
		  Test4.myshow();
		  //调用外部类的普通成员变量/成员方法
		  int myage =  Test4.this.age;
		  Test4.this.show();
	  }
  }
  /**
      * 调用内部类测试方法
   * */
  public void test() {
	  //外部类调用内部类的方法和属性
	  Inner_One one = new Inner_One();
	  System.out.println(one.innage);
	  one.oneShow();
  } 
  /**
   * main方法调用内部类的资源
   * */
  public static void main(String[] args) {
	  //调用成员内部类的资源
	  Test4 t = new Test4();
	  Inner_One one = t.new Inner_One();
	  one.oneShow();
	  
  }
  
  public static String name = "tom";
  public static void myshow() {
	  System.out.println("myshow");
  }
  /**
   * 静态内部类
   * */
  public static class Inner_Three{
	  
	  
  }
  //匿名内部类
	
}
package com.briup.ch9;
/**
  * 匿名内部类<br>
 * <li>继承父类
 * <li>实现父接口
 * */
public class Test5 {
	public static void main(String[] args) {
		//使用匿名内部类继承父类F
		F f = new F() {
			public void show() {
				System.out.println();
			}
		};
		f.show();
		//实现接口,实现接口中没有完成的方法
		I1 i = new I1() {
			@Override
			public void show() {
				System.out.println("匿名内部类实现接口");
			}
		} ;
		i.show();
		
	}
}

class F{
	public void show() {
		System.out.println("F");
	}
}
class Z extends F{
	public void show() {
		System.out.println("z");
	}
}

interface I1{
	public void show();
}
class Impl implements I1{
	@Override
	public void show() {
		System.out.println("show");
	}
}






== 和 equals()区别

    8.1: == 定义:比较内存地址是否相同。
    8.2: equals()定义:Object类中的方法,Object中的equals()是比较内存地址是否相同。
    注意:equals(Obj)是一个方法,所以只能是对象调用。equals是一个方法,所以可能被重写。
    8.3: String类重写了equals()方法,所以在String对象调用equals时是比较值。
    补充:
        toString():当system输出对象的时候,其实是输出对象.toString();可以重写。
        getClass():返回当前的真实对象,不可以重写。

String类的使用 : 
     字符串,【创建以后不能改变】,每一次拼接都会产生新的字符串。
     构造方法: 
    new String(byte[],start,len);
    new String("");
     常用方法:
        char = charAt(int index);  
        返回下标的字符
    int = compareTo(String anotherString);
        比较两个字符串
        返回 字符串 相差
        s11.compareTo(s12);
        正数  s11   >   s12
        负数  s11   <   s12
        0     s11   ==  s12
    boolean = equals(Object anObject)
        比较两个字符串值是否相同

    byte[]  = getBytes();
        返回字符串的字节表现形式
    * int = indexOf("");
        判断某个字符串在当前字符串中出现的位置
        int : 第一次出现的下标 0 开始
        找不到 返回 -1
    int = lastIndexOf("");
        从后往前找
        int : 第一次出现的下标 0 开始
        找不到 返回 -1
    int = length();
        获取字符串的长度

    * boolean  = matches(String regex);
        "hello".matches("规则");
        使用hello字符串和规则进行匹配
        如果匹配成功(字符串符合定义的规则) 返回true
    * String = replace(oldChar,newChar);
        替换 : 
    String = replaceAll(String regex,String replacement);
    * String[]  = split(String regex);
        分隔,打断

    * String = subString(beginIndex);
        取子串
        从beginIndex位置获取到最后
    * String = subString(beginIndex,endIndex);
        取子串
        从beginIndex位置获取到endIndex

    String = toLowerCase();
        返回小写
    String = toUpperCase();
            返回大写
    String = trim();
        去除字符串 左右 两端的空格
        
StringBuffer使用 : 
    使用缓冲区解决每次拼接都产生新字符串的问题。
    

package com.briup.ch9;

import java.util.Arrays;

public class Test6 {
	public static void main(String[] args) {
		//创建字符串 1
		//            a  b  c  d   e
		byte[] but = {97,98,99,100,101};
		String s1 = new String(but,0,3);
		System.out.println(s1);
		
		//创建字符串 2
		String s2 = new String("abc");
		String s4 = new String("abc");
		
		String s3 = "abc";
		
		System.out.println(s2==s3);//地址 false
		System.out.println(s2.equals(s3)); //比较值 true
		System.out.println(s2==s4);//地址 false
		
		//字符串中常用的方法 :
		// API 
		//String 内部是 一个char[] value;
		String s5 = "hello world";
		char charAt = s5.charAt(0);
		System.out.println(charAt);
		
		String s11 = "taom";//97
		String s12 = "teom";//101
		int compareTo = s11.compareTo(s12);
		System.out.println(compareTo);
		//负数: s11   <  s12
		
		String s21 = "tom";
		String s22 = new String("tom");
		boolean equals = "".equals(null);
		System.out.println(equals);
		//运行
		//ArrayIndexOutOf
		//ClassCase
		//NullPointer
		
		String s31 = "hello world";
		int indexOf = s31.indexOf("low");
		System.out.println(indexOf);
		
		String s41 = "hello world";
		int lastIndexOf = s41.lastIndexOf("lo");
		System.out.println(lastIndexOf);
//		//java正则表达式
		String s51 = "tam";
		System.out.println(s51.matches("tom"));
		
		//量词 控制数量
		System.out.println(s51.matches("t.{2,4}m"));
		//数量 至少3个
		System.out.println(s51.matches("t.{3,}m"));
		//数量任意 *
		System.out.println(s51.matches("t.*m"));
		//数量 0 - 1  ?
		System.out.println(s51.matches("t.?m"));
		//数量 1-n  +
		System.out.println(s51.matches("t.+m"));
		
		// [] 方括号表达式 ,代表一个字符串,这个字符只能是[]中出现过的 
		s51 = "t_m";
		System.out.println(s51.matches("t[A-z0-9]m"));
		//伪代码,表示中文的范围
		System.out.println(s51.matches("t[\u4e00-\u8888]m"));
		//数字字母下划线
		System.out.println(s51.matches("t\\wm"));
		
		
		//正则约束 邮箱 
		//xiongzg@briup.com
		
		//替换
		String s61 = "你好to好m";
		String replace = s61.replace("好", "");
		System.out.println(replace);
		
		String s71 = "tom lisi wangwu zhao";
		String[] split = s71.split(" ");
		System.out.println(Arrays.toString(split));
		
		//拼接字符串...StringBuffer
		String s81 = "hello";
		//使用hello 字符串 创建 StringBuffer
		StringBuffer sb = new StringBuffer(s81);
		//拼接
		sb.append("tom");
		sb.append("lisi");
		sb.append("赵六");
		//StringBuffer缓冲区-->转换为字符串
		String string = sb.toString();
		System.out.println(string);
		
		
		char[] v1 = new char[10];//存放出现过的字符
		int index = 0;
		int[] v2 = new int[10];//存放出现过字符的次数
		for(int i = 0;i<s5.length();i++) {
			char charAt2 = s5.charAt(i);
			v1[index] = charAt2;
			v2[index] = 0;
		}
    
        s511 = "t_m";
		System.out.println(s511.matches("tm"));
		System.out.println(s511.matches("t[A-z0-9]m"));//ASCALL码表中:A-z之间包含_ [ ] ^ `
		System.out.println(s511.matches("t[\u4e00-\u8888]m"));
		System.out.println(s511.matches("t.{1,}m"));//中文算一个,因为String底层是char数组
		System.out.println(s511.matches("t.*m"));
		
		System.out.println("人".getBytes().length);//UTF-8下,中文占3个字符
		System.out.println("a".getBytes().length);
		
		System.out.println(s511.matches("t\\wm"));//数字字母下划线   \w
		
		String s611 = "你好to_好m";
		String replace = s611.replace("好", " ");//替换所有的 好 为空格
		System.out.println(replace);
		System.out.println(s611.replaceAll("\\w","a" ));//不同点是:可以基于正则表达式的替换,
		
		String s711 = " tom lis wangwu  zhao";
		String[] split = s711.split(" ");//每一个空格都会识别
		System.out.println(Arrays.toString(split));
		
		
		
		
	}
}

StringBuilder与StringBuffer:


注意:重写:返回值类型也可以不同,但返回值类型必须是父子类的关系

// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package com.huawei.it.jalor5.upload.support.impl; import com.huawei.his.jalor.helper.StoreHelper; import com.huawei.it.env.support.EnvHolder; import com.huawei.it.jalor5.core.annotation.JalorOperation; import com.huawei.it.jalor5.core.annotation.SecurityPolicy; import com.huawei.it.jalor5.core.config.ApplicationConfigProperties; import com.huawei.it.jalor5.core.exception.ApplicationException; import com.huawei.it.jalor5.core.exception.ExceptionHandler; import com.huawei.it.jalor5.core.io.CheckResult; import com.huawei.it.jalor5.core.io.FileInfoVO; import com.huawei.it.jalor5.core.io.IFileContentHandler; import com.huawei.it.jalor5.core.ioc.Jalor; import com.huawei.it.jalor5.core.log.ILogger; import com.huawei.it.jalor5.core.log.JalorLoggerFactory; import com.huawei.it.jalor5.core.util.CollectionUtil; import com.huawei.it.jalor5.core.util.FileUtil; import com.huawei.it.jalor5.core.util.JsonUtil; import com.huawei.it.jalor5.core.util.PathUtil; import com.huawei.it.jalor5.core.util.StringUtil; import com.huawei.it.jalor5.core.util.XssStringUtil; import com.huawei.it.jalor5.htmlarea.HtmlAreaApplicationException; import com.huawei.it.jalor5.logs.LogVO; import com.huawei.it.jalor5.logs.service.ILogService; import com.huawei.it.jalor5.registry.RegistryVO; import com.huawei.it.jalor5.registry.service.IRegistryQueryService; import com.huawei.it.jalor5.upload.support.FolderSplitType; import com.huawei.it.jalor5.upload.support.IUploadFileConsumer; import com.huawei.it.jalor5.upload.support.IUploadSupportService; import com.huawei.it.jalor5.upload.support.UploadException; import com.huawei.it.jalor5.upload.support.UploadSettingVO; import com.huawei.it.jalor5.web.support.internal.impl.RequestUtil; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileCountLimitExceededException; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.RequestContext; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletRequestContext; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component("/servlet/upload") public class UploadServlet extends HttpServlet { private static final String UPLOAD_ERROR = "huawei.jalor5.upload.support.00010001"; private static final Set<String> FILE_TYPE = new HashSet(Arrays.asList("der", "cer", "txt", "sql", "pfx", "jks", "crl", "pem")); private static final int FORM_BUFFER_SIZE = 2000; private static final long serialVersionUID = -460601782542501990L; private static int currentUploading = 0; private static int defaultMaxUploading = 10000; private static final ILogger LOGGER = JalorLoggerFactory.getLogger(UploadServlet.class); @Autowired( required = false ) private transient IUploadSupportService uploadSupportService; @Autowired( required = false ) private transient ILogService logService; @Autowired( required = false ) private transient IRegistryQueryService registryService; public UploadServlet() { } @JalorOperation( policy = SecurityPolicy.AllSystemUser ) public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { HttpServletRequest httpRequest = RequestUtil.getHttpServletRequest(request); LOGGER.debug("Upload Query String:{0}", new Object[]{httpRequest.getQueryString()}); String uploadType = request.getParameter("ulType"); Map<String, String> queryParams = RequestUtil.getRequestQueryParams(httpRequest); try { this.checkUploadingTimes(); Map<String, String> convertedMap = CollectionUtil.convert(request.getParameterMap()); convertedMap.put("taskType", queryParams.get("taskType")); Object obj = this.processUpload(httpRequest, uploadType, convertedMap); String json = JsonUtil.getJsonString(obj); LOGGER.debug("Object return:{0}", new Object[]{json}); String pattern = null; String uploadCleanXSS = "false"; if (null != this.registryService) { RegistryVO registryVO = this.registryService.findRegistryByPathNoAssertInternal("App.Security.XssFilterMatcher", false); if (null != registryVO) { pattern = registryVO.getValue(); } uploadCleanXSS = this.registryService.findValueByPath("App.Security.UploadCleanXSS", false, "false"); } response.setContentType("text/html"); if ("true".equalsIgnoreCase(uploadCleanXSS)) { response.getWriter().write(XssStringUtil.cleanXSS(json, pattern)); } else { response.getWriter().write(json); } } catch (UploadException ex) { LOGGER.error(ex); throw new ServletException(ex); } catch (ApplicationException ex) { LOGGER.error(ex); throw new ServletException(new UploadException("huawei.jalor5.upload.support.00010001", ex)); } catch (RuntimeException | FileUploadException ex) { LOGGER.error(ex); throw new ServletException(new UploadException("huawei.jalor5.upload.support.00010001", ex, ExceptionHandler.getFriendlyFault(ex).getMessage())); } finally { this.completeUploadingTimes(); } } private void completeUploadingTimes() { synchronized(UploadServlet.class) { if (currentUploading > 0) { --currentUploading; } } } private void checkUploadingTimes() { if (null != this.registryService) { int maxTimes = Integer.parseInt(this.registryService.findValueByPath("App.Security.MaxUploadingTimes", true, defaultMaxUploading)); synchronized(UploadServlet.class) { ++currentUploading; if (currentUploading > maxTimes) { throw new IllegalArgumentException("当前系统正在处理的上传次数超过最大上限,若需要加大上限,请修改数据字典App.Security.MaxUploadingTimes值"); } } } } private Object processUpload(HttpServletRequest request, String uploadType, Map<String, String> parameters) throws ApplicationException, FileUploadException { IUploadFileConsumer uls = this.uploadSupportService.findUploadConsumer(uploadType); if (!uls.validatePrivilege(parameters)) { throw new UploadException("huawei.jalor5.security.00010001"); } else { DiskFileItemFactory factory = new DiskFileItemFactory(); UploadSettingVO uploadSetting = uls.getUploadSetting(parameters); factory.setSizeThreshold(uploadSetting.getMaxMemory()); File file = new File(uploadSetting.getTempRepository()); PathUtil.makeDirs(file); factory.setRepository(file); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(uploadSetting.getMaxFileSize() + 2000L); if ("HtmlAreaImage".equals(uploadType)) { String value = this.registryService.findValueByPath("App.Security.HtmlArea", false, "1"); if ("1".equals(value)) { throw new HtmlAreaApplicationException("huawei.jalor5.Htmlarea.00020007"); } } RequestContext requestContext = new ServletRequestContext(request); long rLength = (long)requestContext.getContentLength(); long setLength = upload.getSizeMax(); LOGGER.debug("Upload image ok?:realLength={0},set={1}", new Object[]{rLength, setLength}); if (rLength > setLength) { throw new UploadException("huawei.jalor5.upload.support.00010004", new Object[]{rLength, setLength}); } else { List<FileItem> items = upload.parseRequest(request); this.validateFileCount(items, uploadSetting.getMaxFileCount()); return this.processFileItems(parameters, uploadSetting, uls, items); } } } private void validateFileCount(List<FileItem> items, int maxFileCount) throws FileCountLimitExceededException { if (!CollectionUtil.isNullOrEmpty(items) && maxFileCount != -1) { long fileCount = 0L; for(FileItem fileItem : items) { if (!fileItem.isFormField()) { ++fileCount; } if (fileCount > (long)maxFileCount) { throw new FileCountLimitExceededException("attachment", (long)maxFileCount); } } } } private Object processFileItems(Map<String, String> parameters, UploadSettingVO uploadSetting, IUploadFileConsumer consumer, List<FileItem> items) throws ApplicationException { Iterator<FileItem> iter = items.iterator(); List<FileInfoVO> files = new ArrayList(); while(iter.hasNext()) { FileItem item = (FileItem)iter.next(); if (!item.isFormField()) { if (item.getSize() == 0L) { throw new UploadException("huawei.jalor5.upload.support.00010003"); } this.validateExtention(uploadSetting, item); FileInfoVO fileInfo = new FileInfoVO(); String fileStorePath = MessageFormat.format("{0}/{1}", consumer.getUploadSetting(parameters).getRepository(), this.generatePathPart(uploadSetting)); PathUtil.makeDirs(fileStorePath); String fileStore = MessageFormat.format("{0}/{1}.jlr", fileStorePath, UUID.randomUUID().toString()); File uploadedFile = FileUtils.getFile(new String[]{fileStore}); Map<String, IFileContentHandler> downloadHandlersMap = Jalor.getContext().getBeansOfType(IFileContentHandler.class); if (downloadHandlersMap.size() > 0) { try { item.write(uploadedFile); fileInfo.setDisplayName(FileUtil.getFileName(item.getName())); fileInfo.setFileSize(item.getSize()); if (fileStore.contains("..")) { throw new IllegalArgumentException("fileStore path [" + fileStore + "] not support string [..]"); } fileInfo.setFilePath(fileStore); } catch (Exception ex) { LOGGER.error2("写文件异常,fileName:[{0}],exception:[{1}]", new Object[]{item.getName(), ex.getMessage()}); } } this.executeFileContentHandler(parameters, fileInfo, uploadedFile, downloadHandlersMap); fileStore = this.writeFile(item, uploadedFile); fileInfo.setDisplayName(FileUtil.getFileName(item.getName())); fileInfo.setFileSize(item.getSize()); fileInfo.setFilePath(fileStore); files.add(fileInfo); } } this.sendlogMessage((String)parameters.get("ulType"), files); return consumer.processFiles(parameters, files); } private void executeFileContentHandler(Map<String, String> parameters, FileInfoVO fileInfo, File uploadedFile, Map<String, IFileContentHandler> downloadHandlersMap) throws ApplicationException { for(Map.Entry<String, IFileContentHandler> entry : downloadHandlersMap.entrySet()) { Object result = ((IFileContentHandler)entry.getValue()).handler(parameters, fileInfo, "upload"); if (result != null && result instanceof CheckResult) { CheckResult reslut = (CheckResult)result; if (!reslut.isContinuation()) { if (uploadedFile.exists()) { uploadedFile.delete(); } throw new UploadException("huawei.jalor5.upload.support.00010006"); } } } } private void sendlogMessage(String type, List<FileInfoVO> files) throws ApplicationException { LogVO logVO = new LogVO(); logVO.buildAotoAttribute(); logVO.setClazz("UploadServlet"); logVO.setOperation("Upload"); logVO.setLogType(0); StringBuffer message = new StringBuffer(); for(FileInfoVO fileInfoVO : files) { message.append(", " + fileInfoVO.getDisplayName()); } String mess = null; if (message.length() > 1) { mess = message.substring(1, message.length()); } logVO.setStatus(1); logVO.setModule(type + " Upload"); logVO.setServerName(RequestUtil.getServerName()); logVO.setMessage("upload file name is " + mess); if (this.logService != null) { this.logService.asyncCreateLog(logVO); } else { try { LOGGER.info("logMessage: " + JsonUtil.objectToJson(logVO)); } catch (IOException ex) { LOGGER.info(ex); } } } private String writeFile(FileItem item, File uploadedFile) throws UploadException { try { if (!uploadedFile.exists()) { item.write(uploadedFile); } String region = ""; String bucketName = ""; try { PathUtil.canonicalPath(uploadedFile.getPath()); return StoreHelper.store(uploadedFile.getPath()); } catch (Exception ex) { LOGGER.error(ex); return uploadedFile.getPath(); } } catch (Exception ex) { throw new UploadException("huawei.jalor5.upload.support.00010001", ex); } } private void validateExtention(UploadSettingVO uploadSetting, FileItem item) throws UploadException { if (!this.isFileTypesAllowed(item.getName(), uploadSetting.getFileTypesAllowed())) { LOGGER.debug("File type {0} is not valid for {1}", new Object[]{FileUtil.getExtension(item.getName()), item.getName()}); throw new UploadException("huawei.jalor5.upload.support.00010002", StringUtil.join(uploadSetting.getFileTypesAllowed(), ",")); } else if (PathUtil.isInvalidPath(item.getName())) { throw new UploadException("huawei.jalor5.upload.support.00010002", "Path contains .."); } else if ("true".equals(ApplicationConfigProperties.getContextProperty("checkRealsFile")) && !this.checkRealFileType(uploadSetting, item)) { throw new UploadException("huawei.jalor5.upload.support.00010002", StringUtil.join(uploadSetting.getFileTypesAllowed(), ",")); } } private boolean isFileTypesAllowed(String fileName, List<String> fileTypesAllowed) { for(String fileType : fileTypesAllowed) { if (StringUtil.toLower(fileName).endsWith("." + StringUtil.toLower(fileType))) { return true; } } return false; } private String generatePathPart(UploadSettingVO uploadSetting) { if (uploadSetting.getFileSplitType().equals(FolderSplitType.ByDay)) { SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd"); return dayFormat.format(new Date()); } else { return com.huawei.it.jalor5.core.request.impl.RequestContext.getCurrent().getUser().getUserAccount(); } } private boolean checkRealFileType(UploadSettingVO uploadSetting, FileItem item) { LOGGER.info("check file real_code."); InputStream inputStream = null; label183: { boolean ex; try { if (!this.fileTypeInWhitelist(item)) { inputStream = item.getInputStream(); byte[] byt = new byte[4]; inputStream.read(byt, 0, byt.length); String code = this.bytesToHexString(byt); LOGGER.info("The file real_code is :" + code); inputStream.close(); HashMap<String, String> mimeTypeMap = null; try { mimeTypeMap = (HashMap)Jalor.getContext().getBean("HashMap." + EnvHolder.getApplication(), HashMap.class); } catch (Exception var24) { LOGGER.info("can&#39;t find Hash.Bean, We will load from local! Now,loading from HashMap.default.."); mimeTypeMap = (HashMap)Jalor.getContext().getBean("HashMap.default", HashMap.class); } String realType = (String)mimeTypeMap.get(code); if (StringUtil.isNullOrEmpty(realType)) { LOGGER.error("The file real_code cannot be discovered. Please configure it."); boolean var29 = false; return var29; } List<String> allowFileType = uploadSetting.getFileTypesAllowed(); boolean checkFirstStep = StringUtil.isNullOrEmpty(realType) ? false : this.checkFileType(realType, allowFileType); if (!checkFirstStep) { break label183; } boolean var10 = true; return var10; } ex = true; } catch (Exception ex) { LOGGER.error("UploadServlet check file real Type ERROR,the message is :" + ex); break label183; } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException var23) { } } } return ex; } LOGGER.error("The file real_code error ,Please configure it."); return false; } private boolean fileTypeInWhitelist(FileItem item) { String endItem = StringUtil.toLower(item.getName().substring(item.getName().lastIndexOf(".") + 1)); String whitelist = ApplicationConfigProperties.getContextProperty("whiteListUlType"); if (!StringUtil.isNullOrEmpty(whitelist) && whitelist.indexOf(endItem) != -1) { return true; } else { return FILE_TYPE.contains(endItem); } } private boolean checkFileType(String type, List<String> allowType) { boolean result = false; if (!StringUtil.isNullOrEmpty(type) && allowType.size() >= 1) { for(String t : allowType) { if (type.toLowerCase(Locale.ROOT).contains(t.toLowerCase(Locale.ROOT))) { result = true; break; } } return result; } else { return result; } } private String bytesToHexString(byte[] src) { StringBuilder sbBuilder = new StringBuilder(); if (src != null && src.length > 0) { for(int i = 0; i < src.length; ++i) { String str = Integer.toHexString(src[i] & 255).toUpperCase(Locale.getDefault()); if (str.length() < 2) { sbBuilder.append(0); } sbBuilder.append(str); } return sbBuilder.toString(); } else { return null; } } }
最新发布
08-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值