有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java上传word文档

我需要从用户上传的word文档中提取文本。我获得了code从位于我的m/c上的文档中提取单词的权限。但我的要求是允许用户使用上载按钮&;阅读该文档(我不需要保存该文档)。你能建议我怎么做吗?我需要知道点击上传按钮后需要发生什么


共 (1) 个答案

  1. # 1 楼答案

    当用户上传他们的文件时,抓取相关的InputStream并将其存储到变量中,如inputStream。然后只需使用示例代码,替换这一行:

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

    。。。比如:

    fs = new POIFSFileSystem(inputStream); 
    

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

    编辑:

    假设您正在使用commons-fileupload解析上传,下面是servlet工作原理的基本知识:

    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...
        }
    }