2

I'm developing an app that will send a file using HTTP server (with nanoHTTPD) to another device by typing the sender's IP:port. The transferring is working correctly, but I'm not able to receive the right file name of the sent file (its being named as 'default', without any extension, by the receiver's browser). Here's my code for the HTTP server:

private class WebServer extends NanoHTTPD {

        public WebServer()
        {
            super(8080);
        }

        @Override
        public Response serve(String uri, Method method,
            Map header, Map parameters,
            Map files) {

        //receive my file's path from an intent

        Intent intent = getIntent();
        String filename = intent.getStringExtra(MainActivity.FILENAME);

        FileInputStream file = null;

        try {

            file = new FileInputStream(filename);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return new NanoHTTPD.Response(Status.OK, "/", file);
      }

I thought I could fix it using FileInputStream(new File(String path, String name)) but it still doesn't work right and it still give me a 'default' file name with 0 byte file size.

Can anyone give me some idea how can I get the right file name from the HTTP server? Hope someone can help me here. Thanks!

1 Answers1

4

It depends on what your server (script) accepts, when sending you should add headers with name of your file, this can be:

Content-Disposition: attachment; filename="fname.ext"

example from http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html "19.5.1 Content-Disposition"

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • I'm not familiar with this 'Content-Disposition', because I'm only retrieving the file's path from an Intent(which contains a simple string), that's why I thought I could set the file name using other method. Is there any simpler solution for this? – The_Student Jan 27 '14 at 08:53
  • Let me just explore this Content-Disposition and I will get back here if it will work. Thanks for the link you provided. – The_Student Jan 27 '14 at 08:56
  • If file is transfered over HTTP then such information like file name is mostly transfered in headers. If you can read headers on your second device, then you can set on device acting as server whatever header you like. This can be even: "myheader-filename: "myfile.txt"" – marcinj Jan 27 '14 at 09:01
  • Thanks again for that information. I'm currently reading some info about nanoHTTPD and headers. Thanks again! – The_Student Jan 27 '14 at 09:04
  • Can I ask again about headers? I think this the code to be used if I want to add headers: header.put(name, value). But I don't know what to put on its "name". I tried putting "filename" on it but it does nothing. Can you explain to me how can I add headers? – The_Student Jan 27 '14 at 11:25
  • 1
    For Apache HTTP client you use ie. HttpPost.addHeader(HEADER_NAME, HEADER_VALUE), for URLConnection its addRequestProperty(HEADER_NAME, HEADER_VALUE) – marcinj Jan 27 '14 at 12:19
  • Thanks for your help! I already got it by using this line: header.put("Content-Disposition: attachment; filename=", filename); – The_Student Jan 28 '14 at 00:07