I want to copy the contents of an uploaded file to a text file on my local.
The important thing is- I already have a file on my system named "Data" and I want to append the contents of uploaded file in "Data".
I have successfully copied the contents of the uploaded file but facing issue in appending the data.
Below is my Servlet file:
package pack;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadHandler extends HttpServlet {
    private final String UPLOAD_DIRECTORY = "D:/Data Repository.txt";
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //process only if its multipart content
        if(ServletFileUpload.isMultipartContent(request)){
            try {
                List<FileItem> multiparts = new ServletFileUpload(
                                         new DiskFileItemFactory()).parseRequest(request);
                for(FileItem item : multiparts){
                    if(!item.isFormField()){
                        item.write(new File(UPLOAD_DIRECTORY));
                    }
                }
               //File uploaded successfully
               request.setAttribute("message", "File Uploaded Successfully");
            } catch (Exception ex)
            {
               request.setAttribute("message", "File Upload Failed due to " + ex);
            }          
        }else{
            request.setAttribute("message",
                                 "Sorry this Servlet only handles file upload request");
        }
    request.getRequestDispatcher("/result.jsp").forward(request, response);
    } 
}
Can something be added or modified in the below statement so that it would append the data to the existing file rather than creating a new file every time and copying its content?
item.write(new File(UPLOAD_DIRECTORY));
 
    