-4

i am on a java class, i am not on a servlet

the question is not duplicated, i am not on a servlet please guys.

and by a java class, i mean:

class HelloWorld{}

I am on server, and I want to allow the client to download a file

this is my code:

@GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    @Path("/downloadFile")
    public Response getFile() {
        File file = new File("/roma.txt");
        System.out.println("path = " + file.getPath());
        System.out.println("absoulte path = " + file.getAbsolutePath());
        return Response
                .ok(file, MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-Disposition",
                        "attachment; filename=\"" + file.getName() + "\"") // optional
                .build();

    }

the file is located in the WebContent of my project as you see here: enter image description here

i got this exception:

java.io.FileNotFoundException: /roma.txt (No such file or directory)
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • on UNIX systems `"/roma.txt"` is an absolute path, if you want to access a file from the program's working directory, remove the `/` from the start of the path. – Titus Sep 11 '15 at 11:04
  • "/roma.txt" means it will be looking at the root of the file hierarchy, and your file is under `WebContent` under your web server hierarchy. Remember that the class `File` doesn't know it's being used in a web server. – RealSkeptic Sep 11 '15 at 11:05
  • @Titus i already tried that, but the path after removing the `/` is inside the `Eclipse.app/MacOS` folder, and for sure no one would like to put his files there – Marco Dinatsoli Sep 11 '15 at 11:05
  • @RealSkeptic yes i know the problem but i don't know the solution, i mean i don't know the correct path – Marco Dinatsoli Sep 11 '15 at 11:05
  • Isn't the WebContent added to the war file, which means the file you need is stored in the archive file you run the application from? – fabian Sep 11 '15 at 11:09
  • This looks like a `Servlet`, you can use the servlet's context to access a resource file. – Titus Sep 11 '15 at 11:10
  • @RealSkeptic please try to read the solution of that question first, that is a wrong answer, what is ServlexContextAware ? i hope you know – Marco Dinatsoli Sep 11 '15 at 11:26
  • @RealSkeptic i have a java class not a java servlet – Marco Dinatsoli Sep 11 '15 at 11:31
  • @RealSkeptic see you too don't know the answer, so please next time i appreciate if you consider reading the whole context before marking any question as duplicated. Thanks for your time helping me – Marco Dinatsoli Sep 11 '15 at 11:50
  • A JAX-RX application *is* a servlet. You can inject the `ServletContext` into it. But fine - I'll let you do your own search for it. – RealSkeptic Sep 11 '15 at 11:55
  • @RealSkeptic so in that case, (where my class is not a totally servlet), do you still think that my question is duplicated? (i am sure the answer is yes, right? ) thanks for the comment i will keep checking – Marco Dinatsoli Sep 11 '15 at 12:00
  • thanks for the down vote guys, i don't know what mistake i did, i showed you what i do and i showed u the error, why i am getting down votes i don't know. – Marco Dinatsoli Sep 11 '15 at 12:02

1 Answers1

-2

Try to remove the "/",

File file = new File("roma.txt");

"/roma.txt" may refer to root directory.

This could work in Windows , Linux , Mac OSX :

File myFile = new File(System.getProperty("user.home"), "roma.txt");

and then "roma.txt" would put into home dir of current user.

Bin Wang
  • 1
  • 2