2

在 Tomcat 网络服务器中使用 JSP 时出现以下错误:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 154 in the jsp file: /creaCBdaXLS_passo2.jsp
The method parseRequest(RequestContext) in the type FileUploadBase is not applicable for the arguments (HttpServletRequest)
151: 
152:            ServletFileUpload upload = new ServletFileUpload(factory);
153: 
154:                List items = upload.parseRequest(request);
155:                Iterator itr = items.iterator();
156: 
157:                while (itr.hasNext()) {


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:468)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

我该如何解决?

4

5 回答 5

5

我有同样的问题并通过更换线路解决了

List items = upload.parseRequest(request);

List items = upload.parseRequest(new ServletRequestContext(request));

你应该包括 ServletRequestContext

于 2014-11-19T17:05:31.740 回答
2

看看这个错误行,

parseRequest(RequestContext)类型FileUploadBase中的方法不适用于参数(HttpServletRequest)

您使用了错误的课程。您尝试使用的类方法不HttpServletRequest作为方法参数。

您正在使用的课程>>org.apache.commons.fileupload.FileUploadBase

你应该使用的类>>org.apache.commons.fileupload.servlet.ServletFileUpload

于 2013-05-25T15:05:58.913 回答
0

似乎问题是 Tomcat 7 问题(与我猜使用的类有关),因为代码在以前的版本上运行正常。

解决方法将是这样的(它是一个 Servlet):

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

/**
 * Servlet implementation class Uploader7
 * Please note THE USE OF @MultipartConfig
 */
@MultipartConfig
public class Uploader7 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Uploader7() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Hello, I'm a servlet for Tomcat 7 file upload tests");

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();     

        for (Part part : request.getParts()) {
            String name = part.getName();
            String contentType = part.getContentType();

            if(!contentType.equals("image/x-png")) {
                out.println("Only png format supported!");
                break;
            }

            InputStream is = request.getPart(name).getInputStream();
            File uploadDir = new File("C:\\path\\to\\dir");
            File file = File.createTempFile("img", ".png", uploadDir);

            FileOutputStream fos = new FileOutputStream(file);

            int data = 0;
            while ((data = is.read()) != -1) {
                fos.write(data);
            }

            fos.close();
            out.println("File uploaded.");
        }
    }

}

希望能帮助到你。

于 2013-09-12T13:11:40.113 回答
0

编码:

<%@page import="mia.*"%>
    <%@page import="java.util.StringTokenizer"%>
    <%@page import="java.io.FileReader"%>
    <%@page import="java.io.BufferedReader"%>
    <%@page import= "it.unimib.disco.lintar.cbr.definexml.*" %>
     <%@page import="org.apache.tomcat.util.http.fileupload.FileItem"%>
    <%@page import="java.util.Iterator"%>
    <%@page import="java.util.List"%>
     <%@page import="org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload"%>
     <%@page import="org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory"%>
     <%@page import="org.apache.tomcat.util.http.fileupload.RequestContext"%>
     <%@page import="org.apache.tomcat.util.http.fileupload.FileUploadException"%>

    <%@page import="java.io.File"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Load Structure</title>
            <link href="dropdown-menu.css" media="screen" rel="stylesheet" type="text/css" />
    <link href="div_app.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    @import url("div_bottoni.css");

    body {
        background-color: #F1F1F1;
    }
    </style>
    <link href="div_bottoni_orizz.css" rel="stylesheet" type="text/css" />
    <link href="AgNodo.css" rel="stylesheet" type="text/css" />

            <script language="javascript" src="dom-drag.js"></script>
            <script language="Javascript" type="text/javascript">


            </script>
        </head>

        <body >      

     <ul id="navigation" class="nav-main">
        <li><a href="index.html"> Home </a> </li>
        <li class="list"><a href="#">Case Base</a>
            <ul class="nav-sub">
        <li><a href="NewCaseBase.jsp">New Case Base</a></li>
            <li><a href="LoadCaseBase.jsp?">Load Case Base</a></li>
            <li><a href="creaCBdaXLS_passo1.jsp">Create Case Base from Excel</a></li>
            <li><a href="creaUnsolveddaXLS_passo1.jsp">Create Unsolved Case From XLS</a></li>
        </ul>


        </li>

        <li class="list"><a href="#">Structure</a>
        <ul class="nav-sub">
        <li><a href="NewStruct.jsp">New Structure</a></li>
            <li><a href="LoadStruct.jsp?n=0">Load Structure</a></li>
            <li><a href="creaStructdaXLS_passo1.jsp">Create From XLS</a></li>

        </ul>
      </li>

        <li class="list"><a href="#">Case</a>
        <ul class="nav-sub">
                <li><a href="NewCaseSol.jsp" onclick='CB=window.showModalDialog("SelectCB.jsp","Scegli CaseBase","dialogWidth: 200px; dialogHeight: 350px; center: 1; location: off; resizeable: off; help: off; status:off ;");
                                                        this.href+="?nomeCaseBase="+CB'>New Case Solved</a></li>
                <li><a href="NewCaseUns.jsp" onclick='CB=window.showModalDialog("SelectCB.jsp","Scegli CaseBase","dialogWidth: 200px; dialogHeight: 350px; center: 1; location: off; resizeable: off; help: off; status:off ;");
                                                        this.href+="?nomeCaseBase="+CB'>New Case Unsolved</a></li>
                <li><a href="LoadCaseSol.jsp?n=0">Load Case Solved</a></li>
                <li><a href="LoadCaseUns.jsp?n=0">Load Case Unsolved</a></li>
        </ul>
        </li>

        <li class="list"><a href="">Help</a>
        <ul class="nav-sub">
        <li><a href="javascript:window.open('About.jsp','about','height=100,width=200,resizable,top=5,left=10')">About</a></li>
            <li><a href="#" onclick="window.close();return false">Exit</a></li>


        </ul>
        </li>

    </ul>
        <p>CReP Web Application</p>
    <br/>
    <br/>
    <br/>
    <br/>
    <br/>

                <p/>
    <p>

     <%
                    String nomeCaseBaseNuova ="";
                    String nomeCaseBaseExcel="";
                    String nomeExcel="";
                    boolean server=true;
                    boolean up=true;
                    File savedFile = new File("./webapps/CRePERIE/CaseBases");

                    try {
                        nomeCaseBaseNuova = new String(request.getParameter("nomeCaseBaseNuova"));
                    } catch (Exception e) {
                        out.print("<script language='Javascript' type='text/javascript'>history.back();</script>");
                        up=false;
                        server=false;
                    }

                    try {
                        nomeCaseBaseExcel = new String(request.getParameter("nomeCaseBaseExcel"));
                        nomeExcel = new String(request.getParameter("nomeExcel"));
                    //  cartella =new String(request.getParameter("nomeExcel"));
                    } catch (Exception e) {
                        up=true&&up;
                        server=false;
                    }
                    if(server){
                        up=false;

                        File xlsUns=new File("./webapps/CRePERIE/CaseBases/" + nomeCaseBaseExcel + "/Cases/Solved/" + nomeExcel);
                        File xls=   new File("./webapps/CRePERIE/CaseBases/" + nomeCaseBaseNuova + "/Cases/Solved/" + nomeExcel);
                        if(!nomeCaseBaseExcel.equalsIgnoreCase(nomeCaseBaseNuova)){

                            if(xls.exists()){
                                xls.delete();
                            }
                            if( xlsUns.exists()){
                                savedFile=new CopyXml().Copy(xlsUns, "./webapps/CRePERIE/CaseBases/" + nomeCaseBaseNuova + "/Cases/Solved/" + nomeExcel);
                            }

                        }else{
                            if( xls.exists()){
                                savedFile=xls;
                            }
                        }
                    }


                    if(up){
                        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
                        if (isMultipart) { // inizio salvataggio file
                            try {
                                DiskFileItemFactory factory = new DiskFileItemFactory();

                                ServletFileUpload upload = new ServletFileUpload(factory);

                                List items = upload.parseRequest(request);
                                Iterator itr = items.iterator();

                                while (itr.hasNext()) {
                                    FileItem item = (FileItem) itr.next();
                                    if (!item.isFormField()) {
                                        File fullFile = new File(item.getName());


                                            savedFile = new File("./webapps/CRePERIE/CaseBases/"+nomeCaseBaseNuova+"/Cases/Solved/"+item.getName());
                                            item.write(savedFile);


                                    }
                                }

                            } catch (Exception e) {
                            out.println("file danneggiato riprovare o cambiare file");
                            }

                        } 
                        nomeExcel=savedFile.getName();
                    }

            %>
            <table width="100%" height="500px" border="0" style=" border: thin inset #140000;">
        <tr>
          <td border="0" colspan="2">

            <p>
            </p>
    </div></td>
      </tr>
        <tr>
          <td border="1" colspan="2" bgcolor="#F3F3F3">
              <div id="div" border="0">
    <p/>
            <p style="padding-left:50px;">
            Scegliere un file di Struttura che si vuole caricare
            </p>
            <p/>
            <table style="padding-left:75px; width: 1000px;">
            <tr>
            <td border="0" style="text-align: center; height: 200px; width: 33%; border: thin inset #140000;">
            <p> Crea la sruttura in modo dinamico dal file Excel</p>
            <FORM id="creaDin" name="upload" ENCTYPE='multipart/form-data' method='POST' action="">
                <INPUT TYPE='submit' VALUE='Crea' onclick='var form=document.getElementById("creaDin"); 
                                                            sol=window.showModalDialog("SelectSol.jsp?struct=1&nomeExcel=<%=nomeExcel%>&nomeCaseBase=<%=nomeCaseBaseNuova%>","Scegli soluzione","dialogWidth: 200px; dialogHeight: 350px; center: 1; location: off; resizeable: off; help: off; status:off ;");
                                                            form.action="creaStructdaXLS_passo3.jsp?struct=1&nomeExcel=<%=nomeExcel%>&nomeCaseBase=<%=nomeCaseBaseNuova%>&soluzione="+sol;'/>
            </FORM>
            </td>
            <td border="0" style="text-align: center; height: 200px; width: 33%; border: thin inset #140000;">
            <p> Carica un file dalla macchina utente</p>
            <FORM id="uploadU" name="upload" ENCTYPE='multipart/form-data' method='POST' action="">
                <INPUT TYPE='file' NAME='fileUp'>
                <INPUT TYPE='submit' VALUE='UploadFile' onclick='var form=document.getElementById("uploadU"); 
                                                                 sol=window.showModalDialog("SelectSol.jsp?struct=1&nomeExcel=<%=nomeExcel%>&nomeCaseBase=<%=nomeCaseBaseNuova%>","Scegli soluzione","dialogWidth: 200px; dialogHeight: 350px; center: 1; location: off; resizeable: off; help: off; status:off ;");
                                                                 form.action="creaCBdaXLS_passo3.jsp?struct=2&nomeExcel=<%=nomeExcel%>&nomeCaseBaseNuova=<%=nomeCaseBaseNuova%>&soluzione="+sol;'>
            </FORM>
            </td>
            <td border="0" style="text-align: center; height: 200px; width: 33%; border: thin inset #140000;">
            <p>Carica un file presente sul server</p>
            <FORM id="uploadS" name="upload server" ENCTYPE='multipart/form-data' method='POST' action="">
                <INPUT TYPE='button' VALUE='Seleziona File dal server' onclick=' var form=document.getElementById("uploadS"); 
                                                                                 Str=window.showModalDialog("SelectStruct.jsp","Scegli Struct","dialogWidth: 250px; dialogHeight: 350px; center: 1; location: off; resizeable: off; help: off; status:off ;");
                                                                                 form.action="creaCBdaXLS_passo3.jsp?struct=3&nomeCaseBaseStruct="+Str;'>
                <INPUT TYPE='submit' VALUE='UploadFile' onclick='var form=document.getElementById("uploadS"); 
                                                                sol=window.showModalDialog("SelectSol.jsp?struct=1&nomeExcel=<%=nomeExcel%>&nomeCaseBase=<%=nomeCaseBaseNuova%>","Scegli soluzione","dialogWidth: 200px; dialogHeight: 350px; center: 1; location: off; resizeable: off; help: off; status:off ;"); 
                                                                form.action+="&nomeExcel=<%=nomeExcel%>&nomeCaseBaseNuova=<%=nomeCaseBaseNuova%>&soluzione="+sol;'>
            </td>
            <p/>
            </tr>
            </table>
              <br>


                        </div></td>
                </tr>
               </table>


        </body>
    </html>
于 2013-05-29T07:14:41.547 回答
0

我正在使用的课程是:

<%@page import="mia.*"%>
<%@page import="java.util.StringTokenizer"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import= "it.unimib.disco.lintar.cbr.definexml.*" %>
<%@page import="org.apache.tomcat.util.http.fileupload.FileItem"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.tomcat.util.http.fileupload.RequestContext"%>
<%@page import="org.apache.tomcat.util.http.fileupload.FileUploadException"%>
<%@page import="java.io.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

还是有问题。。

于 2013-05-27T15:00:03.810 回答