有谁知道如何使用ResteasyClient和代理框架发送请求并上传文件?
我知道用MultipartFormDataOutput发送请求的方法
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("http://.../upload");
MultipartFormDataOutput mdo = new MultipartFormDataOutput();
mdo.addFormData("file", new FileInputStream(new File("/path/to/file.txt")),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(mdo) {};
Response r = target.request().post( Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));
我想使用代理框架发送此请求,但它不起作用:(
FileUploadForm fileForm = new FileUploadForm();
//add file
ResteasyClient client = new ResteasyClientBuilder().build();
client.target(getHostUrl()).proxy(ProxyApi.class).uploadFile(fileForm);
@Path("/")
public interface ProxyApi {
@POST
@Produces(MediaType.MULTIPART_FORM_DATA)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/upload")
String uploadFile(@MultipartForm FileUploadForm form);
}
import org.jboss.resteasy.annotations.providers.multipart.PartType;
import javax.ws.rs.FormParam;
public class FileUploadForm {
@FormParam("pos")
@PartType("text/plain")
public File image;
//Get and set method
}
最佳答案
如果字段的类型是InputStream
而不是File
,则可以使用,例如:
FileUploadForm fileForm = new FileUploadForm();
fileForm.setImage(new FileInputStream("path/to/file"));
// ...
public class FileUploadForm {
@FormParam("pos")
@PartType("text/plain")
public InputStream image;
//Get and set method
}