未为类型Part定义方法getSubmittedFileName

时间:2019-02-08 17:39:59

标签: java eclipse

我一直在搜索其他答案,但是任何答案对我来说都是正确的!

实际上我在Eclipse上已安装:

  • Tomcat 8.5.37
  • Servlet-Api版本8.5.11

当我尝试在下一个代码上使用方法getSubmittedFileName()时:

    package com.test.servlet;

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.nio.file.Paths;

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

    /**
    * Servlet implementation class MyServlet
    */
    @WebServlet("/MyServlet")
    @MultipartConfig
    public class MyServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;

        /**
        * @see HttpServlet#HttpServlet()
        */
        public MyServlet() {
            super();
            // TODO Auto-generated constructor stub
        }

        /**
        * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
        */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String usuario = request.getParameter("usuario");
            String password = request.getParameter("password");

            response.getWriter().append("Usuario:").append(usuario).append(" Password:").append(password).append(" Desde doGet");
        }

        /**
        * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
        */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String msg = request.getParameter("msg");
            Part file = request.getPart("myFile");
            Part file2 = request.getPart("myFile2");
            InputStream fileComoStream = file.getInputStream();
            InputStream fileComoStream2 = file2.getInputStream();
            String pathfile =  Paths.get(file.getSubmittedFileName()).getFileName().toString();

            System.out.println("file 1:" + pathfile);
            System.out.println("file 2:" + pathfile2);
            System.out.println("msg:" + msg);
            System.out.println("file 1:" + fileComoStream);
            System.out.println("file 2:" + fileComoStream2);

            OutputStream os1 = new FileOutputStream("C:\\tmp\\" + pathfile);

            byte[] buffer = new byte[1024];
            int bytesRead;

            while((bytesRead = fileComoStream.read(buffer)) !=-1){
                os1.write(buffer, 0, bytesRead);
            }
            fileComoStream.close();

            os1.flush();
            os1.close();

            OutputStream os2 = new FileOutputStream("C:\\tmp\\" + pathfile2);

            byte[] buffer2 = new byte[1024];
            int bytesRead2;

            while((bytesRead2 = fileComoStream2.read(buffer2)) !=-1){
                os2.write(buffer2, 0, bytesRead2);
            }
            fileComoStream2.close();

            os2.flush();
            os2.close();

    //      String usuario = request.getParameter("usuario");
    //      String password = request.getParameter("password");
    //      
    //      response.getWriter().append("Usuario:").append(usuario).append(" Password:").append(password).append(" Desde doPost");
        }

    }

我得到一个错误(类型Part的getSubmittedFileName方法未定义)。

我已经阅读了文档,并且我已经满足所有要求。

有人可以帮我解决这个问题吗?

0 个答案:

没有答案