在java中使用Restlet multipart / form-data上传文件 [英] Upload a file using Restlet multipart/form-data in java

查看:1878
本文介绍了在java中使用Restlet multipart / form-data上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我现在搜索了很多样本​​代码,但是我发现的唯一的东西就是服务器端的例子,也就是接收部分

我想创建一个应用程序,使用restlet上传文件,内容类型为: multipart / form-data 。所以我需要发送部分



如何为此创建表单?



我试过的是以下,但是不起作用:

  public void UploadFile(File f){
Form fileForm = new Form();
fileForm.add(Disposition.NAME_FILENAME,test.jpg);
处置处置=新的处置(Disposition.TYPE_INLINE,fileForm);
FileRepresentation entity = new FileRepresentation(f,MediaType.IMAGE_ALL);
entity.setDisposition(disposition);

FormData fd = new FormData(photo,entity);
FormDataSet fds = new FormDataSet();
fds.setMultipart(true);
fds.setMediaType(MediaType.MULTIPART_FORM_DATA);
fds.getEntries()。add(fd);

String url =http:// localhost / uploadFile;
可选< JsonRepresentation> opJrep = m_RestClient.postJson(url,fds,MediaType.MULTIPART_FORM_DATA,Optional.empty());






$ b

使用以下方法发布表单并接收JSON代表性(postJson意义上的帖子,让JSON回来)

  public可选< JsonRepresentation> postJson(String url,Object sendObject,MediaType mediaType,Optional< Collection< ;? extends Header>> headers){
// build resource
ClientResource resource = new ClientResource(url);

//建立请求头部
Request request = new Request(Method.POST,url);

headers.ifPresent(col-> {
request.getHeaders().addAll(col);
});

//设置请求
resource.setRequest(request);

//获取响应
resource.post(sendObject,mediaType);
表示形式responseEntity = resource.getResponseEntity();

System.out.println(responseEntity.toString());

尝试{
//获取json表示
JsonRepresentation json = new JsonRepresentation(responseEntity);
return Optional.of(json);
} catch(Exception e){
}

return Optional.empty();
}

当一切正常时,接收服务器应该返回一个JSON字符串。

Endpoint实际上不是我的本地主机,而是一个带有SendPhoto方法的Telegram Bot。在那里,你可以使用multipart / form-data发布一个文件作为图像

SendPhoto Documentation



我能做什么错?如何上传一个文件(在我的情况下,使用restlet作为一个多部分/形式的数据?)

解决方案

我做了一些尝试并使用 FormDataSet 进行错误编程,并得到结果。

上传一个文件您必须执行以下操作:

  FileRepresentation entity = new FileRepresentation(file,mediaType); //创建fileRepresentation 

FormDataSet fds = new FormDataSet(); //创建FormDataSet
FormData fd = new FormData(key,entity); //使用键和值创建Formdata(文件)
fds.getEntries()。add(fd); //将表单数据添加到集合
fds.setMultipart(true); //将多部分的值设置为true

String url =http:// localhost / uploadPhoto;
可选< JsonRepresentation> opJrep = m_RestClient.postJson(url,fds,MediaType.MULTIPART_FORM_DATA,Optional.empty());

这个例子使用相同的 postJson 在问题中描述。


So I searched quite a bit now for sample codes but the only thing I found was on examples for the server side, meaning the receiving part.

I want to create an application, that uses restlet to upload a file, content type: multipart/form-data. So I need the sending part

How do I create the form for this?

What I tried is the following, but it's not working:

public void UploadFile(File f){
    Form fileForm = new Form(); 
    fileForm.add(Disposition.NAME_FILENAME, "test.jpg");
    Disposition disposition = new Disposition(Disposition.TYPE_INLINE, fileForm); 
    FileRepresentation entity = new FileRepresentation(f, MediaType.IMAGE_ALL);  
    entity.setDisposition(disposition);

    FormData fd = new FormData("photo", entity);        
    FormDataSet fds = new FormDataSet();
    fds.setMultipart(true);
    fds.setMediaType(MediaType.MULTIPART_FORM_DATA);
    fds.getEntries().add(fd);

    String url = "http://localhost/uploadFile";
    Optional<JsonRepresentation> opJrep = m_RestClient.postJson(url,fds,MediaType.MULTIPART_FORM_DATA, Optional.empty());

}

using the following Method to post the Form and Receive a JSON Representation (postJson meaning post, get json back)

public Optional<JsonRepresentation> postJson(String url, Object sendObject,MediaType mediaType,Optional<Collection<? extends Header>> headers){
    //build resource
    ClientResource resource = new ClientResource(url);

    //build request with headers
    Request request = new Request(Method.POST,url);

    headers.ifPresent(col->{
        request.getHeaders().addAll(col);
    });

    //set request
    resource.setRequest(request);

    //get response
    resource.post(sendObject,mediaType);
    Representation responseEntity = resource.getResponseEntity();

    System.out.println(responseEntity.toString());

    try {
        //get json representation
        JsonRepresentation json = new JsonRepresentation(responseEntity);
        return Optional.of(json);
    } catch (Exception e) {
    }

    return Optional.empty();
}

The receiving server should return a JSON string, when everything is fine.

The Endpoint actually is not my localhost, but a Telegram Bot with the SendPhoto Method. There you can post a File as an image using multipart/form-data

SendPhoto Documentation

What could I do wrong? How to upload a file (image in my case) using restlet as a multipart/form-data?

解决方案

I did some try and error programming with the FormDataSet and got the result.

To upload a file (in this case picture) using restlet you have to do the following:

    FileRepresentation entity = new FileRepresentation(file, mediaType); //create the fileRepresentation  

    FormDataSet fds = new FormDataSet(); //create the FormDataSet
    FormData fd = new FormData(key, entity); //create the Formdata using a key and a value (file)       
    fds.getEntries().add(fd); //add the form data to the set
    fds.setMultipart(true); //set the multipart value to true

    String url = "http://localhost/uploadPhoto";
    Optional<JsonRepresentation> opJrep = m_RestClient.postJson(url,fds,MediaType.MULTIPART_FORM_DATA, Optional.empty());

This example uses the same postJson method as described in the question.

这篇关于在java中使用Restlet multipart / form-data上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆