hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marki <puto.c...@gmail.com>
Subject Re: Sending serialized object and stream
Date Thu, 22 May 2008 05:47:41 GMT
Ok, then i share my server code as well, where i handle the multipart form:

        String PDFFolder = "c:\\folder\\";
        int  maxSize = 300000;




        String extensionRealFichero = null;
        String contentType = null;
        int idImagen = 0;

        DiskFileUpload fu = new DiskFileUpload();
        // maximum size before a FileUploadException will be thrown
        fu.setSizeMax(maxSize);
        // maximum size that will be stored in memory
        fu.setSizeThreshold(4096);
        // the location for saving data that is larger than
getSizeThreshold()
        fu.setRepositoryPath(directorioPDF);

try {
            List fileItems  = fu.parseRequest(req);
            // assume we know there are two files. The first file is a small
            // text file, the second is unknown and is written to a file on
            // the server

            Iterator i = fileItems.iterator();

            while (i.hasNext()) {
                actual = (FileItem) i.next();
                if (actual.isFormField()) {
//NORMAL ARGUMUENT RECIEVED
                    System.out.println("ARG-->" + actual.getFieldName() +
"="+ actual.getString());
                    paramsForm.setProperty(actual.getFieldName(),
actual.getString());
                } else {
//FILE RECIEVED
                    System.out.println("Nombre Fichero recibido:" +
actual.getName()+ ".");
                    paramsForm.setProperty("nombreReal", actual.getName());
                    ficheroUpload = actual;

                }
            }

            extensionRealFichero =
(actual.getName().substring(actual.getName().length()-3,actual.getName().length())).toUpperCase();

            contentType = ficheroUpload.getContentType();

            System.out.println("contentType:"+contentType);
            System.out.println("extension:"+extensionRealFichero);
            System.out.println("Ruta Final del
Fichero:"+directorioPDF+actual.getName());

            ficheroUpload.write(new File(directorioPDF+actual.getName()));


hope it helps

On Thu, May 22, 2008 at 7:34 AM, <Natarajan_Valli@emc.com> wrote:

> Thank you for sharing the code. However, the problem is in server side
> where the request is received. The httpclient writes/sends header, part
> boundary etc to server. The received bytes require parsing, I believe.
> I'm looking at fileupload code. In my scenario, though I would use
> FilePart object, it would not be real file; it would be bytes from
> serialized object.
>
> Thanks
> Valliappan
>
> -----Original Message-----
> From: Marki [mailto:puto.chui@gmail.com]
> Sent: Wednesday, May 21, 2008 10:28 PM
> To: HttpClient User Discussion
> Subject: Re: Sending serialized object and stream
>
> If it is useful from you, i succeded sending files through multiparts
> forms
> in this way:
>
>                    File aux = new File ("c:\\anyFILE");
>                    HttpClient httpclient = new HttpClient();
>                    PostMethod filePost = new PostMethod(urlCompleta);
>                    filePost.setDoAuthentication(true);
>                    Part[] parts = {new FilePart(aux.getName(), aux)};
>                    filePost.setRequestEntity(new
> MultipartRequestEntity(parts, filePost.getParams()));
>
> httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(5
> 000);
>                    int status = httpclient.executeMethod(filePost);
>                      if (status == HttpStatus.SC_OK) {
>                          logger.info("sending OK...");
>
>                    } else {
>                         logger.info("sending ERROR...");
>                     }
>
> On Wed, May 21, 2008 at 10:45 PM, <Natarajan_Valli@emc.com> wrote:
>
> > When MultiPartRequestEntity is used to send serialized object, the
> > server (servlet) throws error "unexpected jdwp error: 35" while
> reading
> > the object. I've attached code snippet from client and server
> >
> > Client code:
> >
> >            ByteArrayOutputStream requestStream = new
> > ByteArrayOutputStream();
> >            ObjectOutputStream objectOutput = new
> > ObjectOutputStream(requestStream);
> >            objectOutput.writeObject(request);
> >            objectOutput.close();
> >            String enc = "ISO-8859-1";
> >
> >            MultipartRequestEntity entity = new MultipartRequestEntity(
> >                new Part[] {
> >                    new FilePart(
> >                        "param1",
> >                        new ByteArrayPartSource("filename.txt",
> > requestStream.toByteArray()),
> >                         "application/octet-stream",
> >                         enc) },
> >                 post.getParams());
> >            post.setRequestEntity(entity);
> >
> >            NameValuePair[] pair = new NameValuePair[1];
> >            pair[0] = new NameValuePair(IRequest.HTTP_REQUEST_HANDLER,
> > handler);
> >            post.setQueryString(pair);
> >            m_client.executeMethod(post);
> >
> > Server code:
> >
> >    public void doFilter(ServletRequest request, ServletResponse
> > response, FilterChain chain)
> >            throws IOException, ServletException {
> >        if (m_abortErrorMessage != null) {
> >            ((HttpServletResponse) response).sendError(500,
> > m_abortErrorMessage);
> >            return;
> >        }
> >
> >        if (request instanceof HttpServletRequest) {
> >            HttpServletRequest req = (HttpServletRequest) request;
> >            String reqHandlerName =
> > req.getParameter(IRequest.HTTP_REQUEST_HANDLER);
> >            if (reqHandlerName != null) {
> >                    InputStream reqInputStream = req.getInputStream();
> >
> >                    ObjectInputStream reqStream = new
> > ObjectInputStream(reqInputStream); //this line throws the error
> >                    IRequest reqObject = null;
> >                    try {
> >                        reqObject = (IRequest) reqStream.readObject();
> >                    }
> >                    catch (ClassNotFoundException e) {
> >                        ((HttpServletResponse)
> response).sendError(400);
> >
> >                    }
> >                }
> >        }
> >
> >
> > The same server code works fine when InputStreamRequestEntity is used,
> > here is client code
> >
> >            ByteArrayOutputStream requestStream = new
> > ByteArrayOutputStream();
> >            ObjectOutputStream objectOutput = new
> > ObjectOutputStream(requestStream);
> >            objectOutput.writeObject(request);
> >            objectOutput.close();
> >
> >            InputStream requestContent = new
> > ByteArrayInputStream(requestStream.toByteArray());
> >            if (requestContent != null) {
> >                post.setRequestEntity(new
> > InputStreamRequestEntity(requestContent));
> >            }
> >
> >            NameValuePair[] pair = new NameValuePair[1];
> >            pair[0] = new NameValuePair(IRequest.HTTP_REQUEST_HANDLER,
> > handler);
> >            post.setQueryString(pair);
> >            m_client.executeMethod(post);
> >
> > We need to use multipart request entity.
> >
> > I appreciate help to solve this problem.
> >
> > Regards
> > Valli
> >
> > -----Original Message-----
> > From: Oleg Kalnichevski [mailto:olegk@apache.org]
> > Sent: Sunday, May 18, 2008 8:09 AM
> > To: HttpClient User Discussion
> > Subject: Re: Sending serialized object and stream
> >
> > On Fri, 2008-05-16 at 16:05 -0400, Natarajan_Valli@emc.com wrote:
> > > Hi All,
> > >
> > > I've requirement to send both serialized java object and stream
> > object.
> > > Here is the code that sends serialized object
> > >
> > >             ByteArrayOutputStream requestStream = new
> > > ByteArrayOutputStream();
> > >             ObjectOutputStream objectOutput = new
> > > ObjectOutputStream(requestStream);
> > >             objectOutput.writeObject(request);
> > >             objectOutput.close();
> > >
> > >             InputStream requestContent = new
> > > ByteArrayInputStream(requestStream.toByteArray());
> > >             if (requestContent != null) {
> > >                 post.setRequestEntity(new
> > > InputStreamRequestEntity(requestContent));
> > >             }
> > >
> > > We need to optionally send stream object as well. I believe we need
> to
> > > use multipart request entity. The HC currently supports FilePart and
> > > StringPart. Do we need to write another type of Part like StreamPart
> > to
> > > do so?
> > >
> >
> > Yes, you do
> >
> > Oleg
> >
> > > Help is appreciated.
> > >
> > > Thanks in advance
> > >
> > > Valli
> > >
> > >
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
> >
> >
>
>
> --
> Regards
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>


-- 
Regards

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message