現在很多系統中,爲了維護便捷,都會有日誌管理功能,像一些完備的系統框架都會有完備的日誌功能,今天我給大家分享一下我在做系統時併入系統的一個日誌導出功能,作爲一個公共方法來使用,不多說了,代碼展示:
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獲取日誌文件的相關方法了,希望能夠幫助到大家,謝謝!