小编hue*_*ami的帖子

Jersey 2:发送包含信息和文件的多部分

我想编写一个 Java REST 客户端,上传包含一些信息的 json 文件。Web 服务的描述指出必须使用 MultiPart。不幸的是,我在网上找到的解决方案并不适合我。

网络服务描述指出:

  • 多部分
    • ContentDispositionHeader: 表单数据名称 = "\ObjectInfo"\
      • 文件类型记录 ID:字符串
      • 由用户创建更新:字符串
      • 实际文件名:字符串
    • ContentDispositionHeader:表单数据名称=文件名
      • 文件多部分数据

我的代码如下:

    Client client = ClientBuilder.newBuilder()
        .register(MultiPartFeature.class)
        .build();
    WebTarget target = client.target(Uri);  

    MultivaluedMap<String, String> info = new MultivaluedHashMap<String, String>();
    info.add("FileTypeRecId", scheduleGuid);
    info.add("CreatedUpdatedBy", user);
    info.add("actualFileName", scheduleFileName);
    Gson g = new Gson();
    String infoString = g.toJson(info, MultivaluedMap.class);

    try {
        (new BufferedWriter(new FileWriter(scheduleFileName))).write(scheduleJson);
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileDataBodyPart fileDataBodyPart = new FileDataBodyPart();
    fileDataBodyPart.contentDisposition(FormDataContentDisposition.name(scheduleFileName).build());
    fileDataBodyPart.setFileEntity(new File(scheduleFileName), MediaType.APPLICATION_JSON_TYPE);

    MultiPart multiPart = new MultiPart(); …
Run Code Online (Sandbox Code Playgroud)

rest client multipart jersey-2.0

5
推荐指数
0
解决办法
1276
查看次数

标签 统计

client ×1

jersey-2.0 ×1

multipart ×1

rest ×1