commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jaime Martín <jaime.mar...@rbcdexia-is.es>
Subject problem uploading pdf file
Date Thu, 03 Dec 2009 17:42:47 GMT
Hi:
I´m using FileUpload 1.2.1 I want to upload a pdf file. As a first 
approach I want to achieve this:
I have a file C:/firm-jmp.pdf and I want to upload it to 
D:/test/firm-jmp.pdf

On the one hand I invoke the servlet this way:

    public String saveFile()
    {
        String fileName ="";
        try{
            fileName = "firm-jmp.pdf";
            String nombreFichero ="C:/".concat(fileName);

            URL url = new URL("http://localhost:8080/FondvalCNMV/upload");
            URLConnection con = url.openConnection();
             // inform the connection that we will send output and 
accept input
            con.setDoInput(true);
            con.setDoOutput(true);

           // Don't use a cached version of URL connection.
           con.setUseCaches (false);
           con.setDefaultUseCaches (false);

           con.setRequestProperty("Content-Type", 
"multipart/form-data;boundary=---------------------------7d711d1020176");
          
           // define a new PrintWriter on the output stream
           PrintWriter outWriter = new PrintWriter(con.getOutputStream());

           // send data to the servlet
           outWriter.print("fileName="+nombreFichero);
          
           outWriter.close();
          
           InputStream input = con.getInputStream();
           File outputFile = new File(fileName);
           FileOutputStream out = new FileOutputStream(outputFile);

           boolean eof = false;
           // read all bytes from the input and write them to the output
           while (!eof) {
               // read  the stream
             int byteValue = input.read();
             out.write(byteValue);
             if (byteValue  == -1)
               eof = true;
           }
           out.flush();
           out.close();
        }catch (Exception e) {
            e.printStackTrace();
            fileName ="";
        }
        return fileName;
    }

On the other hand this is my servlet´s doPost.


    protected void doPost(HttpServletRequest request, 
HttpServletResponse response)
    throws ServletException, IOException {
        String nomFichero ="";
        if (request.getParameter("firmaApoderado") != null) {
            nomFichero = request.getParameter("firmaApoderado");
            System.out.println("nomFichero"+nomFichero);
        }
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        
        if(isMultipart) {
            // Create a factory for disk-based file items
            DiskFileItemFactory factory = new DiskFileItemFactory();
            // Set factory constraints
            factory.setSizeThreshold(1000000);
            File ff = new File("C:/firma-jmp.pdf");
            factory.setRepository(ff);
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            // Set overall request size constraint
            upload.setSizeMax(10000000);
            upload.setHeaderEncoding("multipart/form-data");
       // Parse the request
            List<FileItem> items;
            try {
                items = upload.parseRequest(request);

            for(FileItem item : items) {
               
            if (item.isFormField()) {
                String name = item.getFieldName();
                String value = item.getString();
                System.out.println("name "+name);
                System.out.println("value "+value);
                String uploadFileName="D:/test/"+name ;
                File f = new File(uploadFileName);
                item.write(f);
            }
             }
            }
            catch (FileUploadException e) {
                e.printStackTrace();
            }
            catch (Exception e) {
                e.printStackTrace();
            }                   
           

        }
    }

I have no error, the matter is that items = 
upload.parseRequest(request); turns out to return no items, it seems 
that I receive no file in the request.
I guess I´m doing something wrong as for my invocation.
thanks in advance!

AVISO LEGAL
Este mensaje de correo electrónico y sus documentos adjuntos están dirigidos
exclusivamente a los destinatarios especificados. Puede contener información
confidencial o legalmente protegida. No hay renuncia a la confidencialidad o
privilegio por cualquier transmisión errónea. Si usted no es el destinatario
indicado, le rogamos que lo elimine y se lo comunique al remitente. No debe,
directa o indirectamente, usar, revelar, distribuir, imprimir o copiar 
ninguna de las partes de este mensaje. Si siendo destinatario de este 
mensaje no consintiera el uso de correo electrónico, rogamos nos lo 
comunique inmediatamente.
RBCDexia Investor Services E,S.A. y sus filiales no serán responsables de las
opiniones o informaciones incluidas en este mensaje salvo cuando el remitente esté 
autorizado para establecer que dichas opiniones proceden de RBCDexia Investor
Services ,S.A.  y sus filiales.

DISCLAIMER
Addressee/s identified herein. It may contain confidential or legally 
privileged information. No confidentiality privilege is waived or lost
by any mistransmission. If you are not the intended recipient, please
immediately delete it and notify the sender. You must not, directly or
indirectly, disclose, distribute, print, or copy any part of this message.
If you are the addressee of this message and do not consent to the use of
e-mail, please communicate it to us immediately. RBCDexia Investor Services
S.A. and its subsidiaries are not responsible for the opinions or information
included in this message except when the sender is authorised to state them to
 be the views of RBCDexia Investor Services, S.A. and its subsidiaries.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message