发送POST请求(Apache httpclient,这里是Kotlin源代码):
val httpPost = HttpPost("http://localhost:8000")
val builder = MultipartEntityBuilder.create()
builder.addBinaryBody("file", File("testFile.zip"),
ContentType.APPLICATION_OCTET_STREAM, "file.ext")
val multipart = builder.build()
httpPost.entity = multipart
val r = httpClient.execute(httpPost)
r.close()我通过spark-java Request-object在post处理程序中接收请求。如何从post请求中检索原始文件(加上作为奖励的文件名)?request.bodyAsBytes()方法似乎添加了一些字节,因为正文比原始文件大。
谢谢,Jörg
发布于 2017-10-04 22:09:05
在Spark的文档页面的底部有一个"Examples and FAQ"部分。第一个例子是“我如何上传一些东西?”从那里,它进一步链接到example on GitHub。
简而言之:
post("/yourUploadPath", (request, response) -> {
request.attribute("org.eclipse.jetty.multipartConfig", new MultipartConfigElement("/temp"));
try (InputStream is = request.raw().getPart("file").getInputStream()) {
// Use the input stream to create a file
}
return "File uploaded";
});要访问原始文件名:
request.raw().getPart("file").getSubmittedFileName()为了处理多个文件或部分,我通常有类似于下面的代码(假设多部分编码上传中只包含文件):
for (Part part : req.raw().getParts()) {
try (InputStream stream = part.getInputStream()) {
String filename = part.getSubmittedFileName();
// save the input stream to the filesystem, and the filename to a database
}
}https://stackoverflow.com/questions/46566268
复制相似问题