List of usage examples for org.apache.commons.fileupload FileItem write
void write(File file) throws Exception;
From source file:com.official.wears.site.UploadServlet.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // Check that we have a file upload request isMultipart = ServletFileUpload.isMultipartContent(request); response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); if (!isMultipart) { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); return;/*from w w w. jav a 2s .c om*/ } DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File("c:\\temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax(maxFileSize); try { // Parse the request to get file items. List fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); if (!fi.isFormField()) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); String fileName = fi.getName(); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // Write the file if (fileName.lastIndexOf("\\") >= 0) { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); out.println("Uploaded Filename: " + fileName + "<br>"); } } out.println("</body>"); out.println("</html>"); } catch (Exception ex) { System.out.println(ex); } }
From source file:Control.LoadImage.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request//from ww w . j a v a 2s. 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 { String tempPath = "/temp"; String absoluteTempPath = this.getServletContext().getRealPath(tempPath); String absoluteFilePath = this.getServletContext().getRealPath("/data/Image"); int maxFileSize = 50 * 1024; int maxMemSize = 4 * 1024; try { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(maxMemSize); File file = new File(absoluteTempPath); if (file == null) { // tao thu muc } factory.setRepository(file); ServletFileUpload upload = new ServletFileUpload(factory); //upload.setProgressListener(new MyProgressListener(out)); List<FileItem> items = upload.parseRequest(request); if (items.size() > 0) { for (FileItem item : items) { if (!item.isFormField()) { if ("images".equals(item.getFieldName())) { if (item.getName() != null && !item.getName().isEmpty()) { String extension = null; if (item.getName().endsWith("jpg")) { String newLink = "/Image/" + Image.LengthListImg() + ".jpg"; file = new File(absoluteFilePath + "//" + Image.LengthListImg() + ".jpg"); // G?i hm add hnh vo database, link hnh l newLink item.write(file); } else if (item.getName().endsWith("png")) { String newLink = "/Image/" + Image.LengthListImg() + ".png"; file = new File(absoluteFilePath + "//" + Image.LengthListImg() + ".png"); // G?i hm add hnh vo database, link hnh l newLink item.write(file); } else if (item.getName().endsWith("JPG")) { String newLink = "/Image/" + Image.LengthListImg() + ".JPG"; file = new File(absoluteFilePath + "//" + Image.LengthListImg() + ".JPG"); // G?i hm add hnh vo database, link hnh l newLink item.write(file); } else if (item.getName().endsWith("PNG")) { String newLink = "/Image/" + Image.LengthListImg() + ".PNG"; file = new File(absoluteFilePath + "//" + Image.LengthListImg() + ".PNG"); // G?i hm add hnh vo database, link hnh l newLink item.write(file); } } } } else { } } response.sendRedirect(request.getContextPath() + "/LoadImage.jsp"); return; } List<Image> images = loadImages(request, response); request.setAttribute("images", images); request.setAttribute("error", "No file upload"); RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/LoadImage.jsp"); dispatcher.forward(request, response); } catch (Exception ex) { System.err.println(ex); } }
From source file:com.globalsight.dispatcher.controller.TranslateXLFController.java
@RequestMapping(value = "/upload", method = RequestMethod.POST) public void uploadXLF(HttpServletRequest p_request, HttpServletResponse p_response) throws IOException, FileUploadException, JSONException { String securityCode = p_request.getParameter(JSONPN_SECURITY_CODE); Account account = accountDAO.getAccountBySecurityCode(securityCode); if (account == null) { JSONObject jsonObj = new JSONObject(); jsonObj.put(JSONPN_STATUS, STATUS_FAILED); jsonObj.put(JSONPN_ERROR_MESSAGE, "The security code is incorrect!"); logger.error("The security code is incorrect -->" + securityCode); p_response.getWriter().write(jsonObj.toString()); return;/*from w w w. j a va 2 s . co m*/ } File fileStorage = CommonDAO.getFileStorage(); File tempDir = CommonDAO.getFolder(fileStorage, FOLDER_TEMP); String jobIDStr = "-1"; File srcFile = null; JobBO job = null; String errorMsg = null; if (ServletFileUpload.isMultipartContent(p_request)) { jobIDStr = RandomStringUtils.randomNumeric(10); List<FileItem> fileItems = new ServletFileUpload(new DiskFileItemFactory(1024 * 1024, tempDir)) .parseRequest(p_request); for (FileItem item : fileItems) { if (item.isFormField()) { } else { String fileName = item.getName(); fileName = fileName.substring(fileName.lastIndexOf(File.separator) + 1); File srcDir = CommonDAO.getFolder(fileStorage, account.getAccountName() + File.separator + jobIDStr + File.separator + XLF_SOURCE_FOLDER); srcFile = new File(srcDir, fileName); try { item.write(srcFile); logger.info("Uploaded File:" + srcFile.getAbsolutePath()); } catch (Exception e) { logger.error("Upload error with File:" + srcFile.getAbsolutePath(), e); } } } // Initial JobBO job = new JobBO(jobIDStr, account.getId(), srcFile); // Prepare data for MT String parseMsg = parseXLF(job, srcFile); if (parseMsg == null) errorMsg = checkJobData(job); else errorMsg = parseMsg; if (errorMsg == null || errorMsg.trim().length() == 0) { // Do MT doMachineTranslation(job); jobMap.put(job.getJobID(), job); } else { // Cancel Job job = null; } } JSONObject jsonObj = new JSONObject(); if (job != null) { jsonObj.put(JSONPN_JOBID, job.getJobID()); jsonObj.put(JSONPN_SOURCE_LANGUAGE, job.getSourceLanguage()); jsonObj.put(JSONPN_TARGET_LANGUAGE, job.getTargetLanguage()); jsonObj.put("sourceSegmentSize", job.getSourceSegments().length); logger.info("Created Job --> " + jsonObj.toString()); } else { jsonObj.put(JSONPN_STATUS, STATUS_FAILED); jsonObj.put(JSONPN_ERROR_MESSAGE, errorMsg); logger.error("Failed to create Job --> " + jsonObj.toString() + ", file:" + srcFile + ", account:" + account.getAccountName()); } p_response.getWriter().write(jsonObj.toString()); }
From source file:ned.bcvs.admin.fileupload.ConstituencyFileUploadServlet.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // Check that we have a file upload request isMultipart = ServletFileUpload.isMultipartContent(request); response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); if (!isMultipart) { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); return;/*from ww w . j a v a 2 s . c o m*/ } DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File( "D:/glassfish12October/glassfish-4.0/glassfish4/" + "glassfish/domains/domain1/applications/temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax(maxFileSize); try { // Parse the request to get file items. List fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); if (!fi.isFormField()) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); fileName = fi.getName(); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // Write the file if (fileName.lastIndexOf("\\") >= 0) { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); out.println("Uploaded Filename: " + fileName + "<br>"); } } //calling the ejb method to save constituency.csv file to data base out.println(upbean.fileDbUploader(filePath + fileName, "constituency")); out.println("</body>"); out.println("</html>"); } catch (Exception ex) { System.out.println(ex); } }
From source file:ned.bcvs.admin.fileupload.CandidateFileUploadServlet.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // Check that we have a file upload request isMultipart = ServletFileUpload.isMultipartContent(request); response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); if (!isMultipart) { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); return;/* w ww.j a v a2 s . c o m*/ } DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File( "D:/glassfish12October/glassfish-4.0/glassfish4/" + "glassfish/domains/domain1/applications/temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax(maxFileSize); try { // Parse the request to get file items. List fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); if (!fi.isFormField()) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); fileName = fi.getName(); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // Write the file if (fileName.lastIndexOf("\\") >= 0) { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); out.println("Uploaded Filename: " + fileName + "<br>"); } } //calling the ejb method to save voter.csv file to data base out.println(upbean.fileDbUploader(filePath + fileName, "candidate")); out.println("</body>"); out.println("</html>"); } catch (Exception ex) { System.out.println(ex); } }
From source file:ned.bcvs.fileupload.ElectionPartyFileUploadServlet.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // Check that we have a file upload request isMultipart = ServletFileUpload.isMultipartContent(request); response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); if (!isMultipart) { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); return;//from w ww .j a va2 s . c o m } DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File( "D:/glassfish12October/glassfish-4.0/glassfish4/" + "glassfish/domains/domain1/applications/temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax(maxFileSize); try { // Parse the request to get file items. List fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); if (!fi.isFormField()) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); fileName = fi.getName(); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // Write the file if (fileName.lastIndexOf("\\") >= 0) { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); out.println("Uploaded Filename: " + fileName + "<br>"); } } //calling the ejb method to save voter.csv file to data base out.println(upbean.fileDbUploader(filePath + fileName, "electionparty")); out.println("</body>"); out.println("</html>"); } catch (Exception ex) { System.out.println(ex); } }
From source file:ned.bcvs.admin.fileupload.ConstituencyTypeFileUploadServlet.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // Check that we have a file upload request isMultipart = ServletFileUpload.isMultipartContent(request); response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); if (!isMultipart) { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); return;//from w ww. j a v a 2 s. co m } DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File( "D:/glassfish12October/glassfish-4.0/glassfish4/" + "glassfish/domains/domain1/applications/temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax(maxFileSize); try { // Parse the request to get file items. List fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); if (!fi.isFormField()) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); fileName = fi.getName(); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // Write the file if (fileName.lastIndexOf("\\") >= 0) { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); out.println("Uploaded Filename: " + fileName + "<br>"); } } //calling the ejb method to save voter.csv file to data base System.out.println("%%%%%%% " + filePath + fileName); out.println(upbean.fileDbUploader(filePath + fileName, "constituencytype")); out.println("</body>"); out.println("</html>"); } catch (Exception ex) { System.out.println(ex); } }
From source file:FileUploading.UploadServlet.java
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //check that we have a file upload request isMultipart = ServletFileUpload.isMultipartContent(req); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); if (!isMultipart) { String title = ""; out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("<body>"); out.println("<body>"); out.println("<p> No file uploaded</p>"); out.println("</body>"); out.println("</html>"); return;//from w ww.ja v a 2s .c om } DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in the memory factory.setSizeThreshold(maxMemSize); // location to save data that is larger than maxMemsize factory.setRepository(new File("C:\\temp")); //create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); //maximum file size to be upload upload.setSizeMax(maxFileSize); try { //parse the requset to get file items List fileItems = upload.parseRequest(req); // process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>Servket Upload</title>"); out.println("</head>"); out.println("<body>"); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); if (!fi.isFormField()) { //get the uploaded file parameters String fieldName = fi.getFieldName(); String fileName = fi.getName(); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInMemory = fi.getSize(); //write the file if (fileName.lastIndexOf("\\") >= 0) { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); out.println("Uploaded file name : " + fileName + "<br>"); } } out.println("</body>"); out.println("</html>"); } catch (Exception e) { } }
From source file:ned.bcvs.admin.fileupload.VoterFileUploadServlet.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // Check that we have a file upload request isMultipart = ServletFileUpload.isMultipartContent(request); response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter(); if (!isMultipart) { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); return;/*w w w .j a va 2s . com*/ } DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File( "D:/glassfish12October/glassfish-4.0/glassfish4/" + "glassfish/domains/domain1/applications/temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // maximum file size to be uploaded. upload.setSizeMax(maxFileSize); try { // Parse the request to get file items. List fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); if (!fi.isFormField()) { // Get the uploaded file parameters String fieldName = fi.getFieldName(); fileName = fi.getName(); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // Write the file if (fileName.lastIndexOf("\\") >= 0) { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\"))); } else { file = new File(filePath + fileName.substring(fileName.lastIndexOf("\\") + 1)); } fi.write(file); out.println("Uploaded Filename: " + fileName + "<br>"); out.println("Uploaded Filepath: " + filePath + fileName + "<br>"); } } //calling the ejb method to save voter.csv file to data base out.println(upbean.fileDbUploader(filePath + fileName, "voter")); out.println("</body>"); out.println("</html>"); } catch (Exception ex) { System.out.println(ex); } }
From source file:com.kmetop.demsy.modules.ckfinder.FileUploadCommand.java
/** * saves temporary file in the correct file path. * // w ww . j a va 2 s. c o m * @param path * path to save file * @param item * file upload item * @return result of saving, true if saved correctly * @throws Exception * when error occurs. */ private boolean saveTemporaryFile(final String path, final FileItem item) throws Exception { File file = new File(path, this.newFileName); AfterFileUploadEventArgs args = new AfterFileUploadEventArgs(); args.setCurrentFolder(this.currentFolder); args.setFile(file); args.setFileContent(item.get()); if (!ImageUtils.isImage(file)) { item.write(file); if (configuration.getEvents() != null) { configuration.getEvents().run(EventTypes.AfterFileUpload, args, configuration); } return true; } else if (ImageUtils.checkImageSize(item.getInputStream(), this.configuration)) { ImageUtils.createTmpThumb(item.getInputStream(), file, getFileItemName(item), this.configuration); if (configuration.getEvents() != null) { configuration.getEvents().run(EventTypes.AfterFileUpload, args, configuration); } return true; } else if (configuration.checkSizeAfterScaling()) { ImageUtils.createTmpThumb(item.getInputStream(), file, getFileItemName(item), this.configuration); if (FileUtils.checkFileSize(configuration.getTypes().get(this.type), file.length())) { if (configuration.getEvents() != null) { configuration.getEvents().run(EventTypes.AfterFileUpload, args, configuration); } return true; } else { file.delete(); this.errorCode = Constants.Errors.CKFINDER_CONNECTOR_ERROR_UPLOADED_TOO_BIG; return false; } } // should be unreacheable return false; }