0

Below is what i tried to send a HTTP POST request which send the json file as payload. The Error I always get is

java.io.FileNotFoundException: test.json (The system cannot find the file specified)

Although the test.json file is in the same folder.

private void sendPost() throws Exception {`

    String url = "url";
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(url);

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();

    String postData = AutomaticOnboarding.readFile("test.json");
    urlParameters.add(new BasicNameValuePair("data", postData));
    StringEntity se = new StringEntity(postData);
    post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");

    post.setEntity(se);

    HttpResponse response = httpClient.execute(post);
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + post.getEntity());
    int responseCode = response.getStatusLine().getStatusCode();
    System.out.println("Response Code : " +responseCode);
    if(responseCode == 200){
        BufferedReader rd = new BufferedReader(
                        new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        System.out.println(result.toString());
    }
}

Here follows the readFile method:

public static String readFile(String filename) {
    String result = "";
    try {
        BufferedReader br = new BufferedReader(new FileReader(filename));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            line = br.readLine();
        }
        result = sb.toString();
    } catch(Exception e) {
        e.printStackTrace();
    }
    return result;
}
JensS
  • 1,151
  • 2
  • 13
  • 20
First User
  • 11
  • 1
  • 1
    Your test.json should be in the same directory where you run `java` command assuming you open it with `new FileReader("test.json")` – Scadge Oct 04 '17 at 08:11
  • @Scadge ..my file is indeed in the same directory. I specified this in the question also. – First User Oct 04 '17 at 08:18
  • 1
    You should abstract your questions from all that POST context. Your problem can be resume that your file cannot be found. Did you take a look at solutions like this ? https://stackoverflow.com/questions/6639/how-should-i-load-files-into-my-java-application – Victor Petit Oct 04 '17 at 08:26
  • @FirstUser how do you run your program? Note that it's not the .java and test.json files should be in the same folder, but as I said, the test.json should be in the folder where you run your program, i.e. run `java` command – Scadge Oct 04 '17 at 09:20
  • @Scadge I am running program in Eclipse..should I move the file to some specific directory. – First User Oct 04 '17 at 10:10
  • Five years later, I have come across the same issue. Did you find a resolution for the same ? I want to post. payload.json under the resources directory – sss Mar 08 '23 at 09:50

1 Answers1

0

Use the class loader to get resources inside the jar

getClass().getClassLoader().getResourceAsStream(filename)
cisk
  • 589
  • 3
  • 13