春季启动创buildexcel错误(文件名,目录名称或卷标语法不正确)]

我想写创build一个Excel并下载,但是当我添加图像我得到以下错误。

[(文件名,目录名或卷标语法不正确)]

但是,如果我打印出它的名字和path,它显示仪式的名称和path,文件被加载

文件名称:example.png位置文件:\ D:\ blueplustech \ plastocartreport \ target \ plastocartreport-0.0.1-SNAPSHOT.jar!\ BOOT-INF \ classes!\ static \ example.png

类:

//FileInputStream obtains input bytes from the image file InputStream inputStream = new FileInputStream(getClass().getResource("/static/example.png").getFile()); //Get the contents of an InputStream as a byte[]. byte[] bytes = IOUtils.toByteArray(inputStream); //Adds a picture to the workbook int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG); //close the input stream inputStream.close(); //Returns an object that handles instantiating concrete classes CreationHelper helper = workbook.getCreationHelper(); //Creates the top-level drawing patriarch. Drawing drawing = sheet.createDrawingPatriarch(); //Create an anchor that is attached to the worksheet ClientAnchor anchor = helper.createClientAnchor(); //set top-left corner for the image anchor.setCol1(1); anchor.setRow1(2); //Creates a picture Picture pict = drawing.createPicture(anchor, pictureIdx); //Reset the image to the original size pict.resize(); 

有什么build议么…?

不需要使用文件,而是使用getResourceAsStream

而不是

 InputStream inputStream = new FileInputStream(getClass().getResource("/static/example.png").getFile()); 

使用

 InputStream inputStream = getClass().getResourceAsStream("/static/example.png");