Akka http在Java中创建分块实体(从Scala转换)
我正在尝试将以下代码从Scala转换为Java:Akka http在Java中创建分块实体(从Scala转换),akka,akka-http,Akka,Akka Http,我正在尝试将以下代码从Scala转换为Java: object ChunkedStaticResponse { private def createStaticSource(fileName : String) = FileIO .fromPath(Paths get fileName) .map(p => ChunkStreamPart.apply(p)) private d
object ChunkedStaticResponse {
private def createStaticSource(fileName : String) =
FileIO
.fromPath(Paths get fileName)
.map(p => ChunkStreamPart.apply(p))
private def createChunkedSource(fileName : String) =
Chunked(ContentTypes.`text/html(UTF-8)`, createStaticSource(fileName))
def staticResponse(page:String) =
HttpResponse(status = StatusCodes.NotFound,
entity = createChunkedSource(page))
}
但是我在第二种方法的实现上遇到了麻烦。到目前为止,我已经做到了:
class ChunkedStaticResponseJ {
private Source<HttpEntity.ChunkStreamPart, CompletionStage<IOResult>>
createStaticSource(String fileName) {
return FileIO
.fromPath(Paths.get(fileName))
.map(p -> HttpEntity.ChunkStreamPart.create(p));
}
private HttpEntity.Chunked createChunkedSource(String fileName) {
return HttpEntities.create(ContentTypes.TEXT_HTML_UTF8,
createStaticSource(fileName)); // not working
}
public HttpResponse staticResponse(String page) {
HttpResponse resp = HttpResponse.create();
return resp.withStatus(StatusCodes.NOT_FOUND).withEntity(createChunkedSource(page));
}
}
类ChunkedStaticResponseJ{
私人来源
createStaticSource(字符串文件名){
返回文件
.fromPath(路径.get(文件名))
.map(p->HttpEntity.ChunkStreamPart.create(p));
}
私有HttpEntity.Chunked createChunkedSource(字符串文件名){
返回HttpEntities.create(ContentTypes.TEXT\u HTML\u UTF8,
createStaticSource(文件名));//不工作
}
公共HttpResponse静态响应(字符串页){
HttpResponse resp=HttpResponse.create();
返回与状态(未找到状态代码)的响应。与实体(createChunkedSource(第页))的响应;
}
}
我不知道如何在第二种方法中创建分块源。
有人能建议一种方法吗?另外,我的路径是否正确?如果您只想通过testring从文件中读取的每个
元素创建块,您可以利用Chunked.fromData
(HttpEntities.createChunked
在JavaDSL中)
下面是Scala方面的结果
object ChunkedStaticResponse {
private def createChunkedSource(fileName : String) =
Chunked.fromData(ContentTypes.`text/html(UTF-8)`, FileIO.fromPath(Paths get fileName))
def staticResponse(page:String) =
HttpResponse(status = StatusCodes.NotFound,
entity = createChunkedSource(page))
}
这就是它的JavaDSL对应物
class ChunkedStaticResponseJ {
private HttpEntity.Chunked createChunkedSource(String fileName) {
return HttpEntities.createChunked(ContentTypes.TEXT_HTML_UTF8, FileIO.fromPath(Paths.get(fileName)));
}
public HttpResponse staticResponse(String page) {
HttpResponse resp = HttpResponse.create();
return resp.withStatus(StatusCodes.NOT_FOUND).withEntity(createChunkedSource(page));
}
}