%@ page contentType="text/html; charset=UTF-8" %> <%@ page import="java.util.ArrayList, java.util.regex.*, java.io.*"%> <%! class TmpFileManager implements HttpSessionBindingListener { private File tempFile; public void setTempFile(File tmpfile) { tempFile = tmpfile; } public void valueBound(HttpSessionBindingEvent event) { } public void valueUnbound(HttpSessionBindingEvent event) { // remove the temporary file when the session expires tempFile.delete(); } } Pattern amp = Pattern.compile("&"); Pattern lt = Pattern.compile("<"); public String quote(String source) { // Escape HTML return lt.matcher(amp.matcher(source).replaceAll("&")).replaceAll("<"); } %> <% request.setCharacterEncoding("UTF-8"); String src = request.getParameter("src"); String title = request.getParameter("title"); String fname = src; String sTmpFile = "TempFilePath"; try { String formAction=request.getParameter("formAction"); String sourceCode=request.getParameter("sourceCode"); int lastSeparator = Math.max(src.lastIndexOf("/"), 0); String sessionId = session.getId(); String saveAsLabel = "Save as"; // Temp files need to go in the same directory as sources so that // includes come out right. String tempUrl = src.substring(0, lastSeparator)+"/.tmp_"+sessionId+"_"+src.substring(lastSeparator+1); String tempFilePath=application.getRealPath(tempUrl); //new File(tempFilePath).getParentFile().mkdirs(); boolean temp=false; if ("Update".equals(formAction)) { File tempFile = new File(tempFilePath); fname = tempUrl; // BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); FileOutputStream fos = new FileOutputStream(tempFilePath); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); BufferedWriter writer = new BufferedWriter(osw); writer.write(sourceCode); writer.flush(); writer.close(); temp=true; // save the file name with the session object // the file will be deleted when the session expires TmpFileManager tmpFileMgr = (TmpFileManager) session.getAttribute(sTmpFile); if (tmpFileMgr == null) { tmpFileMgr = new TmpFileManager(); tmpFileMgr.setTempFile(tempFile); session.setAttribute(sTmpFile, tmpFileMgr); } } else if (formAction != null && formAction.startsWith(saveAsLabel)) { // response.addHeader("Content-Type","application/octet-strem"); response.setContentType("application/octet-stream; charset=UTF-8"); response.addHeader("Content-Disposition","attachment; filename=\"" + request.getParameter("saveasfile") + "\""); out.write(sourceCode); return; } else { if ("Reset".equals(formAction)) { File file = new File(tempFilePath); if (file.exists()) file.delete(); } else { if (new File(tempFilePath).exists()) { temp = true; fname = tempUrl; } } // File file = new File(temp?tempFilePath:application.getRealPath(src)); // BufferedReader reader = new BufferedReader(new FileReader(file)); FileInputStream fis = new FileInputStream(temp?tempFilePath:application.getRealPath(src)); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); BufferedReader reader = new BufferedReader(isr); String line; StringBuffer sb = new StringBuffer(); while ((line = reader.readLine()) != null) { sb.append(line); sb.append("\n"); } reader.close(); sourceCode = sb.toString(); } String basefile = src.substring(src.lastIndexOf("/")+1); %>
?showTaskBar=false';">