java中将外部上传的.doc文件格式转换为.docx文件格式

所有依赖:

只能实现文字的转换

代码:

只有controller层

@RestController
public class ConversionController {
    @ApiOperation("文件转换")
    @RequestMapping("/uploadAndConvert")
    public ResponseEntity<ByteArrayResource> convertToDocx(@RequestParam("file") MultipartFile uploadedFile) throws IOException {
        if (uploadedFile.isEmpty()) {
            return ResponseEntity.badRequest().build();
        }

        String originalFileName = uploadedFile.getOriginalFilename();

        // 检查文件扩展名,确保上传的文件是DOC格式
        if (!originalFileName.toLowerCase().endsWith(".doc")) {
            return ResponseEntity.badRequest().body(new ByteArrayResource(new byte[0]));
        }

        //读取上传的DOC文件
        InputStream docInputStream = uploadedFile.getInputStream();
        HWPFDocument doc = new HWPFDocument(docInputStream);

        // 创建一个新的DOCX文档
        XWPFDocument docx = new XWPFDocument();

        // 从原始DOC文件中提取文本
        Range range = doc.getRange();
        String docText = range.text();

        // 将提取的文本添加到新的DOCX文档
        docx.createParagraph().createRun().setText(docText);

        // 将新的DOCX文档保存到字节数组
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        docx.write(outputStream);

        // 创建Blob格式的响应
        byte[] docxBytes = outputStream.toByteArray();
        ByteArrayResource resource = new ByteArrayResource(docxBytes);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=converted.docx");

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(docxBytes.length)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(resource);
    }
}

可以实现图片的转换

controller层

@RestController
public class ConversionController {
    @ApiOperation("文件转换")
    @RequestMapping("/uploadAndConvert")
    public ResponseEntity<ByteArrayResource> convertToDocx(@RequestParam("file") MultipartFile uploadedFile) throws IOException {
        if (uploadedFile.isEmpty()) {
            return ResponseEntity.badRequest().build();
        }

        String originalFileName = uploadedFile.getOriginalFilename();

        // 检查文件扩展名,确保上传的文件是DOC格式
        if (!originalFileName.toLowerCase().endsWith(".doc")) {
            return ResponseEntity.badRequest().body(new ByteArrayResource(new byte[0]));
        }

        //读取上传的DOC文件
        InputStream docInputStream = uploadedFile.getInputStream();

        Document document = new Document(docInputStream);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        document.saveToStream(outputStream, FileFormat.Docx);

        byte[] docxBytes = outputStream.toByteArray();
        ByteArrayResource resource = new ByteArrayResource(docxBytes);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=converted.docx");

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(docxBytes.length)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(resource);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值