java - Jersey 图像上传客户端

标签 java jersey jax-rs jersey-client jersey-1.0

我正在尝试使用 Jersey webservice 上传图片,我正在使用 jersey 客户端上传图片。 下面是 Jersey 网络服务,它接受输入流并在服务器上上传图像。当我使用 jsp 多部分表单上传直接调用它时它工作正常但当我使用 jersey 客户端上传图像时失败

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) throws ServiceException
{
    // upload code
}

下面是 Jersey 客户端上传图片,如果我直接调用 jersey web 服务上传图片,客户端代码是从 php rest 客户端调用的另一个 web 服务的一部分,这个 jersey 客户端调用 jersey web 服务上传图片工作正常,但当我使用 jersey 客户端上传时它不工作。

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.setChunkedEncodingSize(1024);
WebResource wr = client
        .resource("http://localhost:8080/rest/upload");

String contentDisposition = "attachment; filename=\""
        + fileDetail.getName() + "\"";
FormDataMultiPart form = new FormDataMultiPart();
ContentDisposition contentDisposition2 = new ContentDisposition(contentDisposition);
form.setContentDisposition(contentDisposition2);
FormDataBodyPart fdp = new FormDataBodyPart("file",
        uploadedInputStream, MediaType.MULTIPART_FORM_DATA_TYPE);
form.bodyPart(fdp);
ClientResponse response = wr.type(MediaType.MULTIPART_FORM_DATA).post(
        ClientResponse.class, form)

请帮助我不确定我在这里遗漏了什么。 谢谢。

最佳答案

这是一个使用 jersey 客户端和网络服务上传图像的完整示例 客户端代码

public class Test {

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost:8080/restfullwebservice/resources/generic").build("");
    }

    public static void main(String[] args) throws FileNotFoundException {
        final ClientConfig config = new DefaultClientConfig();
        final Client client = Client.create(config);

        final WebResource resource = client.resource(getBaseURI()).path("upload");

        final File fileToUpload = new File("C:/Users/Public/Pictures/Desert.jpg");

        final FormDataMultiPart multiPart = new FormDataMultiPart();
        if (fileToUpload != null) {
            multiPart.bodyPart(new FileDataBodyPart("file", fileToUpload,
                    MediaType.APPLICATION_OCTET_STREAM_TYPE));
        }

        final ClientResponse clientResp = resource.type(
                MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class,
                multiPart);
        System.out.println("Response: " + clientResp.getClientResponseStatus());

        client.destroy();
    }
}

网络服务服务器代码

@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) throws ServiceChannelException {
    OutputStream os = null;
    try {
        File fileToUpload = new File("C:/Users/Public/Pictures/Desert1.jpg");
        os = new FileOutputStream(fileToUpload);
        byte[] b = new byte[2048];
        int length;
        while ((length = uploadedInputStream.read(b)) != -1) {
            os.write(b, 0, length);
        }
    } catch (IOException ex) {
        Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            os.close();
        } catch (IOException ex) {
            Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

完整应用 https://github.com/abdotalaat/upladeimageusingjersy

关于java - Jersey 图像上传客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27859956/

相关文章:

Java - 从 5 个 CRT 组件生成 RSA key 对

java - 聊天客户端的验收测试

java - 如何实时控制应读取的文件量

java - Jersey 的 PreProcessInterceptor

java - JAX-RS:自定义 SecurityContext 在注入(inject)资源方法时具有意外类型

java - CXF/JAX-RS : Return Custom response from interceptor

java - 运行java应用程序

java - SAML 2.0 支持 Java Jersey Web/REST 应用程序

java - 泽西不能处理大数据

java - REST 的媒体类型