-1

I have a Java class not a Java servlet

    @Path("/helloworld")
    public class HelloWorld  {
    @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();

    }
}

and I want to read the file roma.txt located in the web content as you see:

enter image description here

I have found many answers using getServletContext but I am not in a servlet. Thus, I can't use that function.

Help me please to get the roma.txt file

I am getting file not found exception

again: I swear to God that i am not on a servlet and I don't know why users just give me answers using servlet. i really appreciate it the helping, but please try to read the question first, i said in bold that I am NOT on a servlet

Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • Doesn't this work: `WebContent/roma.txt` ? Here: `File file = new File("roma.txt");` – ACV Sep 11 '15 at 11:44
  • @ACV the same exception `java.io.FileNotFoundException: WebContent/roma.txt (No such file or directory)` – Marco Dinatsoli Sep 11 '15 at 11:45
  • @JoSSte i am not in a jar file, please let's not post questions like this because a lot of users will consider my question duplicated, but it is NOT, specially these users who have great points, if one of them said this question is duplidaed, the question will be closed – Marco Dinatsoli Sep 11 '15 at 11:46
  • `getClass().getClassLoader().getResource("webcontent/roma.txt")` I have used that approach outside a jar file... – JoSSte Sep 11 '15 at 11:56
  • or maybe `final File f = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()+"\\path\\to\\file");` – JoSSte Sep 11 '15 at 12:01
  • an important thing to know is how are you running the application, is it a web-app running in a container? or is it a command-line application? – Daniel Camarda Sep 11 '15 at 12:05
  • @JoSSte according to your suggestions, my code is `final File file = new File(HelloWorld.class.getProtectionDomain() .getCodeSource().getLocation().getPath() + "/roma.txt");` but the absolute path is `absoulte path = /Users/williamkinaan/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/iOSServer/WEB-INF/classes/com/education/api/HelloWorld.class/roma.txt` do you suggest any edit please? – Marco Dinatsoli Sep 11 '15 at 12:08
  • @DanielCamarda i am running my dynamic web project in eclipse, does that give you an answer to your question please? – Marco Dinatsoli Sep 11 '15 at 12:08
  • 1
    If you need to do that, update your original question, don't ask a new one. – John Palmer Sep 11 '15 at 12:18
  • @JohnPalmer kindly check the comments there, i told everyone answers me, and i said that explicitly on the comments below the question. you are an expert users and you can imagine that no one will answer that questions. please if you have any solution help me. – Marco Dinatsoli Sep 11 '15 at 12:20
  • @JohnPalmer just check how that question was marked duplicated to a quesiont that is completely un related, i said explicity that my question is not a servlet. just tell me please how can that question be answered if it is marked as duplicated while it is not? – Marco Dinatsoli Sep 11 '15 at 12:21
  • @MarcoDinatsoli - To further aid, I'd need the folder path of the deployed class file. Your screengrab shows the path relative to the source .java file... – JoSSte Sep 11 '15 at 12:22
  • @JohnPalmer this question is (will) be duplicated to that question, and that question marked duplicated to a complexly unrelated question. what should I do please? the solution is to mark the first question as Non duplicate, and plus as you see, i said explicitly here that i am not on a servlet, and yet the answer is **go to use servet** – Marco Dinatsoli Sep 11 '15 at 12:22
  • 1
    The original question has not (yet) been closed as a duplicate. Someone thinks it is. It will take 5 votes to close it, but it only has one. Edit your question to show how it is not a duplicate and it should stay open. – John Palmer Sep 11 '15 at 12:22
  • @JohnPalmer i edited that quesiotn as well. plus could you see the down votes? i showed people what did i try, so why they down vote? plus as you see they also DON'T know the solution. but simply they mark the question as duplicated, they down vote .... – Marco Dinatsoli Sep 11 '15 at 12:24
  • @JohnPalmer as you see, no one knows the answer, and yet i am getting donwvotes, and a duplicate question ( which is not) – Marco Dinatsoli Sep 11 '15 at 12:31
  • @Louis may I hear your opinion on this discussion please? – Marco Dinatsoli Sep 11 '15 at 12:31
  • @JohnPalmer please see that people are down voting that question even after the update. i don't know what to do, any suggestion to deal with this is great, i don't need the answer anymore, i just need to know how to deal with this situation in stackoverlfow – Marco Dinatsoli Sep 11 '15 at 12:35
  • @MarcoDinatsoli This question is a duplicate of your earlier one. Per Stack Overflow editorial practices, when a user posts a problem and then later reposts the same problem in a new question, even with new information, this is a duplicate. The new information does not make it not a duplicate. As for your original question, it's been a while since I've worked with Java so barring anything evident I'm missing I'd say if you'd explain the execution context of your code instead of just stating "I'm not on a servlet", it would help. Probably some people just disbelieve "I'm not on a servlet". – Louis Sep 11 '15 at 12:37
  • @Louis now i am not looking for the answer, believe me even if i got fired, i don't care anymore, i just need to know why people are donevote my question. and c'mon i said i am on a normal java class, and i showed u the header `class HelloWorld` what more could i say to show you my context? – Marco Dinatsoli Sep 11 '15 at 12:39
  • @MarcoDinatsoli What loads that class and executes the code? – Louis Sep 11 '15 at 12:43
  • @Louis i don't know a clear answer to your query, i just created a eclipse dynamic web project, and i added my code listed on the question. if you know any way to tell me to give u more information, i will be ready – Marco Dinatsoli Sep 11 '15 at 12:45

1 Answers1

-1

You can use ServletContext.

public Response getFile(@Context ServletContext servletContext) {
 File roma = new File(servletContext.getRealPath("roma.txt"));
}

I mean, you're running on a sever aren't you?

ACV
  • 9,964
  • 5
  • 76
  • 81
  • that is why i wrote my first line in bold. I am NOT on a SERVLET, – Marco Dinatsoli Sep 11 '15 at 11:51
  • You wrote this: "I have a Java class not a Java servlet" and yes, this class is not a servlet. However JAX-RS can inject the context in your resource method. So, are you running in a web container or as a standalone app? – ACV Sep 11 '15 at 11:54
  • please do you see me passing serveltContext in my method? i would like to solve it really without any thing connected to servlet. thanks for your time, i appreciate the help – Marco Dinatsoli Sep 11 '15 at 11:59
  • You can try putting the file under WEB-INF and then accessing like you do it now.? – ACV Sep 11 '15 at 12:01