上传word文档

发布于 2024-10-25 20:16:25 字数 214 浏览 2 评论 0原文

我需要从用户上传的word文档中提取文本。我得到了代码来从位于我的文档中提取单词米/克。但我的要求是允许用户使用上传按钮上传自己的文档阅读该文档(我不需要保存该文档)。你能建议我该怎么做吗?我需要知道单击上传按钮后需要发生什么。

I need to extract text from the word document uploaded by the user. I got the code to extract words from a document located on my m/c. But my requirement is to allow users to upload their own document using the upload button & read that document(I dont need to save that document). Can you please suggest me how to go about this? I need to know what all need to happen after clicking the upload button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的心境我的脸 2024-11-01 20:16:25

当用户上传文件时,获取关联的 InputStream 并将其存储到变量中,例如 inputStream。然后只需使用示例代码,并将此行:

fs = new POIFSFileSystem(new FileInputStream(filesname));

...替换为:

fs = new POIFSFileSystem(inputStream); 

应该足够简单,假设您已经有一个 Servlet 来处理上传。

编辑:

以下是 servlet 如何工作的基础知识,假设您使用 commons-fileupload 来解析上传:

public class UploadServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // Create a factory for disk-based file items
        FileItemFactory factory = new DiskFileItemFactory();

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);

        // Parse the request
        List<FileItem> items = upload.parseRequest(request);

        //this assumes that the uploaded file is the only thing submitted by the form
        //if not you need to iterate the list and find it
        FileItem wordFile = items.get(0);

        //get a stream that can be used to read the uploaded file
        InputStream inputStream = wordFile.getInputStream();

        //and the rest you already know...
    }
}

When the user uploads their file, grab the associated InputStream and store it to a variable, like inputStream. Then just take the example code, and replace this line:

fs = new POIFSFileSystem(new FileInputStream(filesname));

...with something like:

fs = new POIFSFileSystem(inputStream); 

Should be simple enough, assuming that you already have a Servlet in place to handle the upload.

Edit:

Here are the basics of how the servlet could work, assuming that you are using commons-fileupload to parse the upload:

public class UploadServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // Create a factory for disk-based file items
        FileItemFactory factory = new DiskFileItemFactory();

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);

        // Parse the request
        List<FileItem> items = upload.parseRequest(request);

        //this assumes that the uploaded file is the only thing submitted by the form
        //if not you need to iterate the list and find it
        FileItem wordFile = items.get(0);

        //get a stream that can be used to read the uploaded file
        InputStream inputStream = wordFile.getInputStream();

        //and the rest you already know...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文