Java Apache POI Zip文件已关闭

Java Apache POI Zip文件已关闭,java,spring-boot,apache-poi,doc,Java,Spring Boot,Apache Poi,Doc,我正在创建一个web API(使用Spring Boot 2.1.1.RELEASE),它在输入中接受一个dotx文件和参数(比如名称或电话),在输出中创建带有更改的docx文件(我使用ApachePOI4.0.1) 步骤: 1-获取输入点x和参数 2-将dotx转换为docx 3-将参数添加到docx文件并发送 控制员: @PostMapping("/addcontent") public ResponseEntity<ByteArrayResource> convertion(@

我正在创建一个web API(使用Spring Boot 2.1.1.RELEASE),它在输入中接受一个dotx文件和参数(比如名称或电话),在输出中创建带有更改的docx文件(我使用ApachePOI4.0.1)

步骤:

1-获取输入点x和参数

2-将dotx转换为docx

3-将参数添加到docx文件并发送

控制员:

@PostMapping("/addcontent")
public ResponseEntity<ByteArrayResource> convertion(@RequestParam(value = "file",required = false) MultipartFile file,@RequestParam(value="input",required = false) String para) throws  InvalidFormatException, IOException{
    Map<String,String> m = new HashMap<String,String>();
    m.put("namur",para);
    String document = "D:/"+file.getContentType().substring(0, file.getOriginalFilename().length() - 4)+"docx";
    ChangeDot.addContentTodot(m, file.getOriginalFilename(),document );

    FileSystemResource out = new FileSystemResource(document);

     return ResponseEntity
                .ok()
                .contentLength(out.contentLength())
                .contentType(
                        MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
                .body(new ByteArrayResource(IOUtils.toByteArray(out.getInputStream())));
}
如果有任何建议,请不要犹豫

谢谢大家!

试试这个:

@PostMapping("/addcontent")
public ResponseEntity<FileSystemResource> convertion(@RequestParam(value = "file",required = false) MultipartFile file,@RequestParam(value="input",required = false) String para) throws  InvalidFormatException, IOException{
    Map<String,String> m = new HashMap<String,String>();
    m.put("namur",para);
    String document = "D:/"+file.getContentType().substring(0, file.getOriginalFilename().length() - 4)+"docx";
    ChangeDot.addContentTodot(m, file.getOriginalFilename(),document );

    FileSystemResource out = new FileSystemResource(document);

     return ResponseEntity
                .ok()
                .contentLength(out.contentLength())
                .contentType(
                        MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
                .body(out);
}
@PostMapping(/addcontent)
公共响应属性转换(@RequestParam(value=“file”,required=false)多部分文件文件,@RequestParam(value=“input”,required=false)字符串参数)引发InvalidFormatException,IOException{
Map m=新的HashMap();
m、 付诸表决(“namur”,第2段);
String document=“D:/”+file.getContentType().substring(0,file.getOriginalFilename().length()-4)+“docx”;
ChangeDot.addContentTodot(m,file.getOriginalFilename(),文档);
FileSystemResource out=新的FileSystemResource(文档);
返回响应性
.ok()
.contentLength(out.contentLength())
.contentType(
parseMediaType(“application/vnd.openxmlformats of icedocument.wordprocessingml.document”))
.机构(外);
}
试试这个:

@PostMapping("/addcontent")
public ResponseEntity<FileSystemResource> convertion(@RequestParam(value = "file",required = false) MultipartFile file,@RequestParam(value="input",required = false) String para) throws  InvalidFormatException, IOException{
    Map<String,String> m = new HashMap<String,String>();
    m.put("namur",para);
    String document = "D:/"+file.getContentType().substring(0, file.getOriginalFilename().length() - 4)+"docx";
    ChangeDot.addContentTodot(m, file.getOriginalFilename(),document );

    FileSystemResource out = new FileSystemResource(document);

     return ResponseEntity
                .ok()
                .contentLength(out.contentLength())
                .contentType(
                        MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
                .body(out);
}
@PostMapping(/addcontent)
公共响应属性转换(@RequestParam(value=“file”,required=false)多部分文件文件,@RequestParam(value=“input”,required=false)字符串参数)引发InvalidFormatException,IOException{
Map m=新的HashMap();
m、 付诸表决(“namur”,第2段);
String document=“D:/”+file.getContentType().substring(0,file.getOriginalFilename().length()-4)+“docx”;
ChangeDot.addContentTodot(m,file.getOriginalFilename(),文档);
FileSystemResource out=新的FileSystemResource(文档);
返回响应性
.ok()
.contentLength(out.contentLength())
.contentType(
parseMediaType(“application/vnd.openxmlformats of icedocument.wordprocessingml.document”))
.机构(外);
}

看起来您正在将
MultipartFile.getOriginalFilename()
值(客户端系统上的文件名)传递给
OPCPackage.open()
方法,该方法需要服务器上的文件名

您应该这样做(
file
MultipartFile
实例):

InputStream InputStream=file.getInputStream();
XWPFDocument doc=新的XWPFDocument(OPCPackage.open(inputStream));

看起来您正在将
MultipartFile.getOriginalFilename()
值(客户端系统上的文件名)传递给
OPCPackage.open()
方法,该方法需要服务器上的文件名

您应该这样做(
file
MultipartFile
实例):

InputStream InputStream=file.getInputStream();
XWPFDocument doc=新的XWPFDocument(OPCPackage.open(inputStream));

是的,问题确实出在输入文件中!是的,问题确实出在输入文件中!
@PostMapping("/addcontent")
public ResponseEntity<FileSystemResource> convertion(@RequestParam(value = "file",required = false) MultipartFile file,@RequestParam(value="input",required = false) String para) throws  InvalidFormatException, IOException{
    Map<String,String> m = new HashMap<String,String>();
    m.put("namur",para);
    String document = "D:/"+file.getContentType().substring(0, file.getOriginalFilename().length() - 4)+"docx";
    ChangeDot.addContentTodot(m, file.getOriginalFilename(),document );

    FileSystemResource out = new FileSystemResource(document);

     return ResponseEntity
                .ok()
                .contentLength(out.contentLength())
                .contentType(
                        MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
                .body(out);
}