Skip to main content

Glass fish V2 Appache common Fileupload Problem

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
Sanila
Offline
Joined: 2012-06-16

I am using apache commom file upload to upload files.
It is possible to upload files. But sometimes it is not working even for small files.
It show the below exception

javax.servlet.ServletException
at app.ui.common.UploadFilter.doFilter(UploadFilter.java:115)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:198)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:197)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:171)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:198)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:288)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:271)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:202)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:206)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:150)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:272)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:637)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:568)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:813)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
at com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTask.doTask(PortUnificationPipeline.java:380)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
Caused by: org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:359)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at app.ui.common.UploadFilter.doFilter(UploadFilter.java:88)
... 32 more
Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:964)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:887)
at java.io.InputStream.read(InputStream.java:89)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:94)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:64)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:354)
... 34 more

Can anyone please help?
The issue is occuring in production. In development everything is getting fine.
My web.xml is

<filter>
        <filter-name>FinaMatrixFilter</filter-name>
        <display-name>FinaMatrix Filter</display-name>
        <description>This is FinaMatrix filter</description>
        <filter-class>app.ui.common.UploadFilter</filter-class>
        <init-param>
            <param-name>sizeThreshold</param-name>
            <param-value>1024</param-value>
        </init-param>
    </filter>

And the java filter class method to read the file is

DiskFileItemFactory factory = new DiskFileItemFactory();
        if (sizeThreshold >= 0) {
            factory.setSizeThreshold(sizeThreshold);
        }
        if (repositoryPath != null) {
            factory.setRepository(new File(repositoryPath));
        }
        ServletFileUpload upload = new ServletFileUpload(factory);

        try {
            List<FileItem> items = (List<FileItem>) upload.parseRequest(httpRequest);
            final Map<String, String[]> map = new HashMap<String, String[]>();
            for (FileItem item : items) {
                String str = item.getString();
                if (item.isFormField()) {
                    if (map.get(item.getFieldName()) == null){
                        map.put(item.getFieldName(), new String[]{str});
                    } else {
                        String[] itemValues = map.get(item.getFieldName());
                        StringBuilder multipleValues = new StringBuilder();
                        for (String tempItem : itemValues) {
                            multipleValues.append(tempItem).append(",");
                        }
                        multipleValues.append(str);
                        if (multipleValues.length() > 0) {
                            map.put(item.getFieldName(), multipleValues.toString().split(","));
                        } else {
                            map.put(item.getFieldName(), new String[]{str});
                        }
                    }
                } else {
                    httpRequest.setAttribute(item.getFieldName(), item);
                }
            }

            chain.doFilter(new HttpServletRequestWrapperImpl(httpRequest,map), response);
        } catch (FileUploadException fuex) {
            ServletException servletEx = new ServletException();
            servletEx.initCause(fuex);
            throw servletEx;
        } catch (Exception ex) {
            ex.printStackTrace();
        }