servlet中的post方法没有从JSP获取值

时间:2016-10-13 23:24:02

标签: image jsp servlets

我试图在互联网上找到这个错误,但我无法解决我的问题。 我正在尝试将图像保存在项目的本地文件夹中,并使用以下教程来执行此操作:

http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html

编译代码时没有收到任何错误,但是我收到了NullPointerException。这是我的jsp:

 <form action="uploadImagem.do" method="post" enctype="multipart/form-data" 
       name="productForm" id="productForm"><br><br>
<form method="POST" action="upload" enctype="multipart/form-data" >
        File:
        <input type="file" name="file" id="file" /> <br/>
        Destination:
        <input type="text" value="/tcc_SI_M_12 - 12-10-2016_v1/images" name="destination" disabled/>
        </br>
        <input type="submit" value="Upload" name="upload" id="upload" />
    </form>

这是我的servlet:

@WebServlet("/uploadImagem.do")
public class uploadImagem extends HttpServlet {
private static final long serialVersionUID = 1L;
private final static Logger LOGGER = 
        Logger.getLogger(FileUpload.class.getCanonicalName());


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Create path components to save the file
    final String path = request.getParameter("destination");
    System.out.println("Parametros: " + path);
    final Part filePart = request.getPart("file");
    final String fileName = getFileName(filePart);


    OutputStream out = null;
    InputStream filecontent = null;
    final PrintWriter writer = response.getWriter();

    try {
        out = new FileOutputStream(new File(path + File.separator
                + fileName));
        filecontent = filePart.getInputStream();

        int read = 0;
        final byte[] bytes = new byte[1024];

        while ((read = filecontent.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        writer.println("New file " + fileName + " created at " + path);
        LOGGER.log(Level.INFO, "File{0}being uploaded to {1}", 
                new Object[]{fileName, path});
    } catch (FileNotFoundException fne) {
        writer.println("You either did not specify a file to upload or are "
                + "trying to upload a file to a protected or nonexistent "
                + "location.");
        writer.println("<br/> ERROR: " + fne.getMessage());

        LOGGER.log(Level.SEVERE, "Problems during file upload. Error: {0}", 
                new Object[]{fne.getMessage()});
    } finally {
        if (out != null) {
            out.close();
        }
        if (filecontent != null) {
            filecontent.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}

private String getFileName(final Part part) {
    final String partHeader = part.getHeader("content-disposition");
    LOGGER.log(Level.INFO, "Part Header = {0}", partHeader);
    for (String content : part.getHeader("content-disposition").split(";")) {
        if (content.trim().startsWith("filename")) {
            return content.substring(
                    content.indexOf('=') + 1).trim().replace("\"", "");
        }
    }
    return null;

}

请注意,我在上面的教程中做了一些更改。另外,我包含和System.out向我展示变量中的勇气,我收到了 null

你能帮帮我吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

为什么使用两种形式?删除以下代码:

<form method="POST" action="upload" enctype="multipart/form-data" >