gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:05:39 26 4
gpt4 key购买 nike

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,但添加它仍然没有给出结果。

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

最佳答案

问题在于 Servlet 声明,您可以删除 <servlet></servlet>从 web.xml 文件(包含里面的所有内容)中添加标签,并在 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>

关于java - getPart() 在使用 servlet 上传文件时返回 null 我已经包含 @MultipartConfig 但它仍然返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29582121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com