1

I have a form in which I have a type="file" and a type="submit", now I need to send the chosen file to server, but the problem is that whenever I submit my form I get a huge apache error saying:

HTTP Status 500 - org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded

Here is my client-side script:

<form method="POST" action="../propicuploader">
            <div class="browsephoto_div">
                <label class="takeapicture_btn">
                    <input type="file" accept=".png, .gif, .jpeg, .jpg" id="imagetoupload" enctype="multipart/form-data" onchange="document.getElementById('myImg').src = window.URL.createObjectURL(this.files[0])" required/>
                    <span>Browse</span>
                </label>
            </div>
            <div class="ProfilePicsubmit_div">
                <input type="submit" class="Profilpicsubmit_btn" value="Next"/>
            </div>
        </form>

My Server side script:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    String description = request.getParameter("description"); // Retrieves <input type="text" name="description">
    Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
    String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
    InputStream fileContent = filePart.getInputStream();
    File file = new File("D:\\image123.jpg");
    file.createNewFile();
    OutputStream stream = new DataOutputStream(new FileOutputStream(file));
    IOUtils.copy(fileContent,stream);
}

I am using apache v9.0 and java EE 8

Jorden
  • 41
  • 1
  • 6

1 Answers1

0

you must add to you form declaration enctype="multipart/form-data" EXample: <form method="POST" action="../propicuploader" enctype="multipart/form-data" >

if you want more info: What does enctype='multipart/form-data' mean?