3 Replies Latest reply: May 18, 2012 2:17 PM by aksarben RSS

    Deleting a file on the Server - HTTP

    user621430
      Hi ,

      I have a file on my application server. And I have http permissions on it.

      So, I am able to read the file using the following piece of code.

      url = new java.net.URL("http://devapp/devreports/Anand.pdf");
      inputStream = url.openStream();
      printInputStream (inputStream);

      Is there a way, I can delete this particular file with the http permission I have?

      Thanks!
      Anand
        • 1. Re: Deleting a file on the Server - HTTP
          gimbal2
          Everything is possible; not everything is a good idea. You could for example implement a servlet that accepts a HTTP DELETE request.

          http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServlet.html#doDelete%28javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse%29

          But if you do that, you are not the only one that will be able to delete files. HTTP is quite a public thing you know. So perhaps you will want to ask yourself a question: do I really WANT to be able to remove a file when you have only HTTP access?
          • 2. Re: Deleting a file on the Server - HTTP
            user621430
            Thank you for your response.

            Is there a way without servlets.

            URL url = new URL("http://devapp/Anand.pdf");
            HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
            httpCon.setDoOutput(true);
            httpCon.setRequestProperty(
            +"Content-Type", "application/x-www-form-urlencoded" );+
            httpCon.setRequestMethod("DELETE");
            httpCon.connect();

            I tried the above, but got a method not allowed 404 response.

            Thanks!
            • 3. Re: Deleting a file on the Server - HTTP
              aksarben
              No easy way. Yeah, you could always set up a VPN, special socket, or something else, but it's still a poor idea. If you can get to it remotely, there's always a chance a hacker, can, too.