使用servlet上传文件时,getPart()返回null我已经包含了@MultipartConfig,但仍然返回null

时间:2015-04-11 19:32:22

标签: java servlets weblogic12c

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import com.oreilly.servlet.*;
import javax.servlet.http.Part;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.*;
@MultipartConfig
@WebServlet("upload")
public class FileUploadServlet extends HttpServlet
{
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
         Part filePart =req.getPart("fileName");
         String realPath = getServletContext().getRealPath("file");
        MultipartRequest mpr = new MultipartRequest(req,realPath,500*1024*1024);
        InputStream inputStream = null; // input stream of the upload file

        // obtains the upload file part in this multipart request



        if (filePart != null) {
            // prints out some information for debugging
            out.println(filePart.getName());
            out.println(filePart.getSize());
            out.println(filePart.getContentType());

            // obtains input stream of the upload file
            inputStream = filePart.getInputStream();
        }
        out.println("<html><body>");
        //out.println(fileName);
        out.println("File uploaded successfully");
        out.println("</body></html>");

    }
}

和html是

<html>
<head>
<title>form</title>
<body>
<h2>Upload Contribution</h2>
          <br><br>

          <p>Contribution Domain: <input type="text" name="contdomain">
          <br>
          <br>
          Contribution Name: <input type="text" name="contname">
          <br>
          <br>
  <form method="post" action="upload" name="submit" enctype="multipart/form-data">
  <input type="file" name="fileName"><br /><br />
  <input type="submit" name="submit" value="Submit">
</form>
</head>
</body>
</html>

并且web.xml是

<web-app>
<servlet>
<servlet-name>FileUploadServlet</servlet-name>
<servlet-class>FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUploadServlet</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
</web-app>

请帮助我解决这个问题...我搜索的所有地方都要求添加@MultipartConfig,但添加它仍然不会给出结果。

我需要文件名,以便我将文件添加到数据库

1 个答案:

答案 0 :(得分:1)

问题在于Servlet声明,您可以从web.xml文件中删除<servlet></servlet>标记(包含其中的所有内容),并在Servlet类的顶部添加以下正确的注释,因此这里是完整版,适用于我的机器:

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.Part;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.*;
@MultipartConfig

@WebServlet(
        name = "FileUploadServlet",
        urlPatterns = { "/upload"},
        loadOnStartup = 1
)
public class FileUploadServlet extends HttpServlet
{
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        Part filePart =req.getPart("fileName");
        InputStream inputStream = filePart.getInputStream();
    }
}

HTML:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>App</title>
</head>
<body>

<form method="post" action="upload" name="submit" enctype="multipart/form-data">
    <input type="file" name="fileName"><br /><br />
    <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>App</display-name>

</web-app>