Warm tip: This article is reproduced from serverfault.com, please click

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

(java - How to upload files to server using JSP/Servlet?)

发布于 2010-03-11 04:07:32

如何使用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)
Questioner
Thang Pham
Viewed
0
BalusC 2019-12-19 00:11:40

介绍

要浏览并选择要上传的文件,你需要<input type="file">在表单中有一个HTML字段。HTML规范中所述,你必须使用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

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

不要手动解析它!

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

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

如果你至少使用Servlet 3.0(Tomcat 7,Jetty 9,JBoss AS 6,GlassFish 3等),则可以使用提供的标准APIHttpServletRequest#getPart()来收集各个部分的表单数据项(大多数Servlet 3.0实现实际上都使用Apache) Commons FileUpload的内容在这里!)。此外,可以通过getParameter()常规方式使用普通表单字段

首先用注释你的servlet,@MultipartConfig以使其识别并支持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解析多部分表单数据请求。它具有出色的《用户指南》和《常见问题解答》(请仔细阅读两者)。也有O'Reilly(“ cos ”)MultipartRequest,但是它有一些(较小的)错误,并且多年来一直没有进行积极维护。我不建议使用它。Apache Commons FileUpload仍在积极维护中,目前非常成熟。

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

你最初的尝试很可能失败,因为你忘记了公共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)返回一个空列表

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

或者,你也可以将所有内容包装在一个Filter可以自动解析的内容中,然后将其放回请求的参数图中,以便你可以继续使用request.getParameter()通常的方法并通过检索上载的文件request.getAttribute()你可以在此博客文章中找到示例

GlassFish3错误getParameter()仍然存在的解决方法null

请注意,低于3.1.2的Glassfish版本存在一个错误,其中该错误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()!)

前往以下答案,详细了解如何正确地将获得的InputStreamfileContent上面代码片段中显示变量)保存到磁盘或数据库中:

提供上传的文件

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

拼凑形式

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


希望这对你有所帮助:)