Pregunta

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!

¿Fue útil?

Solución

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"

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top