List of usage examples for org.apache.commons.fileupload FileUploadException printStackTrace
public void printStackTrace()
From source file:org.wso2.carbon.ui.transports.fileupload.AnyFileUploadExecutor.java
public boolean execute(HttpServletRequest request, HttpServletResponse response) throws CarbonException, IOException { try {/*from w w w . j ava2 s.c om*/ return super.executeCommon(request, response); } catch (FileUploadException e) { e.printStackTrace(); //Todo: change body of catch statement use File | Settings | File Templates. } return false; }
From source file:Project.FileUploadServlet.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request/*from ww w . j av a 2 s . co m*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //processRequest(request, response); response.setContentType("text/html"); String path = ""; boolean isMultiPart = ServletFileUpload.isMultipartContent(request); if (isMultiPart) { ServletFileUpload upload = new ServletFileUpload(); try { String category = ""; String keywords = ""; double cost = 0.0; String imagess = ""; String user = ""; FileItemIterator itr = upload.getItemIterator(request); Map<String, String> map = new HashMap<String, String>(); while (itr.hasNext()) { FileItemStream item = itr.next(); if (item.isFormField()) { //do variable declaration of the specific field String fieldName = item.getFieldName(); InputStream is = item.openStream(); byte[] b = new byte[is.available()]; is.read(b); String value = new String(b); response.getWriter().println(fieldName + ":" + value + "<br/>"); map.put(fieldName, value); } else { //do file upload and store the path as variable path = getServletContext().getRealPath("/"); //will write a method and we will call here if (processFile(path, item)) { response.getWriter().println("File uploaded successfully"); response.getWriter().println(path); } else { response.getWriter().println("File uploading failed"); } } for (Map.Entry<String, String> entry : map.entrySet()) { if (entry.getKey().equals("category")) { category = entry.getValue(); } else if (entry.getKey().equals("keywords")) { keywords = entry.getValue(); } else if (entry.getKey().equals("cost")) { cost = Double.parseDouble(entry.getValue()); } else if (entry.getKey().equals("fileName")) { imagess = entry.getValue(); } else if (entry.getKey().equals("user")) { user = entry.getValue(); } } } response.getWriter().println("images\\" + imagess); imagess = "images\\" + imagess; response.getWriter().println(category + "---" + keywords + "----" + cost + "---" + imagess); DB_Users d = new DB_Users(); d.insertProduct(category, keywords, imagess, cost, user); } catch (FileUploadException e) { e.printStackTrace(); } } else { //do nothing } }
From source file:recite18th.controller.Controller.java
public void processFormData() { //==== START OF penanganan multi-part data isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = null;/*from w w w. java 2s .c o m*/ try { items = upload.parseRequest(request); } catch (FileUploadException e) { e.printStackTrace(); } Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString(); formParams.put(name, value); } else { //upload file here try { String itemName = item.getName(); //TOFIX : kalau FFox, itemName hanya nama file saja. Kalau IE, lengkap dengan nama folder. //make sure upload folder exist File fDir = new File(Config.base_path + "upload"); if (!fDir.exists()) { fDir.mkdir(); } String path = Config.base_path + "upload" + Config.path_delimiter + itemName;//TODO : save all to this folder || allow customization Logger.getLogger(Controller.class.getName()).log(Level.INFO, "Try to write file " + path); File savedFile = new File(path); item.write(savedFile); formParams.put(item.getFieldName(), savedFile.getName()); } catch (Exception e) { Logger.getLogger(Controller.class.getName()).log(Level.INFO, "Possibly, that file doesn't exist");//TODO: better information to user } } } } //==== ENDOF penanganan multi-part data }
From source file:rurales.FileUploadRural.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request//from www .j a v a 2 s . c om * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConectionDB con = new ConectionDB(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String Unidad = ""; boolean isMultiPart = ServletFileUpload.isMultipartContent(request); if (isMultiPart) { ServletFileUpload upload = new ServletFileUpload(); try { HttpSession sesion = request.getSession(true); FileItemIterator itr = upload.getItemIterator(request); while (itr.hasNext()) { FileItemStream item = itr.next(); if (item.isFormField()) { String fielName = item.getFieldName(); InputStream is = item.openStream(); byte[] b = new byte[is.available()]; is.read(b); String value = new String(b); response.getWriter().println(fielName + ":" + value + "<br/>"); } else { String path = getServletContext().getRealPath("/"); if (FileUpload.processFile(path, item)) { //response.getWriter().println("file uploaded successfully"); if (lee.obtieneArchivo(path, item.getName())) { out.println("<script>alert('Se carg el Folio Correctamente')</script>"); out.println("<script>window.location='requerimiento.jsp'</script>"); } //response.sendRedirect("cargaFotosCensos.jsp"); } else { //response.getWriter().println("file uploading falied"); //response.sendRedirect("cargaFotosCensos.jsp"); } } } } catch (FileUploadException fue) { fue.printStackTrace(); } out.println("<script>alert('No se pudo cargar el Folio, verifique las celdas')</script>"); out.println("<script>window.location='requerimiento.jsp'</script>"); //response.sendRedirect("carga.jsp"); } /* * Para insertar el excel en tablas */ }
From source file:Servlet.BAC_UploadInvitationToBid.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from ww w .jav a 2 s . c o m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException { response.setContentType("text/html;charset=UTF-8"); try { InputStream inputStream = null; String category = null; ArrayList<String> files = new ArrayList<String>(); String id2 = ""; boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { //ArrayList<String> List items = upload.parseRequest(request); Iterator iterator = items.iterator(); String id = null; String title = null; File path = null; File uploadedFiles = null; String fileName = null; while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); if (item.isFormField()) { //Returns the string inside the field String value = item.getString(); //returns the name of the field String value2 = item.getFieldName(); if (value2.equals("projectId")) { id = value; id2 = value; } if (value2.equals("projectName")) { title = value; } } if (!item.isFormField()) { fileName = item.getName(); String root = getServletContext().getRealPath("/"); //path where the file will be stored path = new File("D:\\Development\\NetBeans\\Projects\\Cogito\\Upload" + "/Bids and Awards Department" + "/Invitations to bid" + "/" + title + " invitation"); if (!path.exists()) { boolean status = path.mkdirs(); } uploadedFiles = new File(path + "/" + fileName); item.write(uploadedFiles); files.add(fileName); } } BACDAO bacdao = new BACDAO(); OCPDDAO oc = new OCPDDAO(); Project project = oc.getAllProjectDetails(id); bacdao.changeBACStatus1(id); InvitationToBid invite = new InvitationToBid(0, fileName, null, project, "chrome-extension://nhcgkapmceenhknicldaiaplpkpmicmc/" + "Bids and Awards Department" + "/Invitations to bid" + "/" + title + " invitation"); bacdao.uploadInvitation(invite); } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception ex) { Logger.getLogger(BAC_UploadInvitationToBid.class.getName()).log(Level.SEVERE, null, ex); } } request.setAttribute("success", "Success"); ServletContext context = getServletContext(); RequestDispatcher dispatch = context.getRequestDispatcher("/BAC_Home"); dispatch.forward(request, response); } finally { out.close(); } }
From source file:Servlet.BAC_UploadNotices.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w w w . j a v a2 s. co m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try { InputStream inputStream = null; String category = null; String idd = ""; ArrayList<String> files = new ArrayList<String>(); ArrayList<String> fileType = new ArrayList<String>(); boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { //ArrayList<String> List items = upload.parseRequest(request); Iterator iterator = items.iterator(); String id = null; String contractorName = null; int cont_has_proj_ID = 0; String title = null; File path = null; File uploadedFiles = null; String fileName = null; String documentType = null; while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); if (item.isFormField()) { //Returns the string inside the field String value = item.getString(); //returns the name of the field String value2 = item.getFieldName(); if (value2.equals("projectId")) { id = value; idd = value; } if (value2.equals("projectName")) { title = value; } if (value2.equals("contProject")) { cont_has_proj_ID = Integer.parseInt(value); } if (value2.equals("contractor")) { contractorName = value; } } if (!item.isFormField()) { fileName = item.getName(); String root = getServletContext().getRealPath("/"); String fieldname = item.getFieldName(); //Add the conditions here if (fieldname.equalsIgnoreCase("Notice of award")) { documentType = "Notice of award"; } if (fieldname.equalsIgnoreCase("Notice to proceed")) { documentType = "Notice to proceed"; } fileType.add(documentType); //path where the file will be stored path = new File("D:\\Development\\NetBeans\\Projects\\Cogito\\Upload" + "/Bids and Awards Department" + "/Bid Notices/" + title + "/" + contractorName); if (!path.exists()) { boolean status = path.mkdirs(); } uploadedFiles = new File(path + "/" + fileName); item.write(uploadedFiles); files.add(fileName); } } BACDAO bacdao = new BACDAO(); OCPDDAO oc = new OCPDDAO(); //CHANGE Project project = oc.getBasicProjectDetails(id); int contractorIdd = bacdao.getContractorID(cont_has_proj_ID); Contractor contractor = bacdao.getContractorInfo(contractorIdd); bacdao.changeBACStatus4(bacdao.getContUser(contractorIdd), id); bacdao.changeBACStatus5(cont_has_proj_ID); ArrayList<Contractor_Has_Project> respondents = bacdao.getViewRespondents(id); for (int x = 0; x < respondents.size(); x++) { if (respondents.get(x).getID() == contractorIdd) { respondents.remove(x); } } for (int x = 0; x < respondents.size(); x++) { bacdao.deleteContractorProj(respondents.get(x).getID()); } //CHANGE for (int x = 0; x < files.size(); x++) { Bid_Notices bidnotice = new Bid_Notices(0, files.get(x), null, project, contractor, "chrome-extension://nhcgkapmceenhknicldaiaplpkpmicmc/" + "Bids and Awards Department" + "/Bid Notices/" + title + "/" + contractorName, fileType.get(x)); bacdao.uploadBidNotices(bidnotice); } } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception ex) { Logger.getLogger(BAC_UploadInvitationToBid.class.getName()).log(Level.SEVERE, null, ex); } } request.setAttribute("success", "Success"); ServletContext context = getServletContext(); RequestDispatcher dispatch = context.getRequestDispatcher("/BAC_Home"); dispatch.forward(request, response); } finally { out.close(); } }
From source file:Servlet.Citizen_AddInfo.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from ww w.j av a 2s. c om * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); ActivityDAO actdao = new ActivityDAO(); try { CitizenDAO citizenDAO = new CitizenDAO(); NotificationDAO ntDAO = new NotificationDAO(); Citizen c = (Citizen) session.getAttribute("user"); InputStream inputStream = null; ArrayList<String> files = new ArrayList<String>(); boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { //ArrayList<String> List items = upload.parseRequest(request); Iterator iterator = items.iterator(); String title = null; String description = null; String location = null; String locationdetails = null; String videoD = null; String imageD = null; String documentD = null; File path = null; File uploadedFiles = null; String fileName = null; Citizen user = (Citizen) session.getAttribute("user"); while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); if (item.isFormField()) { //Returns the string inside the field String value = item.getString(); //returns the name of the field String value2 = item.getFieldName(); if (value2.equals("testimonialtitle")) { title = value; } if (value2.equals("testimonialdescription")) { description = value; } if (value2.equals("videodescription")) { videoD = value; } if (value2.equals("imagedescription")) { imageD = value; } if (value2.equals("documentdescription")) { documentD = value; } } if (!item.isFormField()) { fileName = item.getName(); String root = getServletContext().getRealPath("/"); //path where the file will be stored path = new File("C:\\Users\\AdrianKyle\\Documents\\NetBeansProjects\\Cogito\\Upload" + "/Citizen/" + title); if (!path.exists()) { boolean status = path.mkdirs(); } uploadedFiles = new File(path + "/" + fileName); item.write(uploadedFiles); files.add(fileName); } } Testimonial t = (Testimonial) session.getAttribute("openTestimonial"); //Add activity actdao.addActivity(new Activity(0, "you have added additional information in a testimonial entitled " + title, null, user.getUser())); //Add Notification ntDAO.addNotification(new Notification(0, c.getFirstName() + " " + c.getLastName() + " has requested to add additional files to testimonial entitled " + t.getTitle(), null, t.getCitizen().getUser())); //Place in appropriate tables for (int x = 0; x < files.size(); x++) { String filename = files.get(x); String[] parts = filename.split(Pattern.quote(".")); String extension = parts[1]; //Videos if (extension.equalsIgnoreCase("mp4") || extension.equalsIgnoreCase("avi") || extension.equalsIgnoreCase("3gp") || extension.equalsIgnoreCase("flv") || extension.equalsIgnoreCase("wmv") || extension.equalsIgnoreCase("mkv")) { Files f = new Files(); f.setFileName(files.get(x)); f.setType("Video"); f.setTestimonial(t); f.setStatus("Pending"); f.setDescription(videoD); f.setUploader(c.getUser().getUsername()); f.setTestimonial(t); citizenDAO.uploadFiles(f, c.getUser().getUsername()); } //Images else if (extension.equalsIgnoreCase("png") || extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("bmp")) { Files f = new Files(); f.setFileName(files.get(x)); f.setType("Image"); f.setTestimonial(t); f.setStatus("Pending"); f.setDescription(imageD); f.setUploader(c.getUser().getUsername()); f.setTestimonial(t); citizenDAO.uploadFiles(f, c.getUser().getUsername()); } //Documents else if (extension.equalsIgnoreCase("pdf") || extension.equalsIgnoreCase("docx") || extension.equalsIgnoreCase("doc") || extension.equalsIgnoreCase("pptx") || extension.equalsIgnoreCase("txt") || extension.equalsIgnoreCase("xlsx")) { Files f = new Files(); f.setFileName(files.get(x)); f.setType("Document"); f.setTestimonial(t); f.setStatus("Pending"); f.setDescription(documentD); f.setUploader(c.getUser().getUsername()); f.setTestimonial(t); citizenDAO.uploadFiles(f, c.getUser().getUsername()); } } } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception ex) { Logger.getLogger(Citizen_AddInfo.class.getName()).log(Level.SEVERE, null, ex); } } request.setAttribute("success", "addSuccess"); ServletContext context = getServletContext(); RequestDispatcher dispatch = context.getRequestDispatcher("/Citizen_SearchTestimonial"); dispatch.forward(request, response); } finally { out.close(); } }
From source file:Servlet.Citizen_SendTestimonial.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./* w w w . j a va2 s . co m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); try { ActivityDAO actdao = new ActivityDAO(); CitizenDAO citizenDAO = new CitizenDAO(); InputStream inputStream = null; Citizen c = (Citizen) session.getAttribute("user"); ArrayList<String> files = new ArrayList<String>(); String category = null; boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { //ArrayList<String> List items = upload.parseRequest(request); Iterator iterator = items.iterator(); String title = null; String message = null; String location = null; String videoD = null; String imageD = null; String documentD = null; File path = null; File uploadedFiles = null; String fileName = null; while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); if (item.isFormField()) { //Returns the string inside the field String value = item.getString(); //returns the name of the field String value2 = item.getFieldName(); if (value2.equals("testimonialtitle")) { title = value; } if (value2.equals("testimonialtype")) { category = value; } if (value2.equals("testimonialdescription")) { message = value; } if (value2.equals("hiddenlocation")) { location = value; } if (value2.equals("videodescription")) { videoD = value; } if (value2.equals("imagedescription")) { imageD = value; } if (value2.equals("documentdescription")) { documentD = value; } } if (!item.isFormField()) { fileName = item.getName(); if (!fileName.isEmpty()) { String root = getServletContext().getRealPath("/"); //path where the file will be stored path = new File("C:\\Users\\AdrianKyle\\Documents\\NetBeansProjects\\Cogito\\Upload" + "/Citizen/" + title); if (!path.exists()) { boolean status = path.mkdirs(); } uploadedFiles = new File(path + "/" + fileName); item.write(uploadedFiles); files.add(fileName); } else { } } } //For testimonial category // for (int x = 0; x < files.size(); x++) { // // String filename = files.get(x); // if (!filename.isEmpty()) { // String[] parts = filename.split(Pattern.quote(".")); // String extension = parts[1]; // //Videos // if (extension.equalsIgnoreCase("mp4") || extension.equalsIgnoreCase("avi") || extension.equalsIgnoreCase("3gp") || extension.equalsIgnoreCase("flv") || extension.equalsIgnoreCase("wmv")) { // category = "video"; // break; // } //Images // else if (extension.equalsIgnoreCase("png") || extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("bmp")) { // category = "image"; // break; // } // } // } Testimonial t = new Testimonial(title, null, message, "chrome-extension://affgigbpilagplecbaihihkcaddmlkji/Citizen/", category, "Pending", c); //Upload testimonial citizenDAO.submitTestimonial(t); t.setId(citizenDAO.lastID()); //Add activity actdao.addActivity(new Activity(0, "you have submitted a testimonial entitled " + title, null, c.getUser())); //set location String[] loc = location.split(","); ArrayList<TLocation> tLoc = new ArrayList<>(); ArrayList<String> longitude = new ArrayList<>(); ArrayList<String> latitude = new ArrayList<>(); for (int x = 0; x < loc.length; x++) { String[] latlong = loc[x].split("&"); String thislat = latlong[0]; String thislong = latlong[1]; longitude.add(thislong); latitude.add(thislat); } for (int x = 0; x < loc.length; x++) { TLocation l = new TLocation(); l.setLongitude(longitude.get(x)); l.setLatitude(latitude.get(x)); l.setTestimonial(t); tLoc.add(l); } for (TLocation tl : tLoc) { citizenDAO.setLocation(tl); } //Place in appropriate tables for (int x = 0; x < files.size(); x++) { String filename = files.get(x); if (!filename.isEmpty()) { String[] parts = filename.split(Pattern.quote(".")); String extension = parts[1]; //Videos if (extension.equalsIgnoreCase("mp4") || extension.equalsIgnoreCase("avi") || extension.equalsIgnoreCase("3gp") || extension.equalsIgnoreCase("flv") || extension.equalsIgnoreCase("wmv") || extension.equalsIgnoreCase("mkv")) { Files f = new Files(); f.setFileName(files.get(x)); f.setType("Video"); f.setTestimonial(t); f.setStatus("Approved"); f.setDescription(videoD); f.setUploader(c.getUser().getUsername()); citizenDAO.submitFiles(f); } //Images else if (extension.equalsIgnoreCase("png") || extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("bmp")) { Files f = new Files(); f.setFileName(files.get(x)); f.setType("Image"); f.setTestimonial(t); f.setStatus("Approved"); f.setDescription(imageD); f.setUploader(c.getUser().getUsername()); citizenDAO.submitFiles(f); } //Documents else if (extension.equalsIgnoreCase("pdf") || extension.equalsIgnoreCase("docx") || extension.equalsIgnoreCase("doc") || extension.equalsIgnoreCase("pptx") || extension.equalsIgnoreCase("txt") || extension.equalsIgnoreCase("xlsx")) { Files f = new Files(); f.setFileName(files.get(x)); f.setType("Document"); f.setTestimonial(t); f.setStatus("Approved"); f.setDescription(documentD); f.setUploader(c.getUser().getUsername()); citizenDAO.submitFiles(f); } } } } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception ex) { Logger.getLogger(Citizen_SendTestimonial.class.getName()).log(Level.SEVERE, null, ex); } } request.setAttribute("success", category + "Success"); ServletContext context = getServletContext(); RequestDispatcher dispatch = context.getRequestDispatcher("/Citizen_SearchTestimonial"); dispatch.forward(request, response); } finally { out.close(); } }
From source file:Servlet.Citizen_SubmitReport.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w w w. j ava2s. c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); HttpSession session = request.getSession(); ActivityDAO actdao = new ActivityDAO(); try { CitizenDAO citizenDAO = new CitizenDAO(); OCPDDAO oc = new OCPDDAO(); InputStream inputStream = null; Citizen c = (Citizen) session.getAttribute("user"); String category = null; ArrayList<String> files = new ArrayList<String>(); boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { //ArrayList<String> List items = upload.parseRequest(request); Iterator iterator = items.iterator(); String message = null; File path = null; File uploadedFiles = null; String fileName = null; Citizen user = (Citizen) session.getAttribute("user"); String projectId = null; String projectName = null; Project proj = null; while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); if (item.isFormField()) { //Returns the string inside the field String value = item.getString(); //returns the name of the field String value2 = item.getFieldName(); if (value2.equals("reportdescription")) { message = value; } if (value2.equals("projectID")) { projectId = value; proj = oc.getBasicProjectDetails(projectId); } if (value2.equals("projectName")) { projectName = value; } } if (!item.isFormField()) { fileName = item.getName(); if (!fileName.isEmpty()) { String root = getServletContext().getRealPath("/"); //path where the file will be stored path = new File("C:\\Users\\AdrianKyle\\Documents\\NetBeansProjects\\Cogito\\Upload" + "/" + projectName + " - " + user.getFirstName() + user.getLastName() + "'s report"); if (!path.exists()) { boolean status = path.mkdirs(); } uploadedFiles = new File(path + "/" + fileName); item.write(uploadedFiles); files.add(fileName); } else { } } } for (int x = 0; x < files.size(); x++) { String filename = files.get(x); if (!filename.isEmpty()) { String[] parts = filename.split(Pattern.quote(".")); String extension = parts[1]; //Videos if (extension.equalsIgnoreCase("mp4") || extension.equalsIgnoreCase("avi") || extension.equalsIgnoreCase("3gp") || extension.equalsIgnoreCase("flv") || extension.equalsIgnoreCase("wmv")) { category = "video"; break; } //Images else if (extension.equalsIgnoreCase("png") || extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("bmp")) { category = "image"; break; } } } //citizenDAO.uploadReport(r); //Testimonial t = new Testimonial(0, title, null, description, "chrome-extension://pediieahejagkkidddjjbiaocanpcjik/Citizen/" + title, location, locationdetails, category, null,"Pending", user); //Testimonial t = new Testimonial(0, title, null, description, "chrome-extension://hjllakiciieioomhnnoiljeofbacabpc/" + title, location, locationdetails, category, null, user); //Upload testimonial //citizenDAO.uploadTestimonial(t); Citizen_Report report = new Citizen_Report(0, message, "chrome-extension://affgigbpilagplecbaihihkcaddmlkji/" + projectName + " - " + user.getFirstName() + user.getLastName() + "'s report", null, user, proj); citizenDAO.uploadCitizenReport(report); int id = citizenDAO.getRecentPostid(user, message); report.setId(id); //Add activity actdao.addActivity(new Activity(0, "you have submitted a report", null, user.getUser())); //Place in appropriate tables for (int x = 0; x < files.size(); x++) { String filename = files.get(x); if (!filename.isEmpty()) { String[] parts = filename.split(Pattern.quote(".")); String extension = parts[1]; //Videos if (extension.equalsIgnoreCase("mp4") || extension.equalsIgnoreCase("avi") || extension.equalsIgnoreCase("3gp") || extension.equalsIgnoreCase("flv") || extension.equalsIgnoreCase("wmv") || extension.equalsIgnoreCase("mkv")) { Report_File rf = new Report_File(); rf.setFilename(files.get(x)); rf.setType("Video"); rf.setCitizenReport(report); rf.setUploader(c.getFirstName()); rf.setDescription("Submitted video"); //Get the project rf.setProject(proj); //rf.setStatus("Approved"); //upload report files citizenDAO.uploadFiles(rf); } //Images else if (extension.equalsIgnoreCase("png") || extension.equalsIgnoreCase("jpeg") || extension.equalsIgnoreCase("jpg") || extension.equalsIgnoreCase("bmp")) { Report_File rf = new Report_File(); rf.setFilename(files.get(x)); rf.setType("Image"); rf.setCitizenReport(report); rf.setUploader(c.getFirstName()); rf.setDescription("Submitted image"); //Get the project rf.setProject(proj); //rf.setStatus("Approved"); //upload report files citizenDAO.uploadFiles(rf); } } } } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception ex) { Logger.getLogger(Citizen_SubmitReport.class.getName()).log(Level.SEVERE, null, ex); } } request.setAttribute("success", category + "Success"); ServletContext context = getServletContext(); RequestDispatcher dispatch = context.getRequestDispatcher("/Citizen_Home"); dispatch.forward(request, response); } finally { out.close(); } }
From source file:Servlet.Contractor_ReUpload.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from ww w .jav a 2s .com*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); try { InputStream inputStream = null; ArrayList<String> files = new ArrayList<String>(); ArrayList<String> fileType = new ArrayList<String>(); ContractorDAO contDAO = new ContractorDAO(); OCPDDAO oc = new OCPDDAO(); //Gets the contractor Contractor_User contractor_user = (Contractor_User) session.getAttribute("user"); Contractor contractor = contractor_user.getContractor(); boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List items = upload.parseRequest(request); Iterator iterator = items.iterator(); String id = null; int docID = 0; String title = null; File path = null; //File path = null; File uploadedFiles = null; String fileName = null; String documentType = null; while (iterator.hasNext()) { FileItem item = (FileItem) iterator.next(); if (item.isFormField()) { //Returns the string inside the field String value = item.getString(); //returns the name of the field String value2 = item.getFieldName(); if (value2.equals("docID")) { docID = Integer.parseInt(value); } if (value2.equals("doctype1")) { documentType = value; } if (value2.equals("ProjectID")) { id = value; } if (value2.equals("ProjectName")) { title = value; } } if (!item.isFormField()) { fileName = item.getName(); String root = getServletContext().getRealPath("/"); //path where the file will be stored path = new File("C:\\Users\\AdrianKyle\\Documents\\NetBeansProjects\\Cogito\\Upload" + "/Bids and Awards Department" + "/Eligibility Documents/" + title + "/" + contractor.getName()); if (!path.exists()) { boolean status = path.mkdirs(); } uploadedFiles = new File(path + "/" + fileName); item.write(uploadedFiles); } } Project project = oc.getBasicProjectDetails(id); Contractor_Has_Project contProject = contDAO.getContractorHasProject(project, contractor); Eligibility_Document document = new Eligibility_Document(0, fileName, "chrome-extension://affgigbpilagplecbaihihkcaddmlkji/" + "Bids and Awards Department" + "/Eligibility Documents/" + title + "/" + contractor.getName(), null, contProject, documentType, null, ""); contDAO.updateEligibilityDocument(docID, document); } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception ex) { Logger.getLogger(Contractor_UploadContractorDocuments.class.getName()).log(Level.SEVERE, null, ex); } } request.setAttribute("success", "Success"); ServletContext context = getServletContext(); RequestDispatcher dispatch = context.getRequestDispatcher("/Contractor_HandleInvitations"); dispatch.forward(request, response); } finally { out.close(); } }