剛開始解釋你我的問題之前,我想與大家分享,我使用圖書館的版本和服務器:如何使用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的解決方案。
你在「大文件」明白什麼?包括Tomcat在內的大多數服務器都有限制(在配置中)上傳時文件的大小。 – Drejc 2014-10-26 17:23:29
如果服務器有限制並且您不想更改它,則會卡住。如何改變它取決於框架,而不是容器:沒有一種通用技術。 – EJP 2014-10-26 19:32:56
錯誤的是什麼?我在這裏沒有說錯。 – EJP 2014-10-26 20:20:32