Java 如何在Spring引导Rest控制器中接收从Jersey客户端发送的文件?
我有一个Jersey客户端(版本1.19),需要将文件发送到服务器进行处理。这是我的密码:Java 如何在Spring引导Rest控制器中接收从Jersey客户端发送的文件?,java,spring-boot,jersey,Java,Spring Boot,Jersey,我有一个Jersey客户端(版本1.19),需要将文件发送到服务器进行处理。这是我的密码: @POST @Path("/sendFile") @Consumes({MediaType.APPLICATION_JSON}) @Produces("text/plain") public Response sendFile(AsanRestModel msg) { log.warn(("inside send
@POST
@Path("/sendFile")
@Consumes({MediaType.APPLICATION_JSON})
@Produces("text/plain")
public Response sendFile(AsanRestModel msg) {
log.warn(("inside send file"));
String attId = msg.getMessage();
String path = msg.getPath();
File file = new File(path);
final ClientConfig config = new DefaultClientConfig();
final Client client = Client.create(config);
final WebResource resource = client.resource("http://localhost:8888/file");
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE);
fileDataBodyPart.setContentDisposition(FormDataContentDisposition.name("file").fileName(file.getName()).build());
final MultiPart request = new FormDataMultiPart()
.field("filename", file.getName(), MediaType.TEXT_PLAIN_TYPE)
.bodyPart(fileDataBodyPart);
request.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
ClientResponse response = resource.type("multipart/form-data").post(ClientResponse.class, request);
return Response.ok(file.getName()).build();
}
我不确定它是否正确发送了文件,但我需要在另一个Spring Boot应用程序Rest控制器中接收它。在该应用程序中是否可以不使用Jersey?我试着用Jersey接收,但不起作用:
@RestController
public class ReceiveSendFileController {
@POST
@Path("/file")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(final MimeMultipart file) {
System.out.println("in upload file");
if (file == null)
return Response.status(Response.Status.BAD_REQUEST)
.entity("Must supply a valid file").build();
try {
for (int i = 0; i < file.getCount(); i++) {
System.out.println("Body Part: " + file.getBodyPart(i));
}
return Response.ok("Done").build();
} catch (final Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e)
.build();
}
}
}
@RestController
公共类ReceiveSendFileController{
@职位
@路径(“/file”)
@生成(MediaType.TEXT\u PLAIN)
@使用(MediaType.MULTIPART\u FORM\u数据)
公共响应上载文件(最终MimeMultipart文件){
System.out.println(“上传文件中”);
if(file==null)
返回Response.status(Response.status.BAD_请求)
.entity(“必须提供有效文件”).build();
试一试{
对于(int i=0;i
有人能帮我做这件事吗?多谢各位