Reputation: 336
I am using Java 8 and Apache HttpClient to create a RESTful web service that passes a filename as a message body. I am attempting to parse the data from the text file and add it to a List. Everything appears to work, except I am getting: Error Parsing: java.io.FileNotFoundException: testFile.txt (No such file or directory)
.
This is the service endpoint code:
@POST
@Path("/fileUpload")
@Consumes(MediaType.TEXT_PLAIN)
public Response fileUpload(String fileName) {
StringBuilder builder = new StringBuilder();
try {
List<String> items = new ArrayList<String>();
Scanner sc = new Scanner(new File(fileName));
while (sc.hasNext()) {
String line = sc.nextLine();
System.out.println(line);
items = Arrays.asList(line.split("\n"));
}
List<Integer> itemNbrs = new ArrayList<>();
for (String string : items) {
itemNbrs.add(Integer.valueOf(string));
}
sc.close();
itemNbrs.forEach(System.out::println);
// processImpl.checkItemsAndInsertIntoTable(itemNbrs);
} catch (Exception e) {
System.out.println("Error Parsing: " + e);
}
System.out.println("Data Received: " + builder.toString());
// return HTTP response 200 in case of success
return Response.status(200).entity(builder.toString()).build();
}
The test file:
123
2345
4567
74543
9875
I have tried to pass the filename as an InputStream, and I get the same exception. I have also changed the permissions on the test file using chmod 777
.
Stack Trace:
Error Parsing: java.io.FileNotFoundException: fileTest.txt
(No such file or directory)
Data Received:
Upvotes: 0
Views: 178
Reputation: 336
Building on RobOhRob's answer I figured it out. Once I hit the service with System.out.println(new File( fileName).getAbsolutePath());
at the beginning of the method it logged the absolute path as /Applications/Eclipse.app/Contents/MacOS/fileTest.txt
.
I put the file in that directory and passed the absolute path as the message body. If you are doing this make sure you ESCAPE YOU FORWARD SLASHES! The absolute path to used in the message body of the service looks like this:
//Applications//Eclipse.app//Contents//MacOS//fileTest.txt
Upvotes: 1
Reputation: 585
Try adding this at the beginning of your endpoint.
System.out.println(new File( fileName).getAbsolutePath());
You can thus see where you are currently looking for said file and adjust from there.
Upvotes: 1