【发布时间】:2015-05-14 04:47:04
【问题描述】:
我使用 Play 框架开发了 Web 服务。我在 Play 框架中创建了 POST 休息服务,如下所示。
在路由文件中:
POST /imageUpload controllers.Application.imageUpload()
在 Application.java 中:
public static Result imageUpload() {
ObjectNode result = Json.newObject();
MultipartFormData body = request().body().asMultipartFormData();
FilePart file = body.getFile("file");
File trainImg = file.getFile();
File temp = new File("/Play_Framework/temp.jpg");
trainImg.renameTo(temp);// moved the image to another folder to check the correct image.
.
.
.
return ok(result);
}
在服务中,我编写了代码来保存从请求中接收到的图像。 我已从 Ajax 调用此服务,如下所示。
在我的 index.html 文件中:
var formData = new FormData();
var blob = new Blob([imageData], { type: "image/jpg"});
formData.append("file", blob, "capturedImage.jpg");
$.ajax({
url: 'http://localhost:9000/imageUpload',
data: formData,
crossDomain: true,
processData: false,
contentType: false,
async: false,
cache: false,
dataType: "json",
type: 'POST',
success: function(data){
alert(data);
var responseStr = JSON.stringify(data);
alert("Response str -- "+responseStr);
}
});
当我将文件作为多部分表单数据上传时,我可以在服务器端将其作为多部分数据接收。但它存储为“multipartBody7906599875117091855asTemporaryFile”。
如果我得到这个文件的模拟类型 - “内容/未知”。
当我调试时,我得到了以下多部分表单数据路径“/var/folders/f3/r3rfqfl949z5pf2cprwn95dm0000gn/T/multipartBody7906599875117091855asTemporaryFile”。
如何将其解析为“jpg”文件?谁能帮我做这件事?
【问题讨论】:
标签: java ajax web-services post playframework