gpt4 book ai didi

java - 在 Play 框架中解析多部分请求

转载 作者:行者123 更新时间:2023-11-30 07:01:57 25 4
gpt4 key购买 nike

我正在从我的 Android 应用程序向运行 Play 框架应用程序的服务器发送视频文件和一些其他数据,我使用的是 combination multipartEntityVolley 向服务器发送 Post 请求。

我需要在我的 Controller 中解析接收到的multipartEntity 请求,并将视频存储在服务器上的一个文件夹中,并将其余数据发送到 mySQL 数据库。

多部分请求:

private static final String FILE_PART_NAME = "fileKey";

public MultipartRequest(String url, File file, Map<String, String> mStringPart,
Response.Listener<String> listener,
Response.ErrorListener errorListener)
super(Method.POST, url, errorListener);

mListener = listener;
mFilePart = file;
this.mStringPart = mStringPart;
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
buildMultipartEntity();
}


private void buildMultipartEntity() {
entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
for (Map.Entry<String, String> entry : mStringPart.entrySet()) {
entity.addTextBody(entry.getKey(), entry.getValue());
}
}

@Override
public byte[] getBody() throws AuthFailureError {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
httpentity = entity.build();
httpentity.writeTo(bos);
} catch (IOException e) {
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}

到目前为止,这是我的 Controller :

public static Result uploadFile(){

RequestBody body = request().body();
Http.MultipartFormData multippartBody = body.asMultipartFormData();

if(multippartBody != null) {
return ok("Got: " + "Hi");
}
else {
return badRequest("Expecting text/plain request body");
}
}

我想我需要某种迭代器来从 multippartBody 中提取视频和其他数据,但我不确定如何完成它

编辑

更新 Controller :

public static Result jsonPost(){

RequestBody body = request().body();
Http.MultipartFormData multippartBody = body.asMultipartFormData();
Http.MultipartFormData.FilePart imageFile = body.getFile(fileKey);
if(multippartBody != null) {
return ok("Got: " + "hi");
}
else {
return badRequest("Expecting text/plain request body");
}
}
}

我在 fileKey 上 Play 时遇到找不到符号错误?

编辑 开始工作

public static Result jsonPost(){

RequestBody body = request().body();
Http.MultipartFormData multippartBody = body.asMultipartFormData();
Http.MultipartFormData.FilePart imageFile = multippartBody.getFile("fileKey");

if(imageFile.getFile() != null) {
File file = imageFile.getFile();
String fileName = imageFile.getFilename();
File newDir = new File("/public/images/","MyApp");
if(!newDir.isDirectory()){
newDir.mkdirs();
System.out.println("Created dir");
}
if(newDir.canWrite()){
file.renameTo(new File(newDir, fileName));
}
return ok("Got: " + "it");
}
else {
return badRequest("Expecting text/plain request body");
}
}

最佳答案

检查请求 header 中的内容类型,如果是应用程序/八位字节流使用

File file = request().body().asRaw().asFile();

如果是 multipart/form-data 类型则使用

play.mvc.Http.MultipartFormData body = request().body().asMultipartFormData();

play.mvc.Http.MultipartFormData.FilePart imageFile = body.getFile(imageKey);

File file = imageFile.getFile();

关于java - 在 Play 框架中解析多部分请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29416274/

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