package com.agilecontrol.nea.core.control.web.binhandler; import com.agilecontrol.nea.core.control.util.FileDownload; import com.agilecontrol.nea.core.control.web.UserWebImpl; import com.agilecontrol.nea.core.control.web.WebUtils; import com.agilecontrol.nea.core.control.web.binhandler.BinaryHandler; import com.agilecontrol.nea.core.report.ReportUtils; import com.agilecontrol.nea.core.util.ConfigValues; import com.agilecontrol.nea.util.Configurations; import com.agilecontrol.nea.util.Tools; 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 DownFile implements BinaryHandler { private static final Logger log = LoggerFactory.getLogger(DownFile.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 isTextType(String fileName) { String ext = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); for(int i = 0; i < TEXT_TYPE.length; ++i) { if(ext.equalsIgnoreCase(TEXT_TYPE[i])) { return true; } } return false; } public void process(HttpServletRequest request, HttpServletResponse response) throws Exception { String filePath = request.getParameter("filename"); boolean deleteFile = false; if(filePath == null) { String isShow = request.getPathInfo(); int out = isShow.indexOf(47, 1); if(out > 0) { filePath = isShow.substring(out + 1); } } boolean isShow1 = Tools.getYesNo(request.getParameter("show"), true); if(filePath != null && !filePath.trim().equals("")) { filePath = filePath.trim(); ReportUtils out1 = new ReportUtils(request); String name = out1.getUserName(); UserWebImpl userWeb = out1.getUser(); if((filePath.contains("..") || filePath.startsWith("/") || filePath.startsWith("\\")) && (!ConfigValues.get("portal.skip_root_download", false) || !userWeb.isAdmin())) { throw new IllegalArgumentException("Illegal file path "); } Configurations conf = (Configurations)WebUtils.getServletContextManager().getActor("nea.configs"); String webRoot = ConfigValues.WEB_ROOT; File file = new File(webRoot + File.separator + filePath); if(file.exists() && file.isFile()) { FileDownload fileDownload = new FileDownload(); fileDownload.processRequest(request, response, true, file, new String[0]); if(deleteFile) { try { if(!file.delete()) { log.debug("Fail to delete file:" + file.getAbsolutePath()); } } catch (Throwable var14) { log.error("Fail to delete file " + file.getAbsolutePath() + ":" + var14); } } return; } log.warn("Could not load file:" + file.getAbsolutePath()); } response.setContentType("text/html; charset=UTF-8"); PrintWriter out2 = response.getWriter(); out2.println(""); out2.println("GetFile"); out2.println(""); out2.println("

文件不存在,或者文件不可读,或者没有指定文件名

"); out2.println(""); } public void init(ServletContext context) { } }