2017-04-04 66 views
0

我想編寫創建一個Excel並下載它,但是當我添加圖像時,我得到以下錯誤。春季啓動創建excel錯誤(文件名,目錄名稱或卷標語法不正確)]

[(文件名,目錄名或卷標籤語法不正確)]

,但如果我打印輸出其名稱和路徑它顯示儀式名稱和路徑將文件加載

文件名:example.png文件位置:\ d:!\ blueplustech \ plastocartreport \目標\ plastocartreport-0.0.1-SNAPSHOT.jar \ BOOT-INF \類\靜\ 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(); 

任何建議...?

回答

1

無需使用文件,但代替的getResourceAsStream

取而代之的

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

使用

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

讓我檢查一下。 –

+0

這個工作正常。謝謝你的時間。 –

相關問題