stringutils源码
⑴ java.lang.NullPointerException 求帮忙解决,源码附着如下
busiLogId 这个有可能为空。要具体看看CommonUtil.getBusiParam这个方法的返回值。
⑵ StringUtils.isNotEmpty(note.getName())的源码有吗或其具体使用
callNo.contains(" ")这个是判断 callNo是否包含空格,你可以数一下到底包含了几个空格。StringUtils.isNotEmpty(callNo)
仅能判断 是否为null或者空字符串""。包含空格是判断不了的。
比如 "123456"不符合条件,但是"123 456"却是符合条件的!
⑶ StringUtils是哪个包下的类
org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的(即如果输入参数String为null则不会抛出NullPointerException,而是做了相应处理,例如,如果输入为null则返回也是null等,具体可以查看源代码)。
除了构造器,StringUtils中一共有130多个方法,并且都是static的,
所以我们可以这样调用StringUtils.xxx()。
下面分别对一些常用方法做简要介绍:
1. public static boolean isEmpty(String str)
判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0
下面是示例:
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
⑷ 怎么解决randomstringutils生成数字乱码
源码:
public static String random(int count, boolean letters, boolean numbers) {
return random(count, 0, 0, letters, numbers);
}
那么20位的数字乱码可以使用下面来生成:
random(20, false, true)
⑸ myeclipse怎么看stringutils源码
1、如果没有附加源码的话,是看不到源码的,必须附加源码:右键项目-Build Path-Configure Build Path-Java Build Path-Libraries-选择jar包-点左边的+号-选择Source attatch-Edit-Browse-选择源码包就可以附加源码了。
2、附加源码后,要查看某个类/方法/变量的源码,只要选中类/方法名/变量名,然后按F3即可看到源码。
⑹ Spring源码解析哪本书好
解析的步骤: 1、加载web.xml、加载监听器<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 2、ContextLoaderListener 初始化initWebApplicationContext方法创建 org.springframework.web.context.support. XmlWebApplicationContext对象 3、XmlWebApplicationContext调用loadBeanDefinitions方法,该方法主要做两件事情:初始化XmlBeanDefinitionReader、获取applicationContext.xml配置文件的路径、然后把事情交给XmlBeanDefinitionReader来处理 4、XmlBeanDefinitionReader获取到applicationContext.xml配置文件的路径、读取配置文件的内容得到一个输入流、对输入流转码操作、然后封装成一个inputSource对象、再然后封装成一个document对象;在生成document对象的同事也生成了一个Resource对象、这两个对象分部是:document对象承载配置文件的主要内容信息、Resource承载配置文件的描述信息以及一些验证信息。 再由Resource对象创建一个XmlReaderContext。完成了以上操作XmlBeanDefinitionReader就把document对象和XmlReaderContext对象交给来处理 5、1)、对XmlReaderContext装饰成一个BeanDefinitionParserDelegate对象; 2)、迭代document对象、把document对象拆分成Element元素逐个逐个解析; 3)、使用BeanDefinitionParserDelegate装饰对象解析Element元素或者说标签。 if (absoluteLocation) { try { int importCount = getReaderContext().getReader().loadBeanDefinitions(location, actualResources); if (logger.isDebugEnabled()) { logger.debug("Imported " + importCount + " bean definitions from URL location [" + location + "]"); } } catch (BeanDefinitionStoreException ex) { getReaderContext().error( "Failed to import bean definitions from URL location [" + location + "]", ele, ex); } } else { // No URL -> considering resource location as relative to the current file. try { int importCount; Resource relativeResource = getReaderContext().getResource().createRelative(location); if (relativeResource.exists()) { importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource); actualResources.add(relativeResource); } else { String baseLocation = getReaderContext().getResource().getURL().toString(); importCount = getReaderContext().getReader().loadBeanDefinitions( StringUtils.applyRelativePath(baseLocation, location), actualResources); } if (logger.isDebugEnabled()) { logger.debug("Imported " + importCount + " bean definitions from relative location [" + location + "]"); } } catch (IOException ex) { getReaderContext().error("Failed to resolve current resource location", ele, ex); } catch (BeanDefinitionStoreException ex) { getReaderContext().error("Failed to import bean definitions from relative location [" + location + "]", ele, ex); } } Resource[] actResArray = actualResources.toArray(new Resource[actualResources.size()]); getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele)); } 解析alias标签的方法:
⑺ 求浏览器 web 大文件上传源代码
String fid = request.getHeader("id");
String blockIndex = request.getHeader("blockIndex");//基于1
String blockOffset = request.getHeader("blockOffset");//块偏移,相对于整个文件
String blockSize = request.getHeader("blockSize");//块大小(当前需要下载的大小)
String pathSvr = request.getHeader("pathSvr");//文件在服务器的位置
pathSvr = PathTool.url_decode(pathSvr);
if ( StringUtils.isBlank(fid)
||StringUtils.isBlank(blockIndex)
||StringUtils.isEmpty(blockOffset)
||StringUtils.isBlank(blockSize)
||StringUtils.isBlank(pathSvr))
{
response.setStatus(500);
response.setHeader("err","参数为空");
return;
}
File f = new File(pathSvr);
//文件不存在
if(!f.exists())
{
response.setStatus(500);
OutputStream os = response.getOutputStream();
System.out.println(String.format("%s 文件不存在",pathSvr));
os.close();
return;
}
long fileLen = f.length();
response.setContentType("application/x-download");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.addHeader("Content-Length",blockSize);
response.setDateHeader("Expires", 0);
OutputStream os = response.getOutputStream();
try
{
RandomAccessFile raf = new RandomAccessFile(pathSvr,"r");
int readToLen = Integer.parseInt(blockSize);
int readLen = 0;
raf.seek( Long.parseLong(blockOffset) );//定位索引
byte[] data = new byte[1048576];
while( readToLen > 0 )
{
readLen = raf.read(data,0,Math.min(1048576,readToLen) );
readToLen -= readLen;
os.write(data, 0, readLen);
}
os.flush();
os.close();
raf.close();
os = null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}
catch(Exception e)
{
response.setStatus(500);
os.close();
out.close();
e.printStackTrace();
}
finally
{
if(os != null)
{
os.close();
os = null;
}
out.clear();
out = pageContext.pushBody();
}%>
⑻ Java中判断对象为空的问题
首先来看一下工具StringUtils的判断方法:
StringUtils.isEmpty(CharSequence cs); //org.apache.commons.lang3包下的StringUtils类,判断是否为空的方法参数是字符序列类,也就是String类型StringUtils.isEmpty(Object str); //而org.springframework.util包下的参数是Object类,也就是不仅仅能判断String类型,还能判断其他类型,比如Long等类型。12345
一种是org.apache.commons.lang3包下的;
另一种是org.springframework.util包下的。这两种StringUtils工具类判断对象是否为空是有差距的:
从上面的例子可以看出第二种的StringUtils类更实用。
下面来看一下org.apache.commons.lang3的StringUtils.isEmpty(CharSequence cs)源码:public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0;
接下来是org.springframework.util的StringUtils.isEmpty(Object str)源码:public static boolean isEmpty(Object str) { return (str == null || "".equals(str));
基本上判断对象是否为空,StringUtils.isEmpty(Object str)这个方法都能搞定。
接下来就是判断数组是否为空
list.isEmpty(); //返回boolean类型。⑼ org.springframework.util.stringutils 是java哪个jar包下的
spring-core
我们经常会对字符串进行操作,spring已经实现了常用的处理功能。我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串。
工具类整理如下:
StringUtils.hasLength(null) = false
StringUtils.hasLength("") = false
StringUtils.hasLength(" ") = true
StringUtils.hasLength("Hello") = true
StringUtils.hasText(null) = false
StringUtils.hasText("") = false
StringUtils.hasText(" ") = false
StringUtils.hasText("12345") = true
StringUtils.hasText(" 12345 ") = true
//是否包含空白字符
StringUtils.containsWhitespace(null)=false
StringUtils.containsWhitespace("")=false
StringUtils.containsWhitespace("a")=false
StringUtils.containsWhitespace("abc")=false
StringUtils.containsWhitespace("abc")=false
StringUtils.containsWhitespace(" ")=true
StringUtils.containsWhitespace(" a")=true
StringUtils.containsWhitespace("abc ")=true
StringUtils.containsWhitespace("a b")=true
StringUtils.containsWhitespace("a b")
StringUtils.trimWhitespace(null)=null;
StringUtils.trimWhitespace("")="";
StringUtils.trimWhitespace(" ")="";
StringUtils.trimWhitespace("/t")="";
StringUtils.trimWhitespace(" a")="a";
StringUtils.trimWhitespace("a ")="a";
StringUtils.trimWhitespace(" a ")="a";
StringUtils.trimWhitespace(" a b ")="a b";
StringUtils.trimLeadingWhitespace(null)=null;
StringUtils.trimLeadingWhitespace("")="";
StringUtils.trimLeadingWhitespace(" ")="";
StringUtils.trimLeadingWhitespace("/t")="";
StringUtils.trimLeadingWhitespace(" a")="a";
StringUtils.trimLeadingWhitespace("a ")="a ";
StringUtils.trimLeadingWhitespace(" a ")="a ";
StringUtils.trimLeadingWhitespace(" a b ")="a b "
StringUtils.trimLeadingWhitespace(" a b c ")="a b c "
StringUtils.trimTrailingWhitespace(null)=null;
StringUtils.trimTrailingWhitespace(" ")="";
StringUtils.trimTrailingWhitespace("/t")="";
StringUtils.trimTrailingWhitespace("a ")="a";
StringUtils.trimTrailingWhitespace(" a")=" a";
StringUtils.trimTrailingWhitespace(" a ")=" a";
StringUtils.trimTrailingWhitespace(" a b ")=" a b";
StringUtils.trimTrailingWhitespace(" a b c ")=" a b c";
StringUtils.trimAllWhitespace("")="";
StringUtils.trimAllWhitespace(" ")="";
StringUtils.trimAllWhitespace("/t")="";
StringUtils.trimAllWhitespace(" a")="a";
StringUtils.trimAllWhitespace("a ")="a";
StringUtils.trimAllWhitespace(" a ")="a";
StringUtils.trimAllWhitespace(" a b ")="ab";
StringUtils.trimAllWhitespace(" a b c "="abc";
//统计一个子字符串在字符串出现的次数
StringUtils.countOccurrencesOf(null, null) == 0;
StringUtils.countOccurrencesOf("s", null) == 0;
StringUtils.countOccurrencesOf(null, "s") == 0;
StringUtils.countOccurrencesOf("erowoiueoiur", "WERWER") == 0;
StringUtils.countOccurrencesOf("erowoiueoiur", "x")=0;
StringUtils.countOccurrencesOf("erowoiueoiur", " ") == 0;
StringUtils.countOccurrencesOf("erowoiueoiur", "") == 0;
StringUtils.countOccurrencesOf("erowoiueoiur", "e") == 2;
StringUtils.countOccurrencesOf("erowoiueoiur", "oi") == 2;
StringUtils.countOccurrencesOf("erowoiueoiur", "oiu") == 2;
StringUtils.countOccurrencesOf("erowoiueoiur", "oiur") == 1;
StringUtils.countOccurrencesOf("erowoiueoiur", "r") == 2;
//字符串替换
String inString = "a6AazAaa77abaa";
String oldPattern = "aa";
String newPattern = "foo";
// Simple replace
String s = StringUtils.replace(inString, oldPattern, newPattern);
s.equals("a6AazAfoo77abfoo")=true;
// Non match: no change
s = StringUtils.replace(inString, "qwoeiruqopwieurpoqwieur", newPattern);
s.equals(inString)=true
s = StringUtils.replace(inString, oldPattern, null);
s.equals(inString)=true
// Null old pattern: should ignore
s = StringUtils.replace(inString, null, newPattern);
s.equals(inString)=true
//删除字符串
String inString = "The quick brown fox jumped over the lazy dog";
String noThe = StringUtils.delete(inString, "the");
noThe.equals("The quick brown fox jumped over lazy dog")=true;
String nohe = StringUtils.delete(inString, "he");
nohe.equals("T quick brown fox jumped over t lazy dog")=true;
String nosp = StringUtils.delete(inString, " ");
nosp.equals("")=true;
String killEnd = StringUtils.delete(inString, "dog");
killEnd.equals("The quick brown fox jumped over the lazy ")=true;
String mismatch = StringUtils.delete(inString, "dxxcxcxog");
mismatch.equals(inString)=true;
//删除任何字符
//源代码如下
//char c = inString.charAt(i);
//如果不存在 c 值,则返回 -1
//if (charsToDelete.indexOf(c) == -1) {
//out.append(c);
//}
String inString = "Able was I ere I saw Elba";
String res = StringUtils.deleteAny(inString, "I");
res.equals("Able was ere saw Elba")=true;
res = StringUtils.deleteAny(inString, "AeEba!");
res.equals("l ws I r I sw l")=true;
String mismatch = StringUtils.deleteAny(inString, "#@$#$^");
mismatch.equals(inString)=true;
//源代码如下 return (str != null ? "'" + str + "'" : null);
assertEquals("'myString'", StringUtils.quote("myString"));
assertEquals("''", StringUtils.quote(""));
assertNull(StringUtils.quote(null));
//将第一个字符改大写
StringUtils.capitalize(Str)
//将第一个个字符改小写
StringUtils.uncapitalize(str)
//mypath/myfile.txt" -> "myfile.txt
//获取字符串文件名和扩展名
StringUtils.getFilename("myfile").equals("myfile")=true;
StringUtils.getFilename("mypath/myfile".equals("myfile")=true;
StringUtils.getFilename("mypath/myfile".equals("myfile")=true;
StringUtils.getFilename("myfile.txt").equals("myfile.txt")=true;
StringUtils.getFilename("mypath/myfile.txt").equals("myfile.txt")=true;
//获取字符串扩展名,以.分隔
StringUtils.getFilenameExtension("myfile")=null;
StringUtils.getFilenameExtension("myPath/myfile")=null;
StringUtils.getFilenameExtension("myfile.").equals("")=true;
StringUtils.getFilenameExtension("myPath/myfile.").equals("")=true;
StringUtils.StringUtils.getFilenameExtension("myfile.txt").equals("txt")=true;
StringUtils.getFilenameExtension("mypath/myfile.txt").equals("txt")=true;
//舍去文件名扩展名
StringUtils.stripFilenameExtension(null)=true;
StringUtils.stripFilenameExtension("").equals("")=true;
StringUtils.stripFilenameExtension("myfile").equals("myfile")=true;
StringUtils.stripFilenameExtension("mypath/myfile").equals("mypath/myfile")=true;
StringUtils.stripFilenameExtension("myfile.").equals("myfile")=true;
StringUtils.stripFilenameExtension("mypath/myfile.").equals("mypath/myfile")=true;
StringUtils.stripFilenameExtension("mypath/myfile.").equals("mypath/myfile")=true;
StringUtils.stripFilenameExtension("myfile.txt").equals("myfile")=true;
StringUtils.stripFilenameExtension("mypath/myfile.txt").equals("mypath/myfile")=true;