Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailemVyřešeno Čtení formuláře multipart/form-data v servletu.

Snažím se uploadovat soubory v servletu a současně odeslat z formuláře checkbox.

Formulář:

<form method="post" action="" enctype="multipart/form-data"> 

Metoda do POST:

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpServletRequestWrapper a = new HttpServletRequestWrapper(req);
        String[] parameterNotifiEmail = a.getParameterValues(PARAMETER_NOTIFI_EMAIL);
        String[] parameterPriority = a.getParameterValues(PARAMETER_PRIORITY);
        System.out.println("-------------------------------------------------------------");
        System.out.println("2: " + Arrays.toString(parameterNotifiEmail));
        System.out.println("3: " + Arrays.toString(parameterPriority));
        System.out.println("-------------------------------------------------------------");

        // configures upload settings
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // sets memory threshold - beyond which files are stored in disk
        factory.setSizeThreshold(MEMORY_THRESHOLD);
        // sets temporary location to store files
        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

        ServletFileUpload upload = new ServletFileUpload(factory);

        // sets maximum size of upload file
        upload.setFileSizeMax(MAX_FILE_SIZE);

        // sets maximum size of request (include file + form data)
        upload.setSizeMax(MAX_REQUEST_SIZE);

        // constructs the directory path to store upload file
        // this path is relative to application's directory
        String uploadPath = getServletContext().getRealPath("")
            + File.separator + UPLOAD_DIRECTORY;

        // creates the directory if it does not exist
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }

        try {
            // parses the request's content to extract file data
            @SuppressWarnings("unchecked")
            List<FileItem> formItems = upload.parseRequest(req);

            if (formItems != null && formItems.size() > 0) {
                // iterates over form's fields
                for (FileItem item : formItems) {
                    // processes only fields that are not form fields
                    if (!item.isFormField()) {
                        String fileName = new File(item.getName()).getName();
                        String filePath = uploadPath + File.separator + fileName;
                        File storeFile = new File(filePath);

                        // saves the file on disk
                        item.write(storeFile);
                        req.setAttribute("message",
                            "Upload has been done successfully!");
                        System.out.println(fileName); //<------------ FILE
                    }
                }
            } else {
                System.out.println("NENI NIC V LISTU");
            }
        } catch (Exception ex) {
            req.setAttribute("message",
                "There was an error: " + ex.getMessage());
        }

Problém je v tom, mi nejde číst soubory a současně číst parametr check box, když zakomentím jedno, funguje druhé, když zakomentím druhé funguje první.
Jinak je v tom druhém NULL.

Nechápu proč, co dělám špatně?

Třída má anotaci

@WebServlet("/admin/add-request")
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 10, // 10 MB 
    maxFileSize = 1024 * 1024 * 50, // 50 MB
    maxRequestSize = 1024 * 1024 * 100)   	// 100 MB
Předmět Autor Datum
Ach jo, zas ty Servlety... Normalne se to dela takhle: uploading-files
MaSo 06.08.2016 17:36
MaSo
Vše jsem měl z examplů a tutoriálků, ale nechápu proč mi vždy fungovalo buď jedno, nebo druhé.
MašinkaTomáš 06.08.2016 17:41
MašinkaTomáš
na začiatok skontroluj http request a response, či tam tie dáta naozaj máš zlaté php, stručne jasne…
čitateľ 06.08.2016 18:04
čitateľ
Vyřešil jsem to tím, že jsem udělal 2 stránky a dva formuláře, jinak bych se z toho asi PO*. Protože… poslední
MašinkaTomáš 13.08.2016 13:27
MašinkaTomáš

Zpět do poradny Odpovědět na původní otázku Nahoru