现在很多系统中,为了维护便捷,都会有日志管理功能,像一些完备的系统框架都会有完备的日志功能,今天我给大家分享一下我在做系统时并入系统的一个日志导出功能,作为一个公共方法来使用,不多说了,代码展示:
1、jsp页面:
——a标签:
<a id="journal" class='btn btn btn-warning btn-small' href="${pageContext.request.contextPath}/admin/sys/browse_journal">获取日志文件</a>
——js:
//防止多次点击
$("#journal").one("click",function(){
$(this).click(function (){return false;});
});
2、controller业务层:
// 获取日志文件
@RequestMapping(value = "/sys/browse_journal", method = RequestMethod.GET)
public String journalData(HttpServletRequest request,HttpServletResponse response,Model model) {
Map<String,Object> param = new HashMap<>();
try {
String results = Client.send(Values.URL_APPPARAM_JOURNAL, param);
Map<String, Object> resultMap = JacksonUtils.json2map(results);
Map<String, Object> dataMap = ActionUtils.castMap(
resultMap.get("data"), Object.class);
String fileName = dataMap.get("data").toString();
ZipDownLoadFactory.zipDownload(request,response,fileName);
} catch (BusinessException e) {
model.addAttribute("map",param);
model.addAttribute("msgText", e.getErrorCode()+":"+e.getMessage());
e.printStackTrace();
return "sys/appParam/updateAppParam";
} catch (Exception e) {
model.addAttribute("msgText", "下载文件失败");
e.printStackTrace();
return "sys/appParam/updateAppParam";
}
return null;
}
/**
* Zip下载工厂类
* @author Administrator
*
*/
public class ZipDownLoadFactory {
private static final Logger logger = LoggerFactory.getLogger(ZipDownLoadFactory.class);
public static void zipDownload(HttpServletRequest request,HttpServletResponse response,String fileName) throws BusinessException {
System.out.println("fileName="+fileName);
String zipName = fileName.substring(fileName.lastIndexOf("/",fileName.lastIndexOf("/")-1)+1);
FileInputStream fis = null;
byte[] bytes = null;
ServletOutputStream ouputStream = null;
ByteArrayOutputStream baos = null;
try {
ServletContext servletContext = request.getSession().getServletContext();
// String filePath = servletContext.getRealPath(fileName);
// System.out.println("fileName="+fileName);
File file = new File(fileName);
fis = new FileInputStream(file);
baos = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
bytes = baos.toByteArray();
response.setContentType("application/zip");
response.setContentLength(bytes.length);
response.setHeader("Content-Disposition", "attachment;fileName=\"" + zipName.substring(zipName.indexOf("/")+1) + "\"");
ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
} catch (Exception e) {
logger.error("", e);
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (baos != null) {
try {
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (ouputStream != null) {
try {
ouputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
由于我们获取的日志是一个完整的文件,我们不可能把这个完整的文件就原封不动的下载下来,这样太浪费时间和效率了,因此我们需要压缩工具来进行压缩下载,代码展示:
// 获取日志文件
@Override
public String queryJournal(Map<String, Object> param)throws BusinessException {
String filePathName = queryKeyValue("fund_home");
// 被压缩文件
String filePath = queryKeyValue("nohup_home")+"nohup.out";
// String filePath = "F:/基金/日志文件/20190529/nohup.out";
if(!new File(filePath).exists()){
throw new BusinessException("99100041",ExceptionHandler.getMessage("99100041"));
}
// 压缩文件
System.out.println("filePath="+filePath);
String fileName = filePathName+"nohup/nohup_"+DateUtils.currentDatetimeMo()+".zip";
// String fileName = "F:/TOMCAT/FUND/webapps/fund-web"+"/nohup/nohup_"+DateUtils.currentDatetimeMo()+".zip";
try {
File file = new File(fileName);
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();//创建父级文件路径
file.createNewFile();//创建文件
}
FileOutputStream fos1 = new FileOutputStream(file);
ZipUtils.toZip(filePath, fos1,true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
logger.error("", e);
throw new BusinessException("10000001", ExceptionHandler.getMessage("10000001"));
}
return fileName;
}
这样,我们就能获取一个日志文件,当然也可以按照同样的方法去获取其他的文件了。
以上就是spring mvc获取日志文件的相关方法了,希望能够帮助到大家,谢谢!