java - 如何使用JSP/Servlet将文件上传到服务器?

标签 java jsp jakarta-ee servlets file-upload

如何使用JSP / Servlet将文件上传到服务器?我尝试了这个:

<form action="upload" method="post">
    <input type="text" name="description" />
    <input type="file" name="file" />
    <input type="submit" />
</form>


但是,我只得到文件名,而不得到文件内容。当我将enctype="multipart/form-data"添加到<form>时,request.getParameter()返回null

在研究过程中,我偶然发现了Apache Common FileUpload。我尝试了这个:

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request); // This line is where it died.


不幸的是,该servlet抛出了一个异常,没有明确的消息和原因。这是堆栈跟踪:

SEVERE: Servlet.service() for servlet UploadServlet threw exception
javax.servlet.ServletException: Servlet execution threw an exception
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:637)

最佳答案

介绍

要浏览并选择要上传的文件,您需要在表单中有一个HTML <input type="file">字段。如HTML specification中所述,您必须使用POST方法,并且表单的enctype属性必须设置为"multipart/form-data"

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="text" name="description" />
    <input type="file" name="file" />
    <input type="submit" />
</form>


提交此类表单后,与未设置enctype相比,二进制多部分表单数据在请求正文中的a different format中可用。

在Servlet 3.0之前,Servlet API本身不支持multipart/form-data。它仅支持默认形式的enctype application/x-www-form-urlencoded。使用多部分表单数据时,request.getParameter()和配偶都将返回null。这就是众所周知的Apache Commons FileUpload出现在其中的地方。

不要手动解析它!

理论上,您可以根据ServletRequest#getInputStream()自己解析请求正文。但是,这是一项精确而乏味的工作,需要对RFC2388的确切了解。您不应尝试自己执行此操作,也不要复制粘贴Internet上其他地方的一些本地编写的无库代码。许多在线资源在此方面都失败了,例如roseindia.net。另请参见uploading of pdf file。您应该使用一个真正的库,该库已被数百万用户使用多年(并进行了隐式测试!)。这样的库已经证明了其健壮性。

如果您已经在Servlet 3.0或更高版本上,请使用本机API

如果您至少使用Servlet 3.0(Tomcat 7,Jetty 9,JBoss AS 6,GlassFish 3等),那么您可以使用HttpServletRequest#getPart()提供的标准API来收集各个部分的表单数据项(大多数Servlet 3.0实现)实际上可以使用Apache Commons FileUpload来实现此目的!)。另外,通常使用getParameter()可以使用普通表单字段。

首先用@MultipartConfig注释servlet,以使其识别并支持multipart/form-data请求,从而使getPart()起作用:

@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {
    // ...
}


然后,按以下方式实现其doPost()

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String description = request.getParameter("description"); // Retrieves <input type="text" name="description">
    Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
    String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
    InputStream fileContent = filePart.getInputStream();
    // ... (do your job here)
}


注意Path#getFileName()。这是有关获取文件名的MSIE修复程序。此浏览器错误地沿名称发送了完整的文件路径,而不仅仅是文件名。

如果您有用于上传多文件的<input type="file" name="file" multiple="true" />,请按以下方式收集它们(不幸的是,没有request.getParts("file")这样的方法):

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // ...
    List<Part> fileParts = request.getParts().stream().filter(part -> "file".equals(part.getName()) && part.getSize() > 0).collect(Collectors.toList()); // Retrieves <input type="file" name="file" multiple="true">

    for (Part filePart : fileParts) {
        String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
        InputStream fileContent = filePart.getInputStream();
        // ... (do your job here)
    }
}


当您尚未使用Servlet 3.1时,请手动获取已提交的文件名

请注意,Part#getSubmittedFileName()是在Servlet 3.1中引入的(Tomcat 8,Jetty 9,WildFly 8,GlassFish 4等)。如果尚未使用Servlet 3.1,则需要其他实用程序方法来获取提交的文件名。

private static String getSubmittedFileName(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); // MSIE fix.
        }
    }
    return null;
}




String fileName = getSubmittedFileName(filePart);


请注意有关获取文件名的MSIE修复程序。此浏览器错误地沿名称发送了完整的文件路径,而不仅仅是文件名。

当您尚未使用Servlet 3.0时,请使用Apache Commons FileUpload

如果您还没有使用Servlet 3.0(不是时候升级了吗?),通常的做法是利用Apache Commons FileUpload解析多部分表单数据请求。它具有出色的User GuideFAQ(请仔细阅读两者)。还有O'Reilly(“ cos”)MultipartRequest,但是它有一些(较小的)错误,并且多年来一直没有得到积极维护。我不建议使用它。 Apache Commons FileUpload仍在积极维护中,目前非常成熟。

为了使用Apache Commons FileUpload,您需要在Webapp的/WEB-INF/lib中至少包含以下文件:


commons-fileupload.jar
commons-io.jar


您最初的尝试很可能失败,因为您忘记了公共IO。

这是一个启动示例,使用Apache Commons FileUpload时doPost()UploadServlet看起来像:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        for (FileItem item : items) {
            if (item.isFormField()) {
                // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                String fieldName = item.getFieldName();
                String fieldValue = item.getString();
                // ... (do your job here)
            } else {
                // Process form file field (input type="file").
                String fieldName = item.getFieldName();
                String fileName = FilenameUtils.getName(item.getName());
                InputStream fileContent = item.getInputStream();
                // ... (do your job here)
            }
        }
    } catch (FileUploadException e) {
        throw new ServletException("Cannot parse multipart request.", e);
    }

    // ...
}


切勿事先在同一请求上调用getParameter()getParameterMap()getParameterValues()getInputStream()getReader()等,这一点非常重要。否则,Servlet容器将读取并解析请求主体,因此Apache Commons FileUpload将获得一个空的请求主体。另请参阅ServletFileUpload#parseRequest(request) returns an empty list

注意FilenameUtils#getName()。这是有关获取文件名的MSIE修复程序。此浏览器错误地沿名称发送了完整的文件路径,而不仅仅是文件名。

或者,您也可以将所有内容包装在Filter中,然后自动对其进行解析,然后将其放回请求的参数图中,以便您可以继续使用request.getParameter()的常规方法并通过request.getAttribute()检索上传的文件。 You can find an example in this blog article

getParameter()的GlassFish3 bug仍返回null的变通办法

请注意,低于3.1.2的Glassfish版本具有a bug,其中getParameter()仍返回null。如果您针对这样的容器且无法升级,则需要借助此实用程序方法从getPart()中提取值:

private static String getValue(Part part) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(part.getInputStream(), "UTF-8"));
    StringBuilder value = new StringBuilder();
    char[] buffer = new char[1024];
    for (int length = 0; (length = reader.read(buffer)) > 0;) {
        value.append(buffer, 0, length);
    }
    return value.toString();
}




String description = getValue(request.getPart("description")); // Retrieves <input type="text" name="description">


保存上传的文件(不要使用getRealPath()part.write()!)

有关将获取的InputStream(如上面代码片段中所示的fileContent变量)正确保存到磁盘或数据库的详细信息,请转到以下答案:


Recommended way to save uploaded files in a servlet application
How to upload an image and save it in database?
How to convert Part to Blob, so I can store it in MySQL?


提供上传的文件

前往以下答案,详细了解如何将已保存的文件从磁盘或数据库正确地提供给客户端:


Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag
How to retrieve and display images from a database in a JSP page?
Simplest way to serve static data from outside the application server in a Java web application
Abstract template for static resource servlet supporting HTTP caching


合并表格

前往以下答案,了解如何使用Ajax(和jQuery)上传。请注意,不需要为此更改用于收集表单数据的servlet代码!仅可以改变您的响应方式,但这相当简单(即,不转发到JSP,仅打印一些JSON或XML甚至纯文本,具体取决于负责Ajax调用的脚本是什么)。


How to upload files to server using JSP/Servlet and Ajax?
Send a file as multipart through xmlHttpRequest
HTML5 File Upload to Java Servlet




希望这对您有所帮助:)

关于java - 如何使用JSP/Servlet将文件上传到服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31423787/

相关文章:

java - 如何覆盖保存在上下文目录中的文件

jakarta-ee - 如何使用 statx 解析器识别空 xml 标记

java - 使用复选框自动选择/突出显示组合框中的项目

java - 如何使用 selenium webdriver 计算网页上可用的图像数量?

java - struts 中的两个按钮相互内联

jquery - 如何从 HTML 中获取 ul 结构

java - 读取excel文件2010 apache poi忽略空单元格

java - 服务器端的响应式 Web 模式

java - 迭代时从自身中删除一个集合

Java API 文档