FileUpload(Apache Commons)和Java POI IOException:无法读取整个标头

时间:2012-06-17 20:49:34

标签: java apache-poi multipart apache-commons-fileupload

我正在尝试使用Apache Commons Streaming API从excel电子表格中获取信息。我继续收到以下错误:

    SEVERE: org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:908)
    at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
    at org.apache.commons.fileupload.servlet.ServletFileUpload.getItemIterator(ServletFileUpload.java:148)
    at eBooks.controller.ReportServlet.processRequest(ReportServlet.java:54)
    at eBooks.controller.ReportServlet.doPost(ReportServlet.java:178)
    at eBooks.controller.ReportServlet.doGet(ReportServlet.java:170)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

INFO: Reports
INFO: Form field upload_report_btnwith valueUpload Report detected
SEVERE: org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:908)
    at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
    at org.apache.commons.fileupload.servlet.ServletFileUpload.getItemIterator(ServletFileUpload.java:148)
    at eBooks.controller.ReportServlet.processRequest(ReportServlet.java:54)
    at eBooks.controller.ReportServlet.doPost(ReportServlet.java:178)
    at eBooks.controller.ReportServlet.doGet(ReportServlet.java:170)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

SEVERE: org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null
    at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:908)
    at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
    at org.apache.commons.fileupload.servlet.ServletFileUpload.getItemIterator(ServletFileUpload.java:148)
    at eBooks.controller.ReportServlet.processRequest(ReportServlet.java:54)
    at eBooks.controller.ReportServlet.doPost(ReportServlet.java:178)
    at eBooks.controller.ReportServlet.doGet(ReportServlet.java:170)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

这是处理信息的ReportServlet代码:

package eBooks.controller;

/**
 * @author L10
 */

import eBooks.business.Account;
import eBooks.business.Report;
import eBooks.business.User;

import java.io.*;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;



public class ReportServlet extends HttpServlet
{

        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        try 
        {
            // Check that we have a file upload request
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);

            //Create a new file upload Handler
            ServletFileUpload upload = new ServletFileUpload();


            //Parse the request
            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
                        {

                            POIFSFileSystem fs = new POIFSFileSystem(stream);               
                            HSSFWorkbook wb = new HSSFWorkbook(fs);
                            HSSFSheet sheet = wb.getSheetAt(0);
                            System.out.println(wb.getSheetName(0));

                            //  session
                            HttpSession session = request.getSession();

                            //Define define values obatained from the document

                            Report report = new Report();
                            User user = new User();
                            Account account = new Account();                    

                            int reportType = report.getReportType();

                            String emailAddress = user.getEmailAddress();
                            String account_id = account.getAccount_id();
                            String accountName = account.getAccountName();
                            String userRole = user.getUserRole();

                            String reportName = report.getReportName(); // is the spreadsheet Name
                            String reportDate = report.getReportDate(); // is the period on the report

                            double totalUS = report.getMonthTotalUS();
                            double totalEUR = report.getMonthTotalEUR();
                            double grandTotal = report.getMonthGrandTotal();

                            double averagePrice = report.getAverageSalePrice();

                            int totalUnitsSold = report.getTotalUnitsSold();
                            double monthlyAverageUnitsSold = report.getMonthlyAverageUnitsSold();
                            double dailyAverageUnitsSold = report.getDailyAverageUnitsSold();

                            int totalUnitsRefunded = report.getTotalUnitsRefunded();
                            double monthlyAverageUnitsRefunded = report.getMonthlyAverageUnitsRefunded();
                            double dailyAverageUnitsRefuded = report.getDailyAverageUnitsRefuded();

                            double monthlyAverageRoyalties = report.getMonthlyAverageRoyalties();

                            // set values for the user, account & report objects

                            report.setReportType(reportType);

                            user.setEmailAddress(emailAddress);
                            account.setAccount_id(account_id);
                            account.setAccountName(accountName);
                            user.setUserRole(userRole);

                            report.setReportName(reportName); // is the spreadsheet Name
                            report.setReportDate(reportDate); // is the period on the report

                            report.setMonthTotalUS(totalUS);
                            report.setMonthTotalEUR(totalEUR);
                            report.setMonthGrandTotal(grandTotal);

                            report.setAverageSalePrice(averagePrice);

                            report.setTotalUnitsSold(totalUnitsSold);
                            report.setMonthlyAverageUnitsSold(monthlyAverageUnitsSold);
                            report.setDailyAverageUnitsSold(dailyAverageUnitsSold);

                            report.setTotalUnitsRefunded(totalUnitsRefunded);
                            report.setMonthlyAverageUnitsRefunded(monthlyAverageUnitsRefunded);
                            report.setDailyAverageUnitsRefuded(dailyAverageUnitsRefuded);

                            report.setMonthlyAverageRoyalties(monthlyAverageRoyalties);


                        // TODO: Add information to the database

                            // set Session for the user, account & report
                            session.setAttribute("User", user);
                            session.setAttribute("Account", account);
                            session.setAttribute("Report", report);                  

                            Cookie emailCookie = new Cookie("emailCookie", emailAddress);
                            emailCookie.setMaxAge(60*60*24*365*2);
                            emailCookie.setPath("/");
                            response.addCookie(emailCookie);

                            String url = "/WEB-INF/view/Dashboard.jsp";
                            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
                            dispatcher.forward(request, response);


                        }
            }

        }catch (FileUploadException ex) {
            Logger.getLogger(ReportServlet.class.getName()).log(Level.SEVERE, null, ex);
        }


    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


}

我的表单看起来如何:

<form action="ReportServlet" method="post" enctype="multipart/form-data">
                    <input type="file" name="file"/> 
                    <br />
                    <input type="submit" value="Upload Report" name="upload_report_btn" />
                </form>

我正在使用glassfish。有什么想法,我继续得到这个消息?

1 个答案:

答案 0 :(得分:0)

在你的循环中,你似乎假设第一个也是唯一一个到达的多部分对象将是电子表格文件。表格本身也会出现。线索是消息

Unable to read entire header; 13 bytes read; expected 512 bytes

注意,表单的上传值为Upload Report,恰好是13个字节。

如果您使用基于浏览器的调试工具(如Firebug或Fiddler),您可能很容易看到这一点。其中一个工具应该在您的工具集中。