1

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));
Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
  • Can something be added or modified in the below statement so that it would append the data to the existing file rather than creating new file every time and copying its content- item.write(new File(UPLOAD_DIRECTORY)); – Kritika Sharma May 03 '16 at 10:58
  • Try something like FileWriter fw = new FileWriter(yourFile, true); fw.write(item.getString()); – rickz May 03 '16 at 14:18
  • See [How to append text to an existing file in Java](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java), might be useful for you. Hope you can upload the file and had issues in updating the file. – Vinoth Krishnan May 03 '16 at 14:22
  • Thanks...it worked for me :) – Kritika Sharma May 06 '16 at 07:33
  • Please post your solution. – rickz May 09 '16 at 04:24

0 Answers0