You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java/Rest Assured中添加xlsx附件时触发FileNotFoundException,但txt文件可正常添加

Troubleshooting FileNotFoundException for Excel Attachments in RestAssured + JIRA

Hey there, let's figure out why your Excel file is throwing a FileNotFoundException while your text file works perfectly. Since the core attachment logic works for .txt, the issue is almost certainly related to how your program locates or accesses the Excel file—here are some actionable fixes:

1. Double-check file path case and spelling (critical for cross-platform)

Operating systems like Linux and macOS are case-sensitive, even if you're developing on Windows. Make sure your filename is exactly API.xlsx (no typos like api.xlsx or API.XLSX). To confirm where your program is looking, add a quick path print before your RestAssured call:

File excelAttachment = new File("API.xlsx");
System.out.println("Looking for Excel file at: " + excelAttachment.getAbsolutePath());
// Rest of your code here...

If the printed path doesn't point to your project's root directory JIRA-API, your runtime working directory isn't what you expect.

2. Fix your runtime working directory

IDEs like IntelliJ or Eclipse sometimes set the working directory to something other than your project root (e.g., the target folder for Maven/Gradle).

  • For IntelliJ: Go to Run/Debug Configurations → under your run configuration, set the Working directory to $MODULE_DIR$ (this points to your project root).
  • For a quick test, use an absolute file path instead:
    // Windows example
    .multiPart("file", new File("C:/path/to/your/JIRA-API/API.xlsx"))
    // Linux/macOS example
    .multiPart("file", new File("/home/yourusername/projects/JIRA-API/API.xlsx"))
    

If this works, you know the relative path was the problem.

3. Use classpath resources for reliable file access

If you're using a build tool like Maven or Gradle, putting files in the project root might mean they don't get copied to your runtime classpath. Move API.xlsx to src/test/resources (since this is likely test code) and load it via the classpath instead:

.multiPart("file", new File(getClass().getResource("/API.xlsx").getFile()))

This method works regardless of your working directory, making your code more portable.

4. Check for file locks or permissions

Make sure API.xlsx isn't open in Excel or another program—locked files can't be read by Java. Also, verify that the user running your program has read permissions for the file.

Modified Code Snippet (with path verification)

Here's your code with the path check added to help debug:

//Add an attachment
System.out.println("\n\n...............ADD AN ATTACHMENT TO BUG..........\n\n");

// Debug: Check file path and existence
File excelFile = new File("API.xlsx");
System.out.println("Excel file absolute path: " + excelFile.getAbsolutePath());
System.out.println("File exists? " + excelFile.exists());

given().log().all().header("X-Atlassian-Token","no-check").filter(session)
 .header("Content-Type","multipart/form-data")
 .multiPart("file", excelFile)
 .when().post("rest/api/2/issue/"+id+"/attachments")
 .then().log().all().assertThat().statusCode(200);

内容的提问来源于stack exchange,提问作者sugirtha chandrasekaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 12:37:34