零空指针异常

时间:2014-09-05 16:09:23

标签: java jsp

我正在尝试上传文件但是我一路上遇到了NullPointerException。

我得到的错误是

SEVERE:   java.lang.NullPointerException at servlet.UploadServlet.doPost(UploadServlet.java:36)

在这一行:

InputStream is = part.getInputStream();

这是servlet和jsp页面的代码。

UploadServlet

    @MultipartConfig(fileSizeThreshold = 1024 * 1024 * 10, maxFileSize = 1024 * 1024 * 50, maxRequestSize = 1024 * 1024 * 50)

public class UploadServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            Part part = request.getPart("file");
            InputStream is = part.getInputStream();

            String filename = getFileName(part);
            String relativeWebPath = "/WEB-INF/uploads";
            String absoluteFilePath = getServletContext().getRealPath(relativeWebPath);
            File uploadedFile = new File(absoluteFilePath, filename);

            FileOutputStream os = new FileOutputStream(uploadedFile);

            int i = is.read();
            while (i != -1) {
                os.write(i);
                i = is.read();
            }
            os.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            out.close();
        }
    }

    private String getFileName(Part part) {
        for (String cd : part.getHeader("content-disposition").split(";")) {
            if (cd.trim().startsWith("filename")) {
                String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
                return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1);
            }
        }
        return null;
    }
}

upload.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <title>MobMel::Upload</title>
    </head>
    <body>
        <form action="UploadServlet" method="post" enctype="multipart/form-data">
            <table class="upload">
                <tr>
                    <td>File</td> 
                    <td><input type="file" name="file"></td>
                </tr>
                <tr>
                    <td></td> 
                    <td><input type="submit" value="Upload"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
    <servlet>
        <servlet-name>RegisterServlet</servlet-name>
        <servlet-class>servlet.RegisterServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>ProfileServlet</servlet-name>
        <servlet-class>servlet.ProfileServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>UploadServlet</servlet-name>
        <servlet-class>servlet.UploadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RegisterServlet</servlet-name>
        <url-pattern>/RegisterServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/LoginServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ProfileServlet</servlet-name>
        <url-pattern>/ProfileServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>UploadServlet</servlet-name>
        <url-pattern>/UploadServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

我希望你们能帮助我解决这个问题。

由于

1 个答案:

答案 0 :(得分:0)

我认为这可能会有所帮助: https://coderanch.com/t/618813/Null-Pointer-Exception-request-getPart 简而言之,您需要添加Multipartsconfig批注。至少在我的情况下,这个人已经走了。