inputStream = new FileInputStream(path) 不起作用


  1. 这是Java Class中的代码。 redflaglight.png图像存在于图像文件夹中 找不到
    空指针异常在
    输入流 = 新的文件输入流(路径(; 支柱2.3.5 Java7

    public String createRootPath() throws UnsupportedEncodingException {  
    String rootPath = "";  
    String path = ExportExcelBusiness.class.getProtectionDomain()  
    .getCodeSource().getLocation().getPath();  
    File f = new File(path);  
    String ff = f.getParent();  
    f = new File(ff);  
    ff = f.getParent();  
    f = new File(ff);  
    ff = f.getParent();  
    f = new File(ff);  
    ff = f.getParent();  
    f = new File(ff);  
    ff = f.getParent();    
    f = new File(ff);  
    ff = f.getParent();  
    f = new File(ff);  
    ff = f.getParent();  
    String decodedPath = URLDecoder.decode(ff, "UTF-8");  
    rootPath = decodedPath.replace('\', '/');  
    rootPath += "/WebContent/pages/appsresponse/images";  
    return rootPath;  
    }  
    path = createRootPath()+ "/up_arrow_export.png";    
    InputStream inputStream = null;    
    int pictureIdx = 0;  
    byte[] bytes;  
    try {  
    inputStream = new FileInputStream(path);  
    bytes = IOUtils  
    .toByteArray(inputStream);  
    pictureIdx = wb.addPicture(bytes,
    Workbook.PICTURE_TYPE_PNG);  
    } finally {  
    if (inputStream != null) {  
    inputStream.close();  
    }  
    }
    

    尽管路径中存在文件,但无法获取该文件。这是在从 Struts2.2.1 升级到 struts2.3.5 后开始

如果您在服务器上运行此代码并使用相对路径,那么您的文件应该在服务器目录中。

your_server_path/pages/appsresponse/images

问题是您将图像保存在工作区中,您需要将其保存在服务器目录中。

编辑
验证您的路径

File file=new File(path);
System.out.print(file.getAbsolutePath()); //check whether file is present at this path or not

最新更新