Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to run server and display index.html and push image to index.html #648

Open
kun510 opened this issue Aug 29, 2024 · 0 comments
Open

Comments

@kun510
Copy link

kun510 commented Aug 29, 2024

I have created a server and I have displayed the index.html file on the server, but now I want to transmit that image to the server and display it in index.html, what should I do?

my code :

class SimpleHTTPServer(port: Int,private val context: Context) : NanoHTTPD(port) {

override fun serve(session: IHTTPSession): Response {
    return when {
        session.uri.endsWith("/") -> serveFile("index.html")
        else -> serveFile(session.uri.substring(1))
    }
}

private fun serveFile(fileName: String): Response {
    return try {
        val assetManager = context.assets
        val inputStream = assetManager.open(fileName)

        val mimeType = when {
            fileName.endsWith(".html") -> "text/html"
            fileName.endsWith(".jpg") -> "image/jpeg"
            fileName.endsWith(".png") -> "image/png"
            fileName.endsWith(".js") -> "application/javascript"
            else -> "application/octet-stream"
        }

        newFixedLengthResponse(Response.Status.OK, mimeType, inputStream, inputStream.available().toLong())

    } catch (e: IOException) {
        Log.e("ImageStreamer", "File not found: $fileName", e)
        newFixedLengthResponse(Response.Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "File not found")
    }
}

}

index.html :

< b o d y >
< i m g class="center" id="Image" src="logo.jpg"/>
</b o d y>

start server:
private fun startServer() {
server= SimpleHTTPServer(8080,context)
server?.start()
Log.d(tag, "Server started")
}

send image on server:
private fun sendImageToServer(base64Image: String) {
val url = URL(SERVER_CONNECT)
Thread {
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
connection.doOutput = true

        val postData = "file=$base64Image"

        OutputStreamWriter(connection.outputStream).use {
            it.write(postData)
            it.flush()
        }

        val responseCode = connection.responseCode
        if (responseCode == HttpURLConnection.HTTP_OK) {
            Log.d("ImageStreamer", "Image uploaded successfully.")
        } else {
            Log.d("ImageStreamer", "Error uploading image: $responseCode")
        }
    }.start()
}
@kun510 kun510 changed the title how to run server and display index.html and pass image to index.html how to run server and display index.html and push image to index.html Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant