最近喜欢在知乎做笔记,这个跟JFinal有点关系,顺便copy来这。
原文:https://zhuanlan.zhihu.com/p/31818968
公司的测试服务器用的是Centos服务器,平时产品做好的原型都要拜托我上传上去。上传多了,嫌烦,花一个下午做了个部署脚本,后台小应用挂在Tomcat,这样让产品自己上传就一了百了。
后台基于JFinal3.3,极其简单:
public void doUpload(){
String method = getRequest().getMethod();
if ("GET".equals(method) ) {
redirect("/upload");
return;
}
UploadFile uf = getFile();
if (uf == null) {
redirect("/upload");
return;
}
LogKit.info("执行上传:"+ uf.getFileName());
try {
Runtime.getRuntime().exec("/usr/local/tomcat/apache-tomcat-7.0/bin/demo-upload.sh");
renderJson(RetKit.ok("上传成功,请访问 http://127.0.0.1/"+uf.getFileName()));
} catch (IOException e) {
LogKit.error(e.getMessage());
renderJson(RetKit.fail(e.getMessage()));
}
}脚本代码:
echo "开始执行脚本......"
#文件根目录
upload_dir="/usr/local/tomcat/apache-tomcat-7.0/webapps/demo-upload/upload";
target_dir="/usr/local/tomcat/apache-tomcat-7.0/webapps";
echo "复制zip文件到webapps......"
find ${upload_dir} -name "*.zip" -type f -exec cp {} ${target_dir} \;
echo "开始解压......"
cd ${target_dir};
find ${upload_dir} -name '*.zip' -exec unzip -o {} \;
echo "解压成功......"
echo "删除zip"
find ${target_dir} -name "*.zip" | xargs rm -rf
find ${upload_dir} -name "*.zip" | xargs rm -rf
echo "结束行脚本......"前台界面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>原型上传</title>
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
<!--<form action="upload/doUpload" method="post" enctype="multipart/form-data">
<p>First name: <input type="file" name="fname" /></p>
<input type="submit" value="Submit" />
</form>-->
<el-row>
<el-col :span="5" :offset="10">
<el-card class="box-card">
<el-upload class="upload-demo" action="upload/doUpload" :on-preview="handlePreview" :before-upload="beforeAvatarUpload" :on-success="handleSuccess" :on-remove="handleRemove" multiple :limit="1" :on-exceed="handleExceed" :file-list="fileList">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传zip文件</div>
</el-upload>
</el-card>
</el-col>
</el-row>
</div>
</body>
<!-- 先引入 Vue -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
new Vue({
el: '#app',
data: function() {
return {
fileList: []
};
},
methods: {
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
},
handleExceed(files, fileList) {
this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
},
handleSuccess(res, file, fileLis) {
if(res.success) {
this.$message.success(res.msg);
} else {
this.$message.error("请联系老徐,顺便截图该报错信息:" + res.msg);
}
},
beforeAvatarUpload(file) {
const isZip = file.type === 'application/zip';
if(!isZip) {
this.$message.error('上传文件只能是 zip 格式!');
}
return isZip;
}
}
})
</script>
</html>