playframework Server端接受POST multipart/form-data请求

playframework Server端接受POST multipart/form-data请求,接收Client端发送的多个文件

static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    public static void getUploadFile(String file) { 
        String dataDirName=simpleDateFormat.format(new Date());
        String path="/home/Accept/"+dataDirName+"/";
        File toSave = new File(path);
        if (!toSave.exists()) {
            toSave.mkdirs();
        }
        JSONObject result = new JSONObject();
        try {
            List files = (List) request.args.get("__UPLOADS");
            for (Upload upload : files) {
                if (upload.getSize() > 0) {
                    File f = upload.asFile();
                    String fileName = f.getName();
                    File storeFile = new File(path+ fileName);
                    Logger.info("####"+path+fileName);
                    Files.copy(f, storeFile);
                }
            }
        } catch (Exception e) {
            result.put("flag", false);
            result.put("msg", "操作失败");
        }
        result.put("flag", true);
        result.put("msg", "操作成功");
        renderJSON(result);
    } 

你可能感兴趣的:(play)