NanoHttpd / nanohttpd

Tiny, easily embeddable HTTP server in Java.
http://nanohttpd.org
BSD 3-Clause "New" or "Revised" License
6.92k stars 1.69k forks source link

Cannot host a website #540

Closed generic-matrix closed 5 years ago

generic-matrix commented 5 years ago

Hello, I am trying to host a website but there is a issue with the mime type.

here is the code : We have the code in the www folder

package com.example.sample.photolab;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;

import java.io.FileReader;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import fi.iki.elonen.NanoHTTPD;

public class HTTP_SERVER extends NanoHTTPD {

    public HTTP_SERVER(int port) {
        super(port);
    }

    public HTTP_SERVER(String hostname, int port) {
        super(hostname, port);
    }

    @Override
    public Response serve(IHTTPSession session) {
        //String msg = "<html><body><h1>Hello server</h1>\n";
        Map<String, String> parms = session.getParms();
        /*if (parms.get("username") == null) {
            msg += "<form action='?' method='get'>\n";
            msg += "<p>Your name: <input type='text' name='username'></p>\n";
            msg += "</form>\n";
        } else {
            msg += "<p>Hello, " + parms.get("username") + "!</p>";
        }*/

        return newChunkedResponse(Response.Status.OK,"text/html",new ByteArrayInputStream(web_page().getBytes(StandardCharsets.UTF_8)));
    }

    private String web_page(){

        String answer = "";
        try {
            // Open file from SD Card
            File root = Environment.getExternalStorageDirectory();
            FileReader index = new FileReader(root.getAbsolutePath() +
                    "/www/index.html");
            BufferedReader reader = new BufferedReader(index);
            String line = "";
            while ((line = reader.readLine()) != null) {
                answer += line;
            }
            reader.close();

        } catch(Exception ioe) {
            Log.e("Httpd", ioe.toString());
        }

        return answer;
    }

}

Screen Shot 2019-04-29 at 8 30 48 PM

This is a ### MIME type error but one thing i can see that the files are getting indexed.

Thanks

generic-matrix commented 5 years ago

www.zip here is the website code