14.将对象的字符串写入文件并保存到盘符
public JsonVo savaCloudScript(ScriptModelVO scriptModelVo) throws RollbackableBizException {
JsonVo jsonVo=new JsonVo();
try {
String savePath = getCloudFileUploadPath(scriptModelVo);
//测试保存到Windows的D:/upload
//savePath = "D:" + File.separator + "upload";
File dir = new File(savePath);
dir.setWritable(true, false);
if (!dir.exists()) {
dir.mkdirs();
}
FileWriter writer;
try {
writer = new FileWriter(savePath + File.separator + scriptModelVo.getUploadFileName());
writer.write(scriptModelVo.getScriptText());
writer.flush();
writer.close();
// 修改权限
String osType = System.getProperty("os.name");
if (osType != null && osType.toLowerCase().indexOf("window") == -1) {
try {
JavaUtilShell.processShellWithException(
"chmod 755 -R " + savePath + File.separator +scriptModelVo.getUploadFileName());
} catch (Exception e) {
jsonVo.setSuccess(false);
jsonVo.setMsg("修改权限失败");
return jsonVo;
}
}
} catch (IOException e) {
jsonVo.setSuccess(false);
jsonVo.setMsg("写入文件失败");
jsonVo.setObj(e);
return jsonVo;
}
jsonVo.setSuccess(true);
jsonVo.setMsg("保存脚本成功");
return jsonVo;
} catch (Exception e) {
jsonVo.setSuccess(false);
jsonVo.setMsg("获取脚本保存路径失败");
jsonVo.setObj(e);
return jsonVo;
}
}