mardi 5 janvier 2021

Handle FormData file in java servlet

I currently have an HTML form containing a couple of checkboxes and a file upload. The goal is to dynamically send this information to my java servlet for further processing when a submit button is clicked, however, I am unable to collect the file.

My HTML form currently looks like this:

    <form enctype="multipart/form-data" id="UploadForm" name="UploadForm">
    <fieldset class="fieldset" id="fieldset" style="margin-bottom: 16px; margin-top: 1px">
        <legend>Which Algorithms would you like to visualize?</legend>
        <label for="ZeroRCheckBox">ZeroR</label>
        <input id="ZeroRCheckBox" type="checkbox" value="ZeroR" name="algorithms"><br/>
        <label for="OneRCheckBox">OneR</label>
        <input id="OneRCheckBox" type="checkbox" value="OneR" name="algorithms"><br/>
        <label for="J48CheckBox">J48</label>
        <input id="J48CheckBox" type="checkbox" value="J48" name="algorithms"><br/>
        <label for="RandomForrestCheckBox">RandomForrest</label>
        <input id="RandomForrestCheckBox" type="checkbox" value="RandomForrest" name="algorithms"><br/>
        <label for="RandomTreeCheckBox">RandomTree</label>
        <input id="RandomTreeCheckBox" type="checkbox" value="RandomTree" name="algorithms"><br/>
        <label for="NaiveBayesCheckBox">Naive Bayes</label>
        <input id="NaiveBayesCheckBox" type="checkbox" value="NaiveBayes" name="algorithms"><br/>
        <label for="NearestNeighborCheckBox">Nearest Neighbor</label>
        <input id="NearestNeighborCheckBox" type="checkbox" value="NearestNeighbor" name="algorithms"><br/>
        <label for="SMOCheckBox">SMO</label>
        <input id="SMOCheckBox" type="checkbox" value="SMO" name="algorithms"><br/>
    </fieldset>

    <div class="input-file-container">
        <input class="input-file" id="my-file" type="file" name="file" accept=".csv,.arff" required>
        <label tabindex="0" for="my-file" class="input-file-trigger">Select a file...</label>
    </div>
    <p class="file-return"></p>

    <button id="PostBTN" class="success button" style="margin-top: 20px">Get Visualizations!</button>

</form>

The PostBTN at the bottom is linked to a javascript function that should submit the information in a FormData object, which looks like this:

var formData = new FormData()

$('#PostBTN').click( function() {

    let algorithms = $("input:checkbox:checked").map(function(){
        return this.value;
    }).toArray();
    formData.append("algorithms", algorithms);

    var file = $("#my-file").val();
    formData.append("file", file);

    var xhr = new XMLHttpRequest;
    xhr.open('POST', '/run', true);
    xhr.send(formData);
});

Using XMLHttpRequest the data is sent to my java servlet where the information should be read. When collecting the information I am able to retrieve the algorithm's array, However, when getting the file using getPart, errors start showing up.

@WebServlet(name = "RunServlet", urlPatterns = "/run")
@MultipartConfig
public class RunServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // Getting the session information
        HttpSession session = request.getSession();
        String sessionID = session.getId();

        Enumeration<String> params = request.getParameterNames();
        while(params.hasMoreElements()){
            String paramName = params.nextElement();
            System.out.println("Parameter Name - "+paramName+", Value - "+request.getParameter(paramName));
        }

        System.out.println("GOING TO FETCH ALGORITHMS");
        String[] algorithms = request.getParameterValues("algorithms");
        System.out.println(Arrays.toString(algorithms));
    
        System.out.println("GOING TO FETCH FILENAME");
        Part filePart = request.getPart("file");
        String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
        System.out.println(fileName);

When printing the contents of the request I can see that it does contain the file.

Parameter Name - algorithms, Value - J48,RandomForrest,RandomTree
Parameter Name - file, Value - C:\fakepath\iris.2D.arff
GOING TO FETCH ALGORITHMS
[J48,RandomForrest,RandomTree]
PRINTED ALGORITHMS
GOING TO FETCH FILENAME
**FILE NOT PRINTED**

What should I do to fix this code in order to obtain the file and parse it?




Aucun commentaire:

Enregistrer un commentaire