package werti.server; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.io.OutputStreamWriter; import java.io.FileOutputStream; import java.io.FileWriter; import java.net.URL; import java.util.Arrays; import java.util.Enumeration; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.Locale; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.commons.lang.StringEscapeUtils; import org.apache.logging.log4j.Logger; import org.apache.uima.jcas.JCas; import org.jsoup.Jsoup; import org.jsoup.nodes.Node; import org.jsoup.nodes.Document; import org.openid4java.OpenIDException; import org.openid4java.consumer.ConsumerException; import org.openid4java.consumer.ConsumerManager; import org.openid4java.discovery.DiscoveryException; import org.openid4java.discovery.DiscoveryInformation; import org.openid4java.discovery.Identifier; import org.openid4java.message.AuthRequest; import org.openid4java.message.MessageException; import werti.WERTiContext; import werti.WERTiContext.WERTiContextException; import werti.util.ActivitiesSessionLoader; import werti.util.HTMLEnhancer; import werti.util.HTMLUtils; import werti.util.JSONEnhancer; import werti.util.PageHandler; import werti.util.PostRequest; import werti.util.PracticeHandler; import com.google.gson.Gson; import weka.core.Instances; import weka.filters.Filter; import werti.util.Constants; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Iterator; import javax.servlet.ServletContext; import javax.servlet.ServletOutputStream; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.FileUploadBase; import org.apache.commons.lang3.RandomStringUtils; import org.apache.tika.Tika; import org.xml.sax.ContentHandler; import org.apache.tika.sax.BodyContentHandler; import org.apache.tika.metadata.Metadata; import org.apache.tika.parser.Parser; import org.apache.tika.parser.AutoDetectParser; import org.apache.tika.parser.ParseContext; import org.xml.sax.SAXException; import org.apache.tika.exception.TikaException; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Node; import org.jsoup.nodes.Document; import java.io.InputStreamReader; import java.util.Scanner; //import java.nio.file.*; import javax.servlet.http.HttpSession; import java.io.Writer; import java.io.BufferedWriter; import java.io.OutputStreamWriter; import java.io.FileWriter; import java.io.BufferedReader; import java.io.FileReader; import werti.util.VerifyRecaptcha; public class UploadDownloadFileServlet extends HttpServlet { private static final long serialVersionUID = 15; private ServletFileUpload uploader = null; @Override public void init() throws ServletException{ DiskFileItemFactory fileFactory = new DiskFileItemFactory(); File filesDir = (File) getServletContext().getAttribute("FILES_DIR_FILE"); fileFactory.setRepository(filesDir); this.uploader = new ServletFileUpload(fileFactory); } public static boolean checkMetaData(File f, String getContentType) { try { InputStream is = new FileInputStream(f); ContentHandler contenthandler = new BodyContentHandler(); Metadata metadata = new Metadata(); metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName()); Parser parser = new AutoDetectParser(); try { parser.parse(is, contenthandler, metadata, new ParseContext()); } catch (SAXException e) { System.out.println("SAXException"); // Handle error return false; } catch (TikaException e) { System.out.println("TikaException"); return false; } System.out.println("content_type="+metadata.get(Metadata.CONTENT_TYPE)); if (metadata.get(Metadata.CONTENT_TYPE).contains(getContentType)) { return true; } else { return false; } } catch (IOException e) { // Handle error return false; } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if(!ServletFileUpload.isMultipartContent(request)){ throw new ServletException("Content type is not multipart/form-data"); } response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); out.write("
"); try { String act = ""; String en = ""; String path = ""; String captcha = ""; boolean file_size_err = false; boolean file_type_err = false; boolean captcha_err = false; boolean verify = false; boolean file_uploaded = false; boolean save_file = false; boolean file_lang_err = false; int i = 0; int file_index = 0; List