在Play框架中解析Multipart请求

时间:2015-04-02 15:05:33

标签: java parsing playframework android-volley multipartentity

我正在将我的Android应用程序中的视频文件和其他一些数据发送到运行Play框架应用程序的服务器,我正在使用multipartEntity Volleyparse来将Post请求发送到服务器。

我需要multipartEntity我的控制器中收到的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(); } 请求,并将视频存储在服务器上的文件夹中,并将其余数据发送到mySQL数据库。

MultipartRequest:

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

我认为我需要某种迭代器来从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"); } } } 中提取视频和其他数据,但我不确定如何实现这一点

修改

更新了控制器:

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");
        }
    }

我从fileKey上的Play中找不到符号错误?

修改让它正常工作

{{1}}

1 个答案:

答案 0 :(得分:4)

检查请求标头中的内容类型,如果是application / octet-stream use

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();