问题

如何使用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)

#1 热门回答(1058 赞)

介绍

要浏览并选择要上载的文件,你需要在表单中使用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>

在提交这样的表单之后,二进制多部分表单数据在a different format中的请求正文中可用,而未设置enctype

在Servlet 3.0之前,Servlet API本身并不支持multipart / form-data。它仅支持application / x-www-form-urlencoded的默认表单enctype。使用多部分表单数据时,request.getParameter()和consorts都会返回null。这是众所周知的868363682进入图片的地方。

##不要手动解析它!

理论上,你可以根据ServletRequest#getInputStream()自己解析请求体。然而,这是一项精确而繁琐的工作,需要精确的知识RFC2388。你不应该试图自己做这个或者复制一些在互联网上其他地方找到的本土的无库代码。许多在线消息来源都很难,例如roseindia.net。另见uploading of pdf file。你应该使用数百万用户多年使用(并隐式测试!)的真实库。这样的库已经证明了它的稳健性。

##当你已经使用Servlet 3.0或更高版本时,请使用本机API

如果你至少使用Servlet 3.0(Tomcat 7,Jetty 9,JBoss AS 6,GlassFish 3等),那么你可以使用标准API提供的HttpServletRequest#getPart()来收集各个多部分表单数据项(大多数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()那样的方法。文件“)):

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // ...
    List<Part> fileParts = request.getParts().stream().filter(part -> "file".equals(part.getName())).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时,手动获取提交的文件名

请注意,在Servlet 3.1(Tomcat 8,Jetty 9,WildFly 8,GlassFish 4等)中引入了“Part#getSubmittedFileName()”。如果你还没有使用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时,你的UploadServletdoPost()看起来如何:

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将获得一个空的请求体。另见a.o.ServletFileUpload#parseRequest(request) returns an empty list

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

或者你也可以把它全部包装在一个Filter中,它自动解析它并将这些东西放回到请求的参数map中,这样你就可以继续使用request.getParameter()通常的方式并通过以下方式检索上传的文件request.getAttribute().You can find an example in this blog article

GetFrameter()的GlassFish3错误的解决方法仍然返回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变量,如上面的代码片段所示)正确保存到磁盘或数据库的详细信息:

  • 推荐的方法将上传的文件保存在servlet应用程序中
  • 如何上传图像并将其保存在数据库中?
  • 如何将Part转换为Blob,以便将其存储在MySQL中?

##提供上传的文件

有关将已保存的文件从磁盘或数据库正确提供回客户端的详细信息,请转到以下答案:

  • 使用<h:graphicImage>或<img>标签从webapps / webcontext / deploy文件夹外部加载图像
  • 如何从JSP页面中的数据库中检索和显示图像?
  • 在Java Web应用程序中从应用程序服务器外部提供静态数据的最简单方法
  • 支持HTTP缓存的静态资源servlet的抽象模板

##对表单进行Ajax化

转到以下答案如何使用Ajax(和jQuery)上传。请注意,收集表单数据的servlet代码无需为此更改!只有你的响应方式可能会被改变,但这是微不足道的(即,不是转发到JSP,只是打印一些JSON或XML甚至纯文本,具体取决于负责Ajax调用的脚本所期望的)。

  • 如何使用JSP / Servlet和Ajax将文件上传到服务器?
  • 通过xmlHttpRequest将文件作为multipart发送
  • HTML5文件上传到Java Servlet

希望这一切都有帮助:)


#2 热门回答(22 赞)

如果你碰巧使用Spring MVC,这是如何:(我将离开这里,以防有人发现它有用)。

使用enctype属性设置为“multipart / form-data`”的表单(与BalusC的答案相同)

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

在你的控制器中,将请求参数file映射到MultipartFile类型,如下所示:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void handleUpload(@RequestParam("file") MultipartFile file) throws IOException {
    if (!file.isEmpty()) {
            byte[] bytes = file.getBytes(); // alternatively, file.getInputStream();
            // application logic
    }
}

你可以使用MultipartFilegetOriginalFilename()getSize()来获取文件名和大小。

我用Spring版本4.1.1.RELEASE测试了这个。


#3 热门回答(11 赞)

你需要将common-io.1.4.jar文件包含在lib目录中,或者如果你在任何编辑器(如NetBeans)中工作,那么你需要转到项目属性并添加JAR文件你会完成的。

要获取common.io.jar文件只需google它或只是转到ApacheTomcat网站,在那里你可以选择免费下载此文件。但请记住一件事:如果你是Windows用户,请下载二进制ZIP文件。


原文链接