java jersey client_如何使用jersey-client V2.13(从客户端到服务器)以JAVA方式上传750MB文件?...

javax.ws.rs-api: 2.0.1

jersey-container-servlet: 2.13

jersey-media-multipart: 2.13

jackson: 2.4.3

I also use Apache Tomcat Server version 7.0.55.

所以我编码的下面:

/**

* On the client side.

*/

public void uploadAFile() {

Client client = ClientBuilder.newBuilder()

.register(MultiPartFeature.class)

.build();

WebTarget target = null;

try {

target = client

.target("https://blo-bla.rhcloud.com/rest")

.path("v1").path("upload");

} catch (IllegalArgumentException | NullPointerException e) {

LOG_TO_CONSOLE.fatal(e, e);

LOG_TO_FILE.fatal(e, e);

}

Builder builder = target.request(MediaType.TEXT_PLAIN);

builder.header("Authorization",

getValidBasicAuthenticationStrEncrypted());

FormDataMultiPart form = new FormDataMultiPart();

form.field("anotherParam", "Bozo");

String fileName = "/Users/drizzy/Documents/Divx/CaseDepartFolder/sample.avi";

File file = new File(fileName);

form.bodyPart(new FileDataBodyPart("file", file,

MediaType.APPLICATION_OCTET_STREAM_TYPE));

Response response = builder.post(Entity.entity(form,

MediaType.MULTIPART_FORM_DATA_TYPE));

LOG_TO_CONSOLE.debug(response.getStatus());

LOG_TO_CONSOLE.debug(response.readEntity(String.class));

}

/**

* On the server side.

*/

@POST

@Path("/upload")

@Consumes(MediaType.MULTIPART_FORM_DATA)

@Produces(MediaType.TEXT_PLAIN)

public String uploadFile(@FormDataParam("file") InputStream fileInputStream,

@FormDataParam("file") FormDataContentDisposition fileDisposition,

@FormDataParam("anotherParam") String str)

throws FileNotFoundException, IOException

{

System.out.println("str: " + str);

final String basePath = "/Users/drizzy/eclipse-workspace/tomcat7jbossews"

+ "/src/main/resources/uploads/";

final String fileName = fileDisposition.getFileName();

System.out.println(new StringBuilder().append("***** fileName ")

.append(fileName)

.toString());

final String filePath = new StringBuilder().append(basePath)

.append(fileName)

.toString();

System.out.println(filePath);

try (OutputStream fileOutputStream = new FileOutputStream(filePath)) {

int read = 0;

final byte[] bytes = new byte[1024];

while ((read = fileInputStream.read(bytes)) != -1) {

fileOutputStream.write(bytes, 0, read);

}

}

return "File Upload Successfully !!";

}

哪产生这些异常在客户端侧:

Java Heap space error

Java binding Exception: Already connected

所以我的问题是要知道是否有人可以请客户端使用jersey-client V2.13的代码示例向客户端上传大文件?甚至可以告诉我上面的代码中有什么问题?

注意:我只想使用泽西客户端版本V2.13来处理这个问题,所以请不要使用第三方库或者不使用泽西客户端版本V2.13的解决方案。

+0

你在“大文件”明白什么?包括Tomcat在内的大多数服务器都有限制(在配置中)上传时文件的大小。 –

2014-10-26 17:23:29

+0

如果服务器有限制并且您不想更改它,则会卡住。如何改变它取决于框架,而不是容器:没有一种通用技术。 –

2014-10-26 19:32:56

+0

错误的是什么?我在这里没有说错。 –

2014-10-26 20:20:32

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值