mongodb 下载word 二进制流文件 转出html文件

本文介绍了一个将.docx和.doc文件转换为HTML预览的方法,包括使用XWPFDocument和HWPFDocument读取文件,以及如何处理图片并保存到指定目录。通过这种方式,可以将Word文档内容转换为网页进行预览。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@RequestMapping(value="/getyjpllook",produces="application/json;charset=UTF-8")
    public  void getyjpllook(String plid)throws Throwable {
        
        String filepath = "C:/html";
        File file1=new File(filepath);
        if(!file1.exists()){  
            file1.mkdir();  
        }  
        String picturesPath = filepath+"/image/";
        File picturesDir = new File(picturesPath);
        if(!picturesDir.exists()){  
            picturesDir.mkdir();  
        } 
        String content = null;
        //InputStream in =new FileInputStream(new File(docFile));
        TbfileSelect tbfileSelect = new TbfileSelect();
        tbfileSelect.setPlanid(plid);
        List<Tbfile> filelist = tbfileservice.getTbfileListByplanid(tbfileSelect);
        ByteAndInfo bi = gsMongoClient.getByteAndInfo(InDomainCommConst.PLAN_NAME,filelist.get(0).getId());
        ByteArrayInputStream input = new ByteArrayInputStream(bi.getByteFile());
        String[] strArray = bi.getName().split("\\.");
        if(strArray[strArray.length -1].equals("docx")){
            XWPFDocument document = new XWPFDocument(input);
            XHTMLOptions options = XHTMLOptions.create();
            options.setExtractor(new FileImageExtractor(picturesDir));
            options.URIResolver(new BasicURIResolver(picturesPath));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            XHTMLConverter.getInstance().convert(document, baos, options);
            baos.close();
            content = baos.toString();
        }else{
            HWPFDocument wordDocument =new HWPFDocument(input);
            WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
            wordToHtmlConverter.setPicturesManager(new PicturesManager(){
                @Override
                public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches,
                        float heightInches) {
                    File file = new File(picturesPath + suggestedName);
                    FileOutputStream fos = null;
                    try{
                        fos = new FileOutputStream(file);
                        fos.write(content);
                        fos.close();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    return picturesPath + suggestedName;
                }
            });
            wordToHtmlConverter.processDocument(wordDocument);
            org.w3c.dom.Document htmlDocument = wordToHtmlConverter.getDocument();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            DOMSource domSource = new DOMSource(htmlDocument);
            StreamResult streamResult = new StreamResult(out);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer serializer = tf.newTransformer();
              serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
              serializer.setOutputProperty(OutputKeys.INDENT, "yes");
              serializer.setOutputProperty(OutputKeys.METHOD, "html");
              serializer.transform(domSource, streamResult);
              out.close();
              content = new String(out.toByteArray());
        }
        File fileyl = new File("C:/html");
        if(fileyl.exists()) {//删除原来的旧文件
         fileyl.delete();
        }
        FileUtils.writeStringToFile(new File("C:/html", "预览.html"), content, "utf-8");
       // File file = new File("预览.html");
        //Runtime ce=Runtime.getRuntime();
      //  ce.exec("cmd /c start "+fileyl.getAbsolutePath()+"/预览.html");//

下载 MongoDB 7 的二进制文件,可以通过访问 MongoDB 官方网站获取适用于不同操作系统的预编译二进制包。以下是详细的下载和安装步骤: 1. **访问官方下载页面** 打开 [MongoDB 下载中心](https://www.mongodb.com/try/download),选择 "MongoDB Community Server",然后在版本(Version)下拉菜单中选择 `7.0` 或更新的稳定版本。 2. **选择操作系统与包类型** 根据你的操作系统(例如 Linux、macOS 或 Windows),选择对应的架构(如 x86_64)和包格式(`.tar.gz` 对于 Linux 用户,`.msi` 或 `.zip` 对于 Windows 用户)。 3. **下载并解压** 以 Linux 系统为例,下载完成后使用以下命令进行解压: ```bash tar -zxvf mongodb-linux-x86_64-*.tgz ``` 接着将解压后的文件移动到指定目录,例如 `/usr/local/mongodb`: ```bash mv mongodb-linux-x86_64-* /usr/local/mongodb ``` 4. **创建数据与日志目录** 在 MongoDB 安装目录下创建用于存储数据和日志的路径: ```bash mkdir -p /usr/local/mongodb/data/db mkdir -p /usr/local/mongodb/logs ``` 5. **配置 MongoDB** 创建或编辑 `mongod.conf` 配置文件,通常位于 `/usr/local/mongodb/bin/` 或 `/etc/` 目录下,内容包括数据库路径、日志路径以及是否启用认证等设置[^3]。 6. **启动 MongoDB** 使用以下命令启动 MongoDB 实例: ```bash cd /usr/local/mongodb/bin/ ./mongod --config ../mongod.conf ``` 如果需要启用安全认证,请确保在配置文件中添加如下内容: ```yaml security: authorization: enabled ``` 这样可以防止未经授权的访问,提高数据库的安全性。 ### 示例:连接 MongoDB 启动 MongoDB 后,可以在另一个终端窗口运行以下命令连接本地数据库: ```bash ./mongo ``` 如果启用了认证机制,则需要提供用户名和密码进行登录: ```bash ./mongo -u <username> -p <password> ``` ### 注意事项 - 如果计划用于生产环境,建议使用更稳定的发行版,并参考官方文档进行详细配置。 - 备份数据时,可以使用 `mongodump` 命令进行导出,示例: ```bash mongodump -u zsr -p 123456 -o /path/to/backup ``` 这样可以确保数据在发生异常时能够快速恢复[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值