gpt4 book ai didi

kotlin - 接收表单参数到 Ktor 服务器

转载 作者:行者123 更新时间:2023-12-02 12:04:37 27 4
gpt4 key购买 nike

我是 Java 和 Kotlin 的新手,试图用 Ktor 构建一个联系表单,所以我从 here 启用了我的 gmail 的不安全连接,并构建了下面的应用程序:
blogApp.kt :

package blog

import org.jetbrains.ktor.netty.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.features.*
import org.jetbrains.ktor.host.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.response.*

import org.apache.commons.mail.*

fun Application.module() {
install(DefaultHeaders)
install(CallLogging)
install(Routing) {
get("/") {
call.respondText("""
My Example Blog2
<form action="/contact-us" method="post">
<input name="subject" placeholder="Subject">
<br>
<textarea name="message" placeholder="Your message ..."></textarea>
<br>
<button>Submit</button>
</form>
""", ContentType.Text.Html)
}
post("/contact-us") {
SimpleEmail().apply {
setHostName("smtp.gmail.com")
setSmtpPort(465)
setAuthenticator(DefaultAuthenticator("my_alias@gmail.com", "my_gmil_passoword"))
setSSLOnConnect(true)
setFrom("my_alias@gmail.com")
setSubject("subject") // I need to use formParam
setMsg("message") // I need to use formParam
addTo("my_alias@gmail.com")
}.send() // will throw email-exception if something is wrong
call.respondRedirect("/contact-us/success")
}
get("/contact-us/success") {
call.respondText("Your message was sent", ContentType.Text.Html)
}
}
}

fun main(args: Array<String>) {
embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start()
}
build.gradle :
group 'Example'

version 'alpha'

buildscript {
ext.kotlin_version = '1.1.4-3'

repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = 1.8
ext.ktor_version = '0.4.0'

repositories {
mavenCentral()
maven { url "http://dl.bintray.com/kotlin/ktor" }
maven { url "https://dl.bintray.com/kotlin/kotlinx" }
}

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile "org.jetbrains.ktor:ktor-core:$ktor_version"
compile "org.jetbrains.ktor:ktor-netty:$ktor_version"
compile "org.apache.commons:commons-email:1.4"
compile "org.slf4j:slf4j-simple:1.7.25"
compile "ch.qos.logback:logback-classic:1.2.1"
testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
kotlin {
experimental {
coroutines "enable"
}
}


jar {
baseName 'abc'
manifest {
attributes 'Main-Class': 'blog.BlogAppKt'
}

from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

事情进展顺利,我能够给自己发送一封电子邮件,然后重定向到成功页面,但是发送的消息是带有预设数据的:
        setSubject("subject")        // I need to use formParam
setMsg("message") // I need to use formParam

如何让 Ktor 接收用户真正在表单中输入的数据,如何读取表单参数?

最佳答案

好吧,虽然上面的答案在那个时候是正确的,但今天当我使用 kotlin + Ktor 时,我发现上面的答案不再有效。

你现在需要的是这样的:

  • call.receive<参数>()["PARAM_NAME"]

  • 参数类在以下包中:io.ktor.http.Parameters

    尼基尔

    关于kotlin - 接收表单参数到 Ktor 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46384446/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com