javaisnotnull
A. java里如何根据某个字段是是否为空,从oracle数据库中分别取出该字段为空和不为空的数据呢
为空 sql 命令是 is null
不为空 sql 命令是 is not null
B. java如何根据word模板生成word文档
首先是action的createDoc方法:
[java]
/**
* 通过HttpCient调用报告服务器的方法生成报告 DOC
*/
public String createDoc() throws Exception {
//定义放回成功与否的判断码
String prMsg="";
// 获取当前登录的用户
UserVo userVo = CommonUtils.getUserMessage();
//获取模版类型
docType = Struts2Utils.getParameter("docType");
//重新创建文档
String creatOrnot = Struts2Utils.getParameter("creatOrnot");
//获取组组编号参数
workgroupId = Struts2Utils.getParameter("workgroupId");
//获取评估用例实例ID参数
evtcaseInstId = Struts2Utils.getParameter("evtcaseInstId");
if(CommonUtils.isNotNull(docType)){
//获取项目Id
projectId = Struts2Utils.getParameter("projectId");
if(!CommonUtils.isNotNull(projectId)){
if(CommonUtils.isNotNull(this.getIdFromSession("PM_PROJECTID"))){
projectId = this.getIdFromSession("PM_PROJECTID").toString();
}else{
Struts2Utils.getRequest().setAttribute("msg", "请先选择项目!");
}
}
if(CommonUtils.isNotNull(projectId)){
prMsg = infoSystemDescService.downloadFileByUrl(projectId, userVo.getUserId(), workgroupId, evtcaseInstId, docType, creatOrnot);
}
}
return "docList";
}
注:在我贴出来的代码中,能看懂就行了,有些不用管他(可能是其他业务方面的判断),关于最后返回的prMsg---代表各种状态 主要表示成功与否或者是出错的信息。
接着我贴出service层的方法downloadFileByUrl
[java]
</pre><p></p><p></p><pre name="code" class="java"><pre name="code" class="java">/**
* 功能:
* 1.(生成报告文档)
* 2.保存指定URL的源文件到指定路径下
* @param projectId
* @param userId
* @param workgroupId
* @param evtcaseInstId
* @param docType
* @param creatOrnot
* @return
* @throws Exception
*/
@SuppressWarnings("deprecation")
public synchronized String downloadFileByUrl(String projectId,String userId,String workgroupId,String evtcaseInstId,String docType,String creatOrnot) throws Exception {
String msg = "1";//"1":默认为创建成功的提示信息 "2":标识创建失败
String srcUrl = ""; //报告服务器的执行路径
HttpResponse response = null;
FileOutputStream out = null;
HttpClient httpclient = null;
HttpGet httpget = null;
long time1 = System.currentTimeMillis();
//获取保存后的路径
TProjDoc projDoc = projectDocDao.findFileByType(userId, Integer.parseInt(docType), Long.parseLong(projectId), workgroupId,evtcaseInstId);
if(projDoc == null || (projDoc != null && CommonUtils.isNotNull(creatOrnot) && creatOrnot.equals("1"))){ //FT_任务编号_[FID]
try {
//获取报告服务器的执行路径
srcUrl = xmlPathDef.getActionUrl(docType, projectId,userId,workgroupId,evtcaseInstId);
HttpParams httpParams = new BasicHttpParams();
// 设置最大连接数
ConnManagerParams.setMaxTotalConnections(httpParams, 1);
// 设置获取连接的最大等待时间
//ConnManagerParams.setTimeout(httpParams, 6000);
// 设置每个路由最大连接数
ConnPerRouteBean connPerRoute = new ConnPerRouteBean(1);
ConnManagerParams.setMaxConnectionsPerRoute(httpParams,connPerRoute);
// 设置连接超时时间
HttpConnectionParams.setConnectionTimeout(httpParams, 6000);
// 设置读取超时时间
if(docType.toString().equals(XmlPathDef.SPOTTEST_DOC) && docType.toString().equals(XmlPathDef.FTEST_DOC)){
HttpConnectionParams.setSoTimeout(httpParams, 2400000);
}else{
HttpConnectionParams.setSoTimeout(httpParams, 600000);
}
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(httpParams, registry);
httpclient = new DefaultHttpClient(connectionManager, httpParams);
httpget = new HttpGet(srcUrl);
//执行返回
response = httpclient.execute(httpget);
//如果是本机既当服务器,又当报表服务器,那么就只生成一遍
String ipvalues = xmlPathDef.getRepUrl();
if(CommonUtils.isNotNull(ipvalues)){
if(ipvalues.indexOf(":") != -1){
ipvalues = ipvalues.substring(0,ipvalues.lastIndexOf(":"));
}
}
HttpEntity entity = response.getEntity();
//获取保存后的路径
projDoc = projectDocDao.findFileByType(userId,Integer.parseInt(docType), Long.parseLong(projectId), workgroupId,evtcaseInstId);
String filePath = "";
if(projDoc != null)
filePath = projDoc.getPath();
if(CommonUtils.isNotNull(filePath)){
String basepath = XmlPathDef.getBasePath();
String outFilePath = (basepath + filePath).replaceAll("\\\\", "\\/");
XmlPathDef.isExists(outFilePath);
File wdFile = new File(outFilePath);
out = new FileOutputStream(wdFile);
int l;
byte[] tmp = new byte[2048];
while ((l = instream.read(tmp)) != -1) {
out.write(tmp, 0, l);
}
out.flush();
out.close();
System.out.println("****************************** ");
System.out.println("");
System.out.println("*************** 恭喜! 报告创建成功 结束 ***************");
System.out.println("");
}else{
msg = "8";//说明word创建成功,但是数据没有保存成功
response = null;
}
}else{
msg = "2";
}
} catch (ClientProtocolException e) {
msg = "7";
e.printStackTrace();
} catch (IOException e) {
msg = "7";
logger.error("数据库报告服务器地址配置错误或网络不通!!2.连接是否超时" + e.getMessage());
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
msg = "7";
logger.error("数据库报告服务器地址配置错误或网络不通!!2.连接是否超时" + e.getMessage());
e.printStackTrace();
}
}
}
}
long time2 = System.currentTimeMillis();
long numTime = time2 - time1;
if(docType.toString().equals(XmlPathDef.SPOTTEST_DOC) && docType.toString().equals(XmlPathDef.FTEST_DOC)){
if(numTime >= 2401000){
msg = "9";
}
}else{
if(numTime >= 601000){
msg = "9";
}
}
System.out.println("");
String loggerinfo = "********* 报告类型为 :" + docType + " 执行时间为: " + (time2 - time1) /1000 + " 秒!***************";
System.out.println(loggerinfo);
System.out.println("");
System.out.println("*****************************");
logger.info(loggerinfo);
return msg;
}
C. java菜鸟求助,关于判断输入是否非空
1. 检查字符串是否为空:
static boolean isBlank(CharSequence str) 判断字符串是否为空或null;
static boolean isNotBlank(CharSequence str) 判断字符串是否非空或非null;
java 的StringUtils包
StringUtils.isBlank(CharSequence str) 判断字符串是否为空或null; 这么用就行
D. java notnull 怎么实现的
not null指判断某个对象非空
Object obj = new Object();
if(obj != null){
System.out.println("obj is not null");
}
E. java判断字符串是否为空
方法一:if(变量.isEmpty())返回true表示,变量中存储的字符串为空,返回false表示变量中存储的的字符串不为空
方法二:if(变量.length()==0);判断变量的长度,长度为0表示变量中存储的字符串为空
方法三:if(变量.equals(""))使用equals比较值,相同返回true,相异返回false;
tips:不推荐用 “变量==null" 来对字符串进行判断。
举例: String str=""; 这种情况,虽然str中不存在值,但是也不等于null
于是得出结论: ==是用来判断路径是否相同