【问题标题】:java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream...Getting this exception while trying to upload a filejava.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream ...尝试上传文件时出现此异常
【发布时间】:2021-08-20 20:25:26
【问题描述】:

这是我的 Upload.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>  

        <form action="UploadServlet" enctype="multipart/form-data" method="post">

        Select File : <input type="file" name="video"><br>

        <input type="submit" name="upload" value="Upload">

        </form>

</body>

</html>

这是我的 UploadServlet.java

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

            // TODO Auto-generated method stub

            boolean isMultiPart = ServletFileUpload.isMultipartContent(request);

            List<FileItem> items = null;

            if(isMultiPart)

            {

                DiskFileItemFactory factory = new DiskFileItemFactory();

                ServletFileUpload upload = new ServletFileUpload(factory);



                try {

                     items = upload.parseRequest(request);

                } catch (FileUploadException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

                     Iterator<FileItem> it = items.iterator();



                    while(it.hasNext())

                    {

                        FileItem item = (FileItem) it.next();





                        if(!(item.isFormField()))

                        {

                            String fieldname = item.getFieldName();

                            String filename = item.getName();

                            long fileSize = item.getSize();



                            System.out.println("Field Name is :"+fieldname);

                            System.out.println("File Name is :"+filename);

                            System.out.println("File Size is :"+fileSize);



                            String filepath = "C:"+File.separator;



                            File video = new File(filepath);

                            FileOutputStream foutput = new FileOutputStream(video);

                            DeferredFileOutputStream fout = new DeferredFileOutputStream(1, video);

                            fout.writeTo(foutput);

                            try {

                                item.write(video);

                            } catch (Exception e) {

                                // TODO Auto-generated catch block

                                e.printStackTrace();

                            }

                        }

                    }



            }

        }

当我尝试上传文件时,它在“upload.parseRequest(request)”方法中抛出“java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream”。我可以使用相同的吗上传视频的代码。如果不是,请告诉我上传视频应该采用的过程。

下面是我的堆栈跟踪:

java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream

    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)

    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)

    at org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:199)

    at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:361)

    at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)

    at com.videoplayer.controller.UploadServlet.doPost(UploadServlet.java:70)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)

    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)

    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)

    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)

    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)

    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)

    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)

    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)

    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)

    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)

    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)

    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)

    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

    at java.lang.Thread.run(Unknown Source)

【问题讨论】:

  • 我知道很难理解“ClassNotFoundException”是什么意思,谷歌也找不到任何东西。所以我告诉你该怎么做:将 apache commons.io 包含到你的 tomcat 中:commons.apache.org/proper/commons-io
  • 感谢 Tom 的回复 :)... 我试过了,但我仍然面临同样的问题

标签: java tomcat file-upload


【解决方案1】:

你可以使用下一个代码

try {
        Part filePart = request.getPart("poster");//name of your parameter
        String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
        String webInfPath = request.getServletContext().getRealPath("images");
        File file = new File(webInfPath,fileName);
        InputStream fileContent = filePart.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int len = fileContent.read(buffer);
        while (len != -1) {
            fileOutputStream.write(buffer, 0, len);
            len = fileContent.read(buffer);
        }
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                System.out.println("can't close file");
            }
        }
} catch (ServletException | IOException e) {
        throw new ServiceException("error of upload");
}

【讨论】:

    【解决方案2】:

    尝试在 pom.xml 中添加下一个依赖项

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 2011-09-17
      • 2016-01-23
      • 1970-01-01
      • 2011-02-02
      • 2013-05-30
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多