JSP File Forms

0

I have in my form to receive a file and it does it well, but if I add inputs type text to the form, I no longer receive the file ...

I think the problem comes from this line

  String contentType = request.getContentType();

Because it receives many different types and then it does not get into the if ... but I have no idea how to take a request.getParemeter (DEL file), in parentheses the name of this itself ... But when it comes to collecting it in the JSP so that it enters the ¿¿if ???

        String textarea = request.getParameter("txtarea");
        String origin = request.getParameter("txtorigin");
        String topic = request.getParameter("txttopic");

        String[] destination = request.getParameterValues("user[]");

        //---------- Upload file to server
        File file;
        int maxFileSize = 5000 * 1024;
        int maxMemSize = 5000 * 1024;
        String filePath = "c:/apache-tomcat/webapps/data/";

        String contentType = request.getContentType();

        if ((contentType.indexOf("multipart/form-data") >= 0)) {

            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setSizeThreshold(maxMemSize);
            factory.setRepository(new File("c:\temp"));
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setSizeMax(maxFileSize);
            try {
                List fileItems = upload.parseRequest(request);
                Iterator i = fileItems.iterator();
                out.println("<html>");
                out.println("<body>");
                while (i.hasNext()) {
                    FileItem fi = (FileItem) i.next();
                    if (!fi.isFormField()) {
                        String fieldName = fi.getFieldName();
                        String fileName = fi.getName();
                        boolean isInMemory = fi.isInMemory();
                        long sizeInBytes = fi.getSize();
                        file = new File(filePath + "yourFileName");
                        fi.write(file);
                        out.println("Uploaded Filename: " + filePath + fieldName + "<br>");
                    }
                }

            } catch (Exception ex) {
            }
        }// end upload file
    
asked by EduBw 11.10.2017 в 12:04
source

1 answer

0

Use libraries like: commons-fileupload-1.3.3 and commons-io-2.5 You do not have to put the type of content, and what you would do would be more or less the following, everything depends on what you want:

    <%@ page import="org.apache.commons.fileupload.*" %>
    <%@ page import="org.apache.commons.fileupload.disk.*" %>
    <%@ page import="org.apache.commons.fileupload.servlet.*" %>
    <%@ page import="org.apache.commons.io.*" %>
    <%@ page import="java.io.*" %>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <% request.setCharacterEncoding("UTF-8"); %>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title></title>
        </head>
        <body>
            <%!
              String mensaje ="";
            %>      
            <%

                FileItemFactory file_factory = new DiskFileItemFactory();
                /*ServletFileUpload esta clase convierte los input file a FileItem*/
                ServletFileUpload servlet_up = new ServletFileUpload(file_factory);
                /*sacando los FileItem del ServletFileUpload en una lista */
                List items = servlet_up.parseRequest(request);

                for(int i=0;i<items.size();i++){
                    /*FileItem representa un archivo en memoria que puede ser pasado al disco duro*/
                    FileItem item = (FileItem) items.get(i);
                    /*item.isFormField() false=input file; true=text field*/
                    if (! item.isFormField()){
                        continue;
                    }else if(item.getFieldName().equalsIgnoreCase("documento")){
                        dni_persona=Integer.parseInt(item.getString());
                    }
                 }               
                            }
                for(int i=0;i<items.size();i++){
                    /*FileItem representa un archivo en memoria que puede ser pasado al disco duro*/
                    FileItem item = (FileItem) items.get(i);
                    /*item.isFormField() false=input file; true=text field*/
                     if (! item.isFormField()){
                        /*cual sera la ruta al archivo en el servidor*/

                        if(item.getSize()>0){/*Si el input file archivo pesa (hay archivo) lo crea*/

                            File archivo_a_crear = new File("c\unaruta\"+item.getFieldName()+".pdf");
                            if(archivo_a_crear.exists())archivo_a_crear.delete();
                            item.write(archivo_a_crear);
                            /*Verificamos si el archivo ya existe*/
                            /*Creamos el archivo*/
                        }
                    } 
                }

            %>
            </script>      
        </body>
    </html>
    
answered by 11.10.2017 в 17:26