Skip to content

Upload file on Servlet #2

@Magador

Description

@Magador

In your HTML form, you just need a form with a file input and a submit button:

<form method="post" action="path/to/servlet" enctype="multipart/form-data">
    <input type="file" required name="file"/>
    <input type="submit" />
</form>

Minimal HTML for single file upload.
In the server-side using Servlet 3.0 API, some simple code are needed:

@MultipartConfig(location = "path/to/file/directory", fileSizeThreshold = maxSizeOfFileOnDisk)
@WebServlet("/path/to/servlet")
public class FileServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part file = req.getPart("file");
        if (file != null) {
            file.write(name);
        }
    }
}

This is the minimal required code for this to work.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions