commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stelios Philippou" <stevo...@gmail.com>
Subject Stream ended unexpectedly
Date Wed, 09 Jul 2008 23:57:34 GMT
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

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