如何在一篇文章中将html复选框信息和文件输入发送到javaservlet?

vyu0f0g1  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(327)

我目前正在尝试建立一个网站,用户可以可视化不同的机器学习算法的学校项目。但是,在尝试收集选中的复选框和输入文件时,我无法在servlet中获取该文件。我查看了几篇关于ajax、formdata等的帖子,但没有一篇能帮我解决这个问题。
从一开始就是我的html表单,它包含了用户所需的所有输入/选项。
HTML表单

<form action="#" id="UploadForm">
<input id="ZeroRCheckBox" type="checkbox" value="ZeroR"><label for="ZeroRCheckBox">ZeroR</label>
<input id="OneRCheckBox" type="checkbox" value="OneR"><label for="OneRCheckBox">OneR</label>
<input id="J48CheckBox" type="checkbox" value="J48"><label for="J48CheckBox">J48</label>
<input id="RandomForrestCheckBox" type="checkbox" value="RandomForrest"><label for="RandomForrestCheckBox">RandomForrest</label>
<input id="RandomTreeCheckBox" type="checkbox" value="RandomTree"><label for="RandomTreeCheckBox">RandomTree</label>
<input id="NaiveBayesCheckBox" type="checkbox" value="NaiveBayes"><label for="NaiveBayesCheckBox">Naive Bayes</label>
<input id="NearestNeighborCheckBox" type="checkbox" value="NearestNeighbor"><label for="NearestNeighborCheckBox">Nearest Neighbor</label>
<input id="SMOCheckBox" type="checkbox" value="SMO"><label for="SMOCheckBox">SMO</label>
<input type="file" name="file" accept=".csv,.arff">
<p>Upload the information here!</p>
<button id="btn">Upload file!</button>
<p class="help-text" id="passwordHelpText">The input should either be in .csv or .arff format.</p>

此表单包含按钮(btn),该按钮应触发一个javascript函数,该函数在formdata对象中收集复选框和文件。然后使用ajax将该对象发送到java后端。
javascript语言

function submit() {
    let algorithms = $("input:checkbox:checked").map(function () {
        return this.value;
    }).toArray();
    var form = $('UploadForm')[0];
    var data = new FormData(form);
    data.append("algorithms[]", algorithms)
    $.ajax({
        url: "boundary-visualizer",
        data: {
            data: data,
        },
        type: "POST",
        enctype: 'multipart/form-data',
        contentType: false,
        processData: false
    });
}
document.getElementById("btn").addEventListener("click", submit);

然后在java servlet中收集这些信息,如下所示:
服务程序

@MultipartConfig

...

System.out.println("GOING TO FETCH ALGORITHMS");
String[] algorithms = request.getParameterValues("algorithms[]");

System.out.println(Arrays.toString(algorithms));
System.out.println("PRINTED ALGORITHMS");

System.out.println("GOING TO FETCH FILENAME");
Part filePart = request.getPart("form");String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();

System.out.println(fileName);
System.out.println("PRINTED FILENAME");

当检查控制台以查看不同的print语句时,它显示算法数组已成功收集和打印,但是文件没有显示,后面的print语句也没有执行。
慰问

GOING TO FETCH ALGORITHMS
[NaiveBayes, NearestNeighbor]
PRINTED ALGORITHMS
GOING TO FETCH FILENAME
...

Followed by this error in another log

org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [VisualizerServlet] in context with path [] threw exception [org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded; charset=UTF-8] with root cause

有什么问题吗?或者如果有更简单/更好的方法,我应该怎么做?欢迎您的帮助和建议!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题