You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Stelios Philippou <st...@gmail.com> on 2008/07/10 01:57:34 UTC

Stream ended unexpectedly

I have just found Apache fileUpload, to help me with the function :)
Anyway, after a lot of trial and error to try and make it work with out
it, i was unsuccessful. So i have tried fileUpload.

Everything was ok, i have my working code :

        private void process(HttpServletRequest request, Picture myForm)
        {
                // Validate text.
                boolean isMultipart =
ServletFileUpload.isMultipartContent(request);
                System.out.println(isMultipart);

                ServletFileUpload upload = new ServletFileUpload();
                upload.setFileSizeMax(41943040);    // 5 mb
        // Parse the request
                try {
                    FileItemIterator iter = upload.getItemIterator(request);
                    while (iter.hasNext()) {
                        FileItemStream item = iter.next();
                        String name = item.getFieldName();
                        InputStream stream = item.openStream();

                        if (item.isFormField()) {
                            System.out.println("Form field ");//+ name + "
with value " + Streams.asString(stream) + " detected.");
                        } else {
                            System.out.println("File field " + name + " with
file name " + item.getName() + " detected." +
item.getContentType().length());
                            // Process the input stream
                            //...
                            // String imageId =
request.getParameter("uploadfile");
                            String imageName =
request.getParameter("imageName");
                            if (imageName == null) {
                                System.out.println("Null Picture Name");
                                imageName = name;
                            }
                            System.out.println(item.getContentType());
                            // Open the remote
                            Properties props = new Properties();
                            props.put(Context.INITIAL_CONTEXT_FACTORY,

"org.jnp.interfaces.NamingContextFactory");
                            props.put(Context.PROVIDER_URL,
"localhost:1099");
                            Context ctx = new InitialContext(props);
                            extraRemote exRemote = (extraRemote)
ctx.lookup("Project/extraBean/Remote");


                            int fileSize = request.getContentLength();
                            byte[] buffer = new byte[fileSize];
                            int readBytes = -1;
                            System.out.println(request.getInputStream());

                            InputStream a = request.getInputStream();
                            System.out.println(fileSize  +" " + a);
                            a.read();
                            System.out.println(buffer);
                            /*while ((readBytes = a.read(buffer, 0,
fileSize)) != -1) {
                                a.read(buffer, 0, readBytes);
                            }*/
 but when i try and run the while loop it will run always around 2 - 4
lines and stop. Never more.
If i try and do the below it will read a lot more, if not all the image

        BufferedReader reader = new BufferedReader(new
        InputStreamReader(request.getInputStream()));
                            String line = null;
                            line = reader.readLine();
                            while ((line = reader.readLine()) != null) {
                                System.out.println(line);
                            }

But still will bring me an error in the end
 [STDOUT] -----------------------------7827094435720167761855652711
 [STDOUT] Content-Disposition: form-data; name="imageName"
 [STDOUT] -----------------------------7827094435720167761855652711--
 [STDOUT] Stream ended unexpectedly


Can someone please let me know what i am doing wrong ? IS there
something missing ? SHould i try something else ? Should i change
something ?
Shouldnt this work the way it is ?

Stelios

RE: Stream ended unexpectedly

Posted by Jeenus Johnson <Je...@mphasis.com>.
Tried a similar activity trying to upload a variety of files with Apache commons fileupload ,its working fine for me ,please find attach the source code .One will have to make minor changes in terms of directory structure etc.
 
Regards,
Jeenus.

________________________________

From: Arash Bizhan zadeh [mailto:arashbi@gmail.com]
Sent: Thu 7/10/2008 7:36 AM
To: Commons Users List
Subject: Re: Stream ended unexpectedly



I was trying to implement a SAAJ solution lately and I faced the same error.
The route cause in my case was because file was big and server was dropping
the connection before reading the whole file. Problem solved by increasing
the timeout.

-arash

On Wed, Jul 9, 2008 at 7:57 PM, Stelios Philippou <st...@gmail.com>
wrote:

> I have just found Apache fileUpload, to help me with the function :)
> Anyway, after a lot of trial and error to try and make it work with out
> it, i was unsuccessful. So i have tried fileUpload.
>
> Everything was ok, i have my working code :
>
>        private void process(HttpServletRequest request, Picture myForm)
>        {
>                // Validate text.
>                boolean isMultipart =
> ServletFileUpload.isMultipartContent(request);
>                System.out.println(isMultipart);
>
>                ServletFileUpload upload = new ServletFileUpload();
>                upload.setFileSizeMax(41943040);    // 5 mb
>        // Parse the request
>                try {
>                    FileItemIterator iter = upload.getItemIterator(request);
>                    while (iter.hasNext()) {
>                        FileItemStream item = iter.next();
>                        String name = item.getFieldName();
>                        InputStream stream = item.openStream();
>
>                        if (item.isFormField()) {
>                            System.out.println("Form field ");//+ name + "
> with value " + Streams.asString(stream) + " detected.");
>                        } else {
>                            System.out.println("File field " + name + " with
> file name " + item.getName() + " detected." +
> item.getContentType().length());
>                            // Process the input stream
>                            //...
>                            // String imageId =
> request.getParameter("uploadfile");
>                            String imageName =
> request.getParameter("imageName");
>                            if (imageName == null) {
>                                System.out.println("Null Picture Name");
>                                imageName = name;
>                            }
>                            System.out.println(item.getContentType());
>                            // Open the remote
>                            Properties props = new Properties();
>                            props.put(Context.INITIAL_CONTEXT_FACTORY,
>
> "org.jnp.interfaces.NamingContextFactory");
>                            props.put(Context.PROVIDER_URL,
> "localhost:1099");
>                            Context ctx = new InitialContext(props);
>                            extraRemote exRemote = (extraRemote)
> ctx.lookup("Project/extraBean/Remote");
>
>
>                            int fileSize = request.getContentLength();
>                            byte[] buffer = new byte[fileSize];
>                            int readBytes = -1;
>                            System.out.println(request.getInputStream());
>
>                            InputStream a = request.getInputStream();
>                            System.out.println(fileSize  +" " + a);
>                            a.read();
>                            System.out.println(buffer);
>                            /*while ((readBytes = a.read(buffer, 0,
> fileSize)) != -1) {
>                                a.read(buffer, 0, readBytes);
>                            }*/
>  but when i try and run the while loop it will run always around 2 - 4
> lines and stop. Never more.
> If i try and do the below it will read a lot more, if not all the image
>
>        BufferedReader reader = new BufferedReader(new
>        InputStreamReader(request.getInputStream()));
>                            String line = null;
>                            line = reader.readLine();
>                            while ((line = reader.readLine()) != null) {
>                                System.out.println(line);
>                            }
>
> But still will bring me an error in the end
>  [STDOUT] -----------------------------7827094435720167761855652711
>  [STDOUT] Content-Disposition: form-data; name="imageName"
>  [STDOUT] -----------------------------7827094435720167761855652711--
>  [STDOUT] Stream ended unexpectedly
>
>
> Can someone please let me know what i am doing wrong ? IS there
> something missing ? SHould i try something else ? Should i change
> something ?
> Shouldnt this work the way it is ?
>
> Stelios
>



--
The dirtiest book of all is the expurgated book.
----
Walt Whitman



Re: Stream ended unexpectedly

Posted by Arash Bizhan zadeh <ar...@gmail.com>.
I was trying to implement a SAAJ solution lately and I faced the same error.
The route cause in my case was because file was big and server was dropping
the connection before reading the whole file. Problem solved by increasing
the timeout.

-arash

On Wed, Jul 9, 2008 at 7:57 PM, Stelios Philippou <st...@gmail.com>
wrote:

> I have just found Apache fileUpload, to help me with the function :)
> Anyway, after a lot of trial and error to try and make it work with out
> it, i was unsuccessful. So i have tried fileUpload.
>
> Everything was ok, i have my working code :
>
>        private void process(HttpServletRequest request, Picture myForm)
>        {
>                // Validate text.
>                boolean isMultipart =
> ServletFileUpload.isMultipartContent(request);
>                System.out.println(isMultipart);
>
>                ServletFileUpload upload = new ServletFileUpload();
>                upload.setFileSizeMax(41943040);    // 5 mb
>        // Parse the request
>                try {
>                    FileItemIterator iter = upload.getItemIterator(request);
>                    while (iter.hasNext()) {
>                        FileItemStream item = iter.next();
>                        String name = item.getFieldName();
>                        InputStream stream = item.openStream();
>
>                        if (item.isFormField()) {
>                            System.out.println("Form field ");//+ name + "
> with value " + Streams.asString(stream) + " detected.");
>                        } else {
>                            System.out.println("File field " + name + " with
> file name " + item.getName() + " detected." +
> item.getContentType().length());
>                            // Process the input stream
>                            //...
>                            // String imageId =
> request.getParameter("uploadfile");
>                            String imageName =
> request.getParameter("imageName");
>                            if (imageName == null) {
>                                System.out.println("Null Picture Name");
>                                imageName = name;
>                            }
>                            System.out.println(item.getContentType());
>                            // Open the remote
>                            Properties props = new Properties();
>                            props.put(Context.INITIAL_CONTEXT_FACTORY,
>
> "org.jnp.interfaces.NamingContextFactory");
>                            props.put(Context.PROVIDER_URL,
> "localhost:1099");
>                            Context ctx = new InitialContext(props);
>                            extraRemote exRemote = (extraRemote)
> ctx.lookup("Project/extraBean/Remote");
>
>
>                            int fileSize = request.getContentLength();
>                            byte[] buffer = new byte[fileSize];
>                            int readBytes = -1;
>                            System.out.println(request.getInputStream());
>
>                            InputStream a = request.getInputStream();
>                            System.out.println(fileSize  +" " + a);
>                            a.read();
>                            System.out.println(buffer);
>                            /*while ((readBytes = a.read(buffer, 0,
> fileSize)) != -1) {
>                                a.read(buffer, 0, readBytes);
>                            }*/
>  but when i try and run the while loop it will run always around 2 - 4
> lines and stop. Never more.
> If i try and do the below it will read a lot more, if not all the image
>
>        BufferedReader reader = new BufferedReader(new
>        InputStreamReader(request.getInputStream()));
>                            String line = null;
>                            line = reader.readLine();
>                            while ((line = reader.readLine()) != null) {
>                                System.out.println(line);
>                            }
>
> But still will bring me an error in the end
>  [STDOUT] -----------------------------7827094435720167761855652711
>  [STDOUT] Content-Disposition: form-data; name="imageName"
>  [STDOUT] -----------------------------7827094435720167761855652711--
>  [STDOUT] Stream ended unexpectedly
>
>
> Can someone please let me know what i am doing wrong ? IS there
> something missing ? SHould i try something else ? Should i change
> something ?
> Shouldnt this work the way it is ?
>
> Stelios
>



-- 
The dirtiest book of all is the expurgated book.
----
Walt Whitman