在 Java Servlet 中上传文件

2022-01-09 00:00:00 file-upload upload java jsp servlets

我有一个 Java 动态 Web 项目,我正在使用 TomCat v7.0.

I have a Java Dynamic Web Project, and I'm using TomCat v7.0.

我是 web 项目的新手,我不太明白如何在我的一个 jsp 页面中上传文件.由于我的项目只是本地的,我想我可以使用一个多部分的形式,人们可以在其中选择文件(这部分很好),然后从我的 Servlet 中检索文件路径.我无法完成这部分,它似乎只给了我文件的名称,而不是它的整个路径.

I am new to web projects and I didn't quite understand how I can upload a file in one of my jsp pages. Since my project is intended to be only local, I thought I could use a multipart form in which the person would choose the file (and this part goes fine) and later retreive the file path from my Servlet. I can't complete this part though, it appears to only give me the name of the file, not its entire path.

谁能指出我正确的方向?我已经阅读了几篇关于 Apache 文件上传和从多部分表单中检索信息的文章,但似乎没有任何帮助.

Can anyone point me to the right direction? I've read several posts about Apache File Upload and retreiving information from the multipart form but nothing seems to help me.

如何从表单中获取文件路径,或者如何获取上传的文件以在我的 Java 类中使用?

How can I get the file path from a form or alternatively how can I get the uploaded file to use in my Java classes?

提前致谢.

.jsp:

<form method="post" action="upload" enctype="multipart/form-data">
<input type="file" name="filePath" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"></input>
<input type="submit" value="Enviar"></input>
</form>

Java Servlet:

Java Servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    PrintWriter out = response.getWriter();
    out.println("<html><body>");

    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();

                out.println("<h1>"+fieldname+" / "+fieldvalue+"</h1>");
            }
            else
            {
                // Process form file field (input type="file").
                String fieldname = item.getFieldName();
                String filename = item.getName();
                InputStream filecontent = item.getInputStream();
                String s = filecontent.toString();
                out.println("<h1>"+s+" / "+filename+"</h1>");
                item.write(null);
            }
        }
    }
    catch (FileUploadException e)
    {
        throw new ServletException("Cannot parse multipart request.", e);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    out.println("</body></html>");
}

推荐答案

不提供文件路径是浏览器的一项安全功能.

Not providing the file path is a security feature of the browser.

您的代码中有可用的文件内容(InputStream filecontent),因此您可以使用它或使用 FileItem 上的一种便捷方法,例如

You have the file contents available in your code (InputStream filecontent) so you could use that or use one of the convenience methods on FileItem, e.g.

item.write(new File("/path/to/myfile.txt"));

相关文章