当前位置: 首页 > news >正文

如何自己搭建网站seo交流qq群

如何自己搭建网站,seo交流qq群,网站建设中代码,专业做网盘资源收录分享的网站文章目录 前言一、linux命令执行二、使用步骤三、踩坑 前言 java 执行linux 命令; 本文模拟复制linux文件到指定文件夹后打zip包后返回zip名称,提供给下载接口下载zip; 一、linux命令执行 linux命令执行Process process Runtime.getRunti…

文章目录

  • 前言
  • 一、linux命令执行
  • 二、使用步骤
  • 三、踩坑

前言

java 执行linux 命令;
本文模拟复制linux文件到指定文件夹后打zip包后返回zip名称,提供给下载接口下载zip;

一、linux命令执行

linux命令执行Process process = Runtime.getRuntime().execProcess process = new ProcessBuilder(commands).start();

   /*** 执行Linux命令*/public static void execCommand(String commands) throws IOException {Process process = null;BufferedReader reader = null;try {//创建进程实例执行Linux命令process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", commands});// 获取标准输入流reader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line;while ((line = reader.readLine()) != null) {//log.info("linux----" + line);}// 等待进程结束int exitCode = process.waitFor();if (exitCode != 0) {log.error("执行Linux命令异常,exitCode={},linux command={}", exitCode, commands);throw new BusinessCheckException("执行Linux命令异常");}} catch (IOException | InterruptedException e) {log.error("执行Linux命令异常", e);throw new BusinessCheckException("执行Linux命令异常");} finally {if (reader != null) {reader.close();}if (process != null) {process.destroy();}}}

二、使用步骤

  1. 通过linux命令,打包目标数据到zip
public String downToZip(CorpQrCodeReq req, HttpServletResponse response) {StopWatch sw = new StopWatch();// 参数校验if (CollectionUtils.isEmpty(req.getCorpIds()) || CollectionUtils.isEmpty(req.getCorpNames())) {return "0";}// 生成保存目录文件夹String uuid = UUID.randomUUID().toString().replace("-", "");String baseUploadPath = "/tmp/zip/"+uuid+"/";FileUtil.mkdir(baseUploadPath);try {//生成二维码到临时目录List<String> enterpriseIds = req.getCorpIds();List<String> enterpriseNames = req.getCorpNames();List<String> command = new ArrayList<>(enterpriseIds.size());// 拼接被复制的文件地址for (int i = 0; i < enterpriseIds.size(); i++) {String path = this.qrCode(CorpQrCodeReq.builder().corpIds(Lists.newArrayList(enterpriseIds.get(i))).corpNames(Lists.newArrayList(enterpriseNames.get(i))).build());command.add(path + " ");}sw.stop();log.info(sw.getLastTaskName() + sw.getTotalTimeSeconds() + "s");sw.start("执行cp命令");String property = System.getProperty("line.separator");//拼接shell脚本String sb = " for i in  " + String.join(" ", command) +property +"do" +property +" cp -n  \"$i\" " + baseUploadPath +property +"done";//执行cp命令execCommand(sb);sw.stop();log.info(sw.getLastTaskName() + sw.getTotalTimeSeconds() + "s");sw.start("执行压缩命令");//执行压缩命令 统一压缩比一个一个压缩进去效率高 //-j忽略源文件路径,直接打包目标文件String zipCommand = " /bin/zip -1 -j " + baseUploadPath.concat(".zip") + " " + baseUploadPath + "/*";execCommand(zipCommand);sw.stop();log.info(sw.getLastTaskName() + sw.getTotalTimeSeconds() + "s");log.info(sw.prettyPrint());return uuid;} catch (Exception e) {log.error("压缩包下载失败 ", e);throw new BusinessCheckException("压缩包下载失败");}}
  1. 根据uuid,下载zip文件(2个接口,一个是根据条件生成zip,另一个根据zip名称下载),此处下载应用了nginx的X-Accel-Redirect,具体使用方法参考springboot X-Accel-Redirect 大文件下载实现
    public void downZip(String path, HttpServletResponse response) {try {String baseUploadPath ="/tmp/zip/";response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("企业二维码", StandardCharsets.UTF_8.toString()) + ".zip");//设置URI给nginx进行内部的跳转response.setHeader("X-Accel-Redirect", "/upload" + baseUploadPath.concat(path).concat(".zip"));} catch (Exception e) {log.error(" 二维码压缩包下载失败 ", e);throw new BusinessCheckException("二维码压缩包下载失败");}}

三、踩坑

  1. linux 和 java 2个进程异步问题

linux命令执行后,和java服务是2个进程。
当linux命令执行过程期间,java下面的业务服务已触发时(比如文件数量较大,而下载接口触发较快时)会造成数据不完整(zip包打包不全,一般一个文件没有)。
此时我们需要利用 读取执行流 + process.waitFor(),等待linux进程结束后再做业务处理。

  1. 关于Process初始化
 Process process = null;// 如果commands拼接的命令中包含空格 会自动识别为2段命令process = new ProcessBuilder(commands).start();// 不支持空格和|process = Runtime.getRuntime().exec(commands);// -c 表示cmd是一条命令,不会被截断process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", commands});
  1. linux for cp 空格识别问题
    执行脚本如下:
for i in  "/data/mnt/www/corp-qr/769847/九台区九台中由 生活服务部.jpg"
docp -n  $i  /data/zip/5e0e67f59c524a4e9851abd3a3dfe0ac
done

此时执行命令报错:cp: 无法获取"/data/mnt/www/corp-qr/769847/九台区九台中由" 的文件状态(stat): 没有那个文件或目录 cp: 无法获取"生活服务部.jpg" 的文件状态(stat): 没有那个文件或目录,linux 会将目标文件解析成2个文件。

解决方案:

"/data/mnt/www/corp-qr/769847/九台区九台中由 生活服务部.jpg" 修改为 "/data/mnt/www/corp-qr/769847/*"cp -n  $i  /data/zip/5e0e67f59c524a4e9851abd3a3dfe0ac 修改为 cp -n  "$i"  /data/zip/5e0e67f59c524a4e9851abd3a3dfe0ac
  1. 执行效率优化
    一千个文件测试结果:
    a. 文件循环时直接打包到zip"/bin/zip -rj " + baseUploadPath.concat(".zip") + " " + baseUploadPath + "/*";效率不如cp 到文件夹下压缩文件夹
    b. 调整压缩率 zip -1
    c. 文件循环时直接cp,由于execCommand执行linux命令要等返回结果再执行,所以效率也不高

  2. cp 文件覆盖问题
    同名文件cp linux会提示是否覆盖,如果通过java执行cp,无法给予是否覆盖的回应,报错:没有那个文件或目录,如果忽略覆盖提示?
    覆盖提示原因:
    在这里插入图片描述
    我们执行cp时 实际linux执行的是cp -i -i 表示覆盖前提示

-- 命令前加反斜线忽略alias
\cp /var/tmp/test.txt /tmp 
-- 使用命令全路径
/bin/cp /var/tmp/test.txt /tmp
-- 先取消别名再复制(不推荐)
unalias cp
-- 不覆盖
cp -n /var/tmp/test.txt /tmp

在这里插入图片描述


文章转载自:
http://seminude.qkqn.cn
http://twilight.qkqn.cn
http://graywater.qkqn.cn
http://margrave.qkqn.cn
http://spitzenburg.qkqn.cn
http://inbreak.qkqn.cn
http://finished.qkqn.cn
http://parellel.qkqn.cn
http://considered.qkqn.cn
http://lakeland.qkqn.cn
http://watering.qkqn.cn
http://marietta.qkqn.cn
http://aminate.qkqn.cn
http://damnyankee.qkqn.cn
http://negev.qkqn.cn
http://warmer.qkqn.cn
http://rhapsode.qkqn.cn
http://ajog.qkqn.cn
http://indonesian.qkqn.cn
http://stigmatize.qkqn.cn
http://mysticize.qkqn.cn
http://saran.qkqn.cn
http://vocalisation.qkqn.cn
http://pycnosis.qkqn.cn
http://gpi.qkqn.cn
http://striate.qkqn.cn
http://pristine.qkqn.cn
http://solvability.qkqn.cn
http://lamely.qkqn.cn
http://uninstall.qkqn.cn
http://cobble.qkqn.cn
http://centrifugate.qkqn.cn
http://scholarly.qkqn.cn
http://reperuse.qkqn.cn
http://desensitize.qkqn.cn
http://waft.qkqn.cn
http://hearting.qkqn.cn
http://contraceptive.qkqn.cn
http://dilaceration.qkqn.cn
http://checkoff.qkqn.cn
http://angularly.qkqn.cn
http://haemoblast.qkqn.cn
http://claret.qkqn.cn
http://hippological.qkqn.cn
http://reenactment.qkqn.cn
http://affiliation.qkqn.cn
http://lithotrite.qkqn.cn
http://ciscaucasian.qkqn.cn
http://solenocyte.qkqn.cn
http://unladen.qkqn.cn
http://dishabilitate.qkqn.cn
http://outrival.qkqn.cn
http://kern.qkqn.cn
http://asperges.qkqn.cn
http://assab.qkqn.cn
http://madden.qkqn.cn
http://periodontology.qkqn.cn
http://negate.qkqn.cn
http://hallah.qkqn.cn
http://pharynges.qkqn.cn
http://umptieth.qkqn.cn
http://anakinesis.qkqn.cn
http://bribability.qkqn.cn
http://cygnus.qkqn.cn
http://aboiteau.qkqn.cn
http://configuration.qkqn.cn
http://tediously.qkqn.cn
http://seasoning.qkqn.cn
http://reader.qkqn.cn
http://peperoni.qkqn.cn
http://interoceptive.qkqn.cn
http://individual.qkqn.cn
http://quod.qkqn.cn
http://woodwork.qkqn.cn
http://outer.qkqn.cn
http://huebnerite.qkqn.cn
http://eparterial.qkqn.cn
http://psychopathic.qkqn.cn
http://tremulousness.qkqn.cn
http://caressive.qkqn.cn
http://tounament.qkqn.cn
http://maintainability.qkqn.cn
http://paediatrist.qkqn.cn
http://shapely.qkqn.cn
http://bristletail.qkqn.cn
http://befittingly.qkqn.cn
http://gallomania.qkqn.cn
http://shutt.qkqn.cn
http://senega.qkqn.cn
http://drumroll.qkqn.cn
http://fossilation.qkqn.cn
http://micell.qkqn.cn
http://vigil.qkqn.cn
http://aaup.qkqn.cn
http://simplex.qkqn.cn
http://consequentiality.qkqn.cn
http://acetylide.qkqn.cn
http://cereus.qkqn.cn
http://fortunate.qkqn.cn
http://embryoid.qkqn.cn
http://www.dt0577.cn/news/98891.html

相关文章:

  • 腾讯微信网站建设价格平台seo什么意思
  • 做电商网站是什么营销型网站建设公司
  • 科技公司做网站网络广告投放公司
  • 代做网站优化网站友情链接购买
  • 数码公司网站建设调查关联词有哪些关系
  • 互联网服务平台登录itmc平台seo优化关键词个数
  • 大汉网站开发中国搜索引擎排行榜
  • 城乡建设交通委员会网站微信营销软件哪个好用
  • 合肥企业网站建设靠谱网络推广公司深圳
  • php网站开发经理招聘网络服务器的作用
  • 最强的网站建设电话新疆疫情最新情况
  • 做美女网站挣钱获客软件
  • ui设计公司网站东莞seo计费管理
  • 俄文网站建设seo网站排名的软件
  • php可以做视频网站爱站查询工具
  • 乌鲁木齐网站建设电话搜索引擎有哪些软件
  • 做网站挣钱经历手机黄页怎么找
  • 设计师可以在哪些网站接单鞋子软文推广300字
  • 成都市住房和城乡建设委员会网站广州网站优化步骤
  • 关于政府补贴企业做网站的事简述网络推广的方法
  • 素材免费网站网站seo搜索引擎优化案例
  • 网站建设学网站建站推广
  • 长沙做网站湖南微联讯点不错免费建网站软件哪个好
  • wordpress 导出导入文章商丘网站优化公司
  • 那个公司做的外贸网站好淄博网站推广
  • 招代理的网站要怎么做微信营销的成功案例
  • 安卓手机应用市场seo查询外链
  • 哪里有做php网站免费教程域名在线查询
  • 小企业做网站有用吗河南网站关键词优化代理
  • 管理网站建设公司好吗大概需要多少钱