Java tutorial
package com.fatih.edu.tr; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author RidvanBal */ import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; 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; /** * Servlet implementation class UploadServlet */ public class NewTaskServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String DATA_DIRECTORY = "data"; private static final int MAX_MEMORY_SIZE = 5000 * 1024 * 2; private static final int MAX_REQUEST_SIZE = 5000 * 1024; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Check that we have a file upload request boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (!isMultipart) { return; } // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Sets the size threshold beyond which files are written directly to // disk. factory.setSizeThreshold(MAX_MEMORY_SIZE); // Sets the directory used to temporarily store files that are larger // than the configured size threshold. We use temporary directory for // java factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); // constructs the folder where uploaded file will be stored String uploadFolder = getServletContext().getRealPath("") + File.separator + DATA_DIRECTORY; // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint upload.setSizeMax(MAX_REQUEST_SIZE); TaskDao taskDao = new TaskDao(); String title = ""; String description = ""; String due_date = ""; String fileName = ""; String filePath = ""; //taskDao.addTask(title, description, due_date, "testfile","testdizi",1); try { // Parse the request List items = upload.parseRequest(request); Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { fileName = new File(item.getName()).getName(); filePath = uploadFolder + File.separator + fileName; File uploadedFile = new File(filePath); System.out.println(filePath); item.write(uploadedFile); } else { String fieldname = item.getFieldName(); String fieldvalue = item.getString(); if (fieldname.equals("title")) { title = item.getString(); } else if (fieldname.equals("description")) { description = item.getString(); } else if (fieldname.equals("due_date")) { due_date = item.getString(); } else { System.out.println("Something goes wrong in form"); } } } taskDao.addTask(title, description, due_date, fileName, filePath, 1); // displays done.jsp page after upload finished getServletContext().getRequestDispatcher("/done.jsp").forward(request, response); } catch (FileUploadException ex) { throw new ServletException(ex); } catch (Exception ex) { throw new ServletException(ex); } } }