Uploading files and data SERVLET

1

How do I receive files and other parameters in Servlet? Here is the shipping code:

    <form action="myservlet" method="post" enctype="multipart/form-data">
      <input type="text" name="mytext">
      <input type="file" name="myfile">
      <input type="submit" value="submit">
    </form>

ps: I saw examples of how to get only the "file" but I do not know how to get the "file" and "text" in the servlet.

    
asked by anonymous 01.11.2017 / 22:05

2 answers

0

You get the data of the inputText and file by name, using request.getParameter(""); by putting the name of the input text in the quotation marks with parentheses Example:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

String nome = request.getParameter("mytext");
 String file = request.getParameter("file");
}
    
01.11.2017 / 22:17
0

Solution to my case:

    PrintWriter out = response.getWriter();
    Map<String, String> fields = new HashMap<>();
    Map<String, List<String>> multiValueFields = new HashMap<>();

    if (!ServletFileUpload.isMultipartContent(request)) {
        out.println("Nothing to upload");
        return;
    }
    FileItemFactory itemfactory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(itemfactory);
    try {

        List<FileItem> items = upload.parseRequest(request);
        for (FileItem item : items) {

            if (!item.isFormField()) {
                String contentType = item.getContentType();
                if (!contentType.equals("image/png")) {
                    out.println("only png format image files supported");
                    continue;
                }
                File uploadDir = new File("C:\");
                File file = File.createTempFile("img", ".png", uploadDir);

                item.write(file);

                fields.put("imagem", file.getPath());

                out.println("File Saved Successfully");
            } else {
                String name = item.getFieldName();
                String value = item.getString();

                if (!multiValueFields.containsKey(name)) {
                    multiValueFields.put(name, new ArrayList<String>());
                }

                fields.put(name, value);
                multiValueFields.get(name).add(value);
            }
        }

        String nome = fields.get("nome");
        String descricao = fields.get("descricao");
        String tipo = fields.get("tipo");

        String nomeImagem = fields.get("imagem");
        List<String> opcao = multiValueFields.get("opcao");

        Exercicio e = new Exercicio();
        e.setNome(nome);
        e.setTipo(tipo);
        e.setDescricao(descricao);
        Instrutor i = new Instrutor(request.getSession().getAttribute("usuario").toString());
        e.setInstrutor(i);
        e.setImagem(nomeImagem);
        ExercicioDAO dao = new ExercicioDAO();

        dao.cadastrar(e); //cadastra no db o obj exercicio

    } catch (FileUploadException e) {
        e.printStackTrace();
        out.println("upload fail");
    } catch (Exception ex) {
        ex.printStackTrace();
        out.println("can't save");
    }
    
03.11.2017 / 06:18