package com.agilecontrol.nea.core.control.web.binhandler; import com.agilecontrol.nea.core.control.util.FileDownload; import com.agilecontrol.nea.core.control.web.WebUtils; import com.agilecontrol.nea.core.control.web.binhandler.BinaryHandler; import com.agilecontrol.nea.core.monitor.MonitorManager; import com.agilecontrol.nea.core.report.ReportUtils; import com.agilecontrol.nea.util.Configurations; import java.io.File; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MFile implements BinaryHandler { private static final Logger log = LoggerFactory.getLogger(MFile.class); private static final String CONTENT_TYPE = "text/html; charset=UTF-8"; private static final String DOWNLOAD_TYPE = "application/octetstream; charset=GBK"; private static final String[] TEXT_TYPE = new String[]{"html", "htm", "csv", "txt", "log"}; private static final String[] EXT = new String[]{"xls", "doc", "pdf", "zip"}; private static final String[] EXT_CONTENT_TYPE = new String[]{"application/vnd.ms-excel", "application/vnd.ms-word", "application/pdf", "application/zip"}; private boolean isChildren(File file, File parent) { boolean b; for(b = file.getParentFile().equals(parent); !b; b = file.getParentFile().equals(parent)) { file = file.getParentFile(); if(file == null) { break; } } return b; } public void process(HttpServletRequest request, HttpServletResponse response) throws Exception { String filePath = request.getParameter("f"); if(filePath == null) { String out = request.getPathInfo(); int name = out.indexOf(47, 1); if(name > 0) { filePath = out.substring(name + 1); } } if(filePath != null && !filePath.trim().equals("")) { filePath = filePath.trim(); ReportUtils out1 = new ReportUtils(request); String name1 = out1.getUserName(); Configurations conf = (Configurations)WebUtils.getServletContextManager().getActor("nea.configs"); String destFolder = MonitorManager.getInstance().getMonitorRootFolder(out1.getUser().getClientDomain()); File parentFolder = new File(destFolder); File file = new File(destFolder + File.separator + filePath); if(file.exists() && file.isFile() && this.isChildren(file, parentFolder)) { FileDownload fileDownload = new FileDownload(); fileDownload.processRequest(request, response, true, file, new String[0]); return; } log.warn("Could not load file:" + file.getAbsolutePath()); } response.setContentType("text/html; charset=UTF-8"); PrintWriter out2 = response.getWriter(); out2.println(""); out2.println("
文件不存在,或者文件不可读,或者没有指定文件名
"); out2.println(""); } public void init(ServletContext context) { } }