ps:zip不推荐使用shell脚本解压:会破坏JAVA的跨平台性
/**
* .调用shell执行指令 cmdline
* @param cmdline:执行指令
* @param directory:
* @return
*/
public static ArrayList<String> command(final String cmdline,final String directory) throws IOException{
String line = null;
InputStream in = null;
InputStreamReader inr = null;
BufferedReader br = null;
try {
Process process = new ProcessBuilder(new String[] {"bash", "-c", cmdline}).redirectErrorStream(true).directory(new File(directory)).start();
ArrayList<String> output = new ArrayList<String>();
in = process.getInputStream();
inr = new InputStreamReader(in);
br = new BufferedReader(inr);
while ( (line = br.readLine()) != null )
output.add(line);
if (0 != process.waitFor())
return null;
return output;
} catch (InterruptedException e) {
return null;
} finally {
if(in!=null) {
in.close();
in = null;
}
if(inr!=null) {
inr.close();
inr = null;
}
if(br!=null) {
br.close();
br = null;
}
}
}
4.gz格式压缩包解压缩
JAVA_实现代码:
/**\解压缩.gz后缀的压缩包,输出到指定目录
* @param file:带解析File
* @param outputDir:指定输出路径
*/
public static void unTarGz(File file,String outputDir) throws IOException{
TarInputStream tarIn = null;
// List<String> list = null;
String path = outputDir;
try {
tarIn = new TarInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))),1024*2);
createDirectory(outputDir,null);//创建输出目录
TarEntry entry = null;
while((entry = tarIn.getNextEntry())!=null) {
if(entry.isDirectory()) {
createDirectory(path, entry.getName());
path = path + entry.getName();
}else {
File tmpFile = new File(outputDir+"/"+entry.getName());
createDirectory(tmpFile.getParent()+"/", null);//创建输出目录
OutputStream out = null;
try {
out = new FileOutputStream(tmpFile);
int length = 0;
byte[] b = new byte[2048];
while((length = tarIn.read(b))!=-1) {
out.write(b,0,length);
}
} catch (IOException e) {
throw e;
}finally {
if(out != null) {
out.close();
}
}
}
}
// return list;
} catch (IOException e1) {
throw new IOException("解压归档文件出现异常");
}finally {
try {
if(tarIn!=null) {
tarIn.close();
}
} catch (IOException e) {
throw new IOException("关闭tarFile出现异常");
}
}
}
8.特殊时间格式处理思路(待补充_专栏)
JAVA_实现代码:
/**\计算两个拉起阶段的劣化时间 时间格式:181013-20:09:32.416599,返回3位精度
* @param first :第一阶段
* @param next : 第二阶段
* @return
*/
public static String calculationTime(String first,String next) throws StringIndexOutOfBoundsException,IllegalArgumentException{
String result;
if(first ==null || next ==null || "".equals(first)|| "".equals(next)) {
return null;
}
if("0".equals(first)) {
if("0".equals(next)) {
result = "0";
}else {
result = "0";
}
}else if ("0".equals(next)) {
result = "0";
}else {
first = "20"+first.substring(0,2)+"-"+first.substring(2,4)+"-"+first.substring(4,6)+" "+first.substring(7);
next = "20"+next.substring(0,2)+"-"+next.substring(2,4)+"-"+next.substring(4,6)+" "+next.substring(7);
Timestamp p1 = Timestamp.valueOf(next);//减数
Timestamp b1 = Timestamp.valueOf(first);//被减数
long time1 = p1.getTime();
long time2 = b1.getTime();
result = String.valueOf(((float) ((float)(time1 - time2)/1000)));
if("0.0".equals(result)) {
result = "0";
}
}
return result;
}
10.数据导出功能:MyUtils-export.html
- 总结了 html excel pdf 格式 导出的一个 实现类: 具体思路如下
- 导出数据前对页面数据量(页面查询完将数据量保存到js全局变量)进行判断,如果大于65534行,Java poi 是不支持导出的
- js的导出function 推荐使用自执行函数,即定义和调用合为一体。我们创建了一个匿名的函数,并立即执行它,由于外部无法引用它内部的变量,因此在执行完后很快就会被释放,关键是这种机制不会污染全局对象。
- 导出文件名 推荐以 用户+当前时间的形式命名
- 前台将 导出文件类型 导出头内容 导出内容字段 控制分支 导出文件名 传递到后台 进行数据查询
- 后台获取传递参数,并捕获异常(在业务层捕获发送前台记录log,而不要在静态校验方法里),根据不同的请求参数进入不同的case分支获取后台数据传给导出方法导出。
11.平台_子系统登录登出权限系统设计
11.1设计思路
- 平台a管理应用b,c
- 访问平台a的login页面,通过SSO登录账号(所有子系统共用的登录系统)
- 通过链接页面跳转到不同的应用的子系统
- 这时候链接跳转的页面会因为session的作用域(同一个application项目共享session),会在服务器创建新的sessionId
- 子系统通过servlert的过滤器,实现对子系统的页面进行权限控制,具体实现为
- 子系统获取a平台传递的用户信息,放在session里管理,并重定向到权限Controller,权限Controller删除当前session的所有属性, 然后根据获取的用户信息去查询当前用户的系统权限 ,重新给当前用户赋予权限 和其他session属性
- 登出: 关闭并重新打开浏览器
- 所有系统的首页都禁用缓存
子系统过滤器
子系统的session管理Controller
excelUtil 补充方法: 用Map替代 对象 实现方法的封装
工具方法
/**
* 解析.xls格式的excel文件,获取excel信息
* @param is:excel文件流
* @return List<实体类>
*/
public static List<Map<String,String>> readXls(InputStream is,String[] array) throws IOException {
List<Map<String,String>> result =new ArrayList<Map<String,String>>(); //返回集
try {
HSSFWorkbook book = new HSSFWorkbook(is);
HSSFSheet sheet = book.getSheetAt(0); //获取第一页
for(int i=1; i<sheet.getLastRowNum()+1; i++) { //通常第一行都是标题,所以从第二行开始读取数据
HSSFRow row = sheet.getRow(i); //获取行信息
//遍历行
Map<String,String> map = new HashMap<String,String>(); //数据库实体类map对象
for(int j =0;j<array.length;j++) {
row.getCell(j).setCellType(Cell.CELL_TYPE_STRING); //设置列类型
map.put(array[j],row.getCell(j).getStringCellValue());
}
result.add(map); //数据集合导入
}
}catch(IOException e) {
throw new IOException("获取excel表格信息发生IO异常");
}
return result;
}
后台请求
path = (String)pathMap.get("excelpath")+ separator +loginUser;
//获取excel文件对应的List<实体类>
in = list.get(2).getInputStream();
String[] array = {"撼地神牛","敌法师","幻影刺客"};
// List<HaAdmin> userList = ExcelUtils.readXls(in);
List<Map<String,String>> userList = ExcelUtils.readXls(in,array);
//导入用户信息
parserProcessService.insertUserList(userList);
result.addProperty("type", "excelUpload");
db交互
Service
//批量插入用户信息
void insertUserList(List<Map<String,String>> list);
Mapper
<insert id="insertUserList" parameterType="java.util.Map">
insert ignore into
Dota2
("","","")
values
<foreach collection="list" item="item" separator=",">
(#{item.a},#{item.b},#{item.v})
</foreach>
</insert>
导入页面
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta content="" name="description">
<meta content="webthemez" name="author">
<script type="text/javascript" src="js/apps/jquery.min.js"></script>
<script src="js/libs/requirejs/require.js" data-main="js/apps/batchAdd" defer></script>
</head>
<style>
.jianli_apply {
display: block;
width: 100px;
height: 30px;
border: 1px solid #CCC;
background: #1E9FFF;
margin-top: 20px;
}
.jianli_apply a {
font-size: 14px;
padding: 5px 6px;
display: block;
color: #ffffff;
}
a{ text-decoration:none}
</style>
<body id = index>
<div>
<form id="layui" enctype="multipart/form-data" method="post">
<fieldset>
<p style="margin-left: 0; position: relative; width: 100%;">
<span style="float: left;font-weight: bold;">用户批量导入:</span>
<input type="hidden" name="checkme" value="Excel">
<input type="hidden" id=""name="baseVersionName">
<input name="excelUpload" id="excelUpload" type="file" style="margin-top: -5px; position: relative; display: inline-block; width: 50%; vertical-align: middle; border: 1px solid #795548; padding: 3px 12px; border-radius: 3px; float: left;">
<input type="button" id="excelUploadButton" value="上传" style="display: none">
<img id = baseUploadLoading src="images/loading.gif" style="width:5%;display: none;">
<span id="excelUploadTypeError" style="display: none; color: red; font-size: 14px; margin-top: 4px; float: left;"> Excel文件名错误</span>
<span id="excelUploadSucess" style="display: none; color: red; font-size: 14px; margin-top: 4px; float: left;"> 用户信息导入成功</span>
</p>
</fieldset>
</form>
<div class="jianli_apply">
<a href="file/HaBatchAddUser.xls" download="HaHaBatchAddUser.xls" ">获取导入模板</a>
</div>
</div>
</body>
</html>
导入Js
define([ 'jquery', 'backbone', 'json2', 'cookie','utils/urlUtils', 'utils/ajaxUtils','utils/responseCode',
'text!templates/HaModelUploadTpl.html','text!templates/departmentTpl.html','text!templates/areaTpl.html','page','views/commonView','custom','customscripts','metisMenu','bootstraps'], function
($, Backbone, JSON, cookie, Url, Ajax,ResponseCode,HaModelUploadTpl,departmentTpl,areaTpl,page,commonView,custom,customscripts,metisMenu,bootstraps) {
return Backbone.View.extend({
el : '#index',
departmentTemplate: _.template(departmentTpl),
areaTemplate:_.template(areaTpl),
events : {
"change #excelUpload":"ExcelUpload", //上传Excele模板
},
dataModel : {
},
initialize : function(options) {
commonView = new commonView();
new metisMenu;
this.init();
},
//初始化权限
init :function(){
},
//ajax回调函数——sucess
batchAddResult : function(data) {
$("#excelUploadSucess").show();
if(data.code == ResponseCode.COMMON_SUCCESS){
if(data.type == excelUpload){
$("#excelUploadSucess").show();
}
}
},
//ajax回调函数——error
error : function(data) {
},
ExcelUpload : function() {
//文件后缀校验
// $("#baseUploadLoading").show();
$("#excelUploadSucess").hide();
$("#excelUploadError").hide();
$("#excelUploadTypeError").hide();
$('#excelUpload').attr("readonly","readonly");
var file = document.getElementById("excelUpload").value;
var fileName = file.substring(file.lastIndexOf("\\"));
fileName = fileName.substring(1,fileName.length);
var fileType = fileName.substring(fileName.lastIndexOf("."));
if(fileName.startsWith("HaBatchAddUser") && ".xls"==fileType){
var formData = new FormData($("#layui")[0]);
Ajax.postWithError(Ajax.servletMap.parserHaController,
formData, this, this.batchAddResult,
this.error);
}else{
$("#excelUploadTypeError").show();
}
},
});
});
require.js
/**
* Created by Administrator on 2018/1/17.
*/
require.config({
appDir : "./",
baseUrl : "js/",
shim : {
'underscore' : {
exports : '_'
},
'backbone' : {
deps : [ 'underscore', 'jquery' ],
exports : 'Backbone'
},
'json2' : {
exports : 'JSON'
},
'cookie' : {
deps : [ 'jquery' ],
exports : 'jQuery'
},
'page':{
deps : [ 'jquery' ],
exports : 'page'
},
'custom':{
deps : [ 'jquery','metisMenu','bootstrap' ],
exports : 'metisMenu'
},
'customscripts':{
deps : [ 'jquery','metisMenu','bootstrap' ],
exports : 'jQuery'
},
'metisMenu':{
deps : [ 'jquery','bootstrap' ],
exports : 'jQuery'
},
'bootstrap':{
deps : [ 'jquery' ],
exports : 'jQuery'
},
'i18n' : {
deps : [ 'jquery' ],
exports : 'jQuery'
},
'bootstraps': {
deps : [ 'jquery' ],
exports : 'jQuery'
},
'datetimepicker':{
deps : [ 'jquery', 'bootstraps'],
exports : 'datetimepicker'
},
'datetimepickerfr':{
deps : [ 'jquery','bootstraps','datetimepicker'],
exports : 'datetimepickerfr'
},
'autocomplete':{
deps:['jquery'],
exports:'jQuery'
},
'combobox':{
deps:['jquery'],
exports:'jQuery'
},
'selectPage':{
deps:['jquery'],
exports:'jQuery'
},
},
paths : {
jquery : 'libs/jquery/jquery-1.10.2.min',
autocomplete:'libs/jquery/jquery.autocomplete',
combobox:'libs/jquery/jquery.ajax-combobox.min',
underscore : 'libs/backbone/underscore',
backbone : 'libs/backbone/backbone',
text : 'libs/requirejs/text',
json2 : 'libs/jquery/json2' ,
cookie : 'libs/jquery/jquery.cookie',
page:'libs/page/jquery.pagination.min',
custom:'libs/common/custom',
customscripts:'libs/common/customscripts',
metisMenu:'libs/common/metisMenu',
bootstrap:'libs/common/bootstrap.min',
i18n : 'libs/jquery/jquery.i18n.properties.min',
bootstraps:'libs/bootstrap/bootstrap.min',
datetimepicker:'libs/time/bootstrap-datetimepicker',
datetimepickerfr:'libs/time/bootstrap-datetimepicker.fr',
selectPage:'libs/jquery/selectpage.min',
}
});
require([ 'backbone','views/batchAddView'], function(Backbone, batchAddView) {
var batchAddView = new batchAddView();
return batchAddView;
});
页面
BatchAdd:function(othis){
layer.open({
type: 2,
title: false, //不显示标题栏
closeBtn: false,
area: '450px;',
shade: 0.8,
id: 'LAY_layuipro', //设定一个id,防止重复弹出
btn: ['退出'],
btnAlign: 'c',
moveType: 1,//拖拽模式,0或者1
content: Ajax.host + "/HaStatistics/batchAdd.html",
yes: function(index, layero){
window.location.href = Ajax.host + "/HaStatistics/administrator.html";
layer.close(index); //如果设定了yes回调,需进行手工关闭
},
success: function(layero){
/* var btn = layero.find('.layui-layer-btn');
btn.find('.layui-layer-btn0').click= window.location.href = Ajax.host + "/HaStatistics/administrator.html"; */
}
});
},