0

I am trying to implement a file-upload functionality using webkitdirectory with java backend.

Step1. Design a HTML form with webkitdirectory

<form action="DataUpload" method="post" enctype="multipart/form-data">
<input type="text" name="dbName" value="Database Name Here" id="dbName"/>
    <input type="file" id="ctrl" webkitdirectory directory multiple/>
    <input type="submit" />
</form>

Step 2. Passing information from form to Servlet

public class DataUpload extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response){

    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator fileIterator;
    try {
        fileIterator = upload.getItemIterator(request);
        InputStream inputStream = null;
        BufferedReader br = null;
        System.out.println("CheckPoint 1");
        while(fileIterator.hasNext()) {
            System.out.println("CheckPoint 2");
            FileItemStream item = fileIterator.next();
            String inputFileName = FilenameUtils.getName(item.getName());
            inputStream = item.openStream();

            inputFileName = inputFileName.split("\\.")[0];
            List<String[]> list = new ArrayList<String[]>();                
            // Getting File
            br = new BufferedReader(new InputStreamReader(inputStream));    // Getting the object to read file
            String line;
            while((line = br.readLine())!= null){// While condition ends then end of file is reached.
                list.add(line.split(","));
            }
            // Checking if File is Empty
            if (list.size() == 0){
                System.err.println("File Empty");
            }else{
                // TODO : Parameter Parser.
                // DO JOB HERE
            }           
        }
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

My code, doesn't give any programming error, but it does not pass through the CheckPoint 2, i.e. it doesn't go inside the while loop. I tried looking into various post, such as:

Keep Directory Structure When Uploading
How to upload files to server using JSP/Servlet? - While this question shows maximum resemblance to problem in question, This question works for selecting multiple files in a folder, where the problem here is question is to upload files in different sub directories inside a folder.

I was wondering, if this was possible using solely java servlets without using javascript. I was able to upload multiple files inside a single folder. But, code doesn't seem to work, when I select a folder as input, instead it works when I select a particular file or a subset of files.

0

2 Answers 2

0

HTML File Form

<form onsubmit="readFiles()">
    <input type="file" name="files" webkitdirectory directory multiple id="files">
    <input type="submit">
</form>

JavaScript Function

function readFiles(){
    var files = document.getElementById("files").files;
    for (var file = 0 ; file < files.length; file++) {
        var reader = new FileReader();
        reader.onload = function(e){
            var object = new Object();
            object.content = e.target.content;
            var json_upload = "jsonObject=" + JSON.stringify(object);
            var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance 
            xmlhttp.open("POST", "http://localhost:8080/readFileServlet");
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.send(json_upload);
        }
        reader.readAsBinaryString(files);
    }
}

Java Function:

JSONObject jObj = new JSONObject(request.getParameter("jsonObject"));
Sign up to request clarification or add additional context in comments.

Comments

-1

in a post method try

make @Autowired ServletContext c; or take object from it in servlets

        byte[] bytes = file.getBytes();

         String UPLOAD_FOLDEdR=c.getRealPath("/images");     
        Path path = Paths.get(UPLOAD_FOLDEdR +"/"+ file.getOriginalFilename());
        Files.write(path, bytes);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.