南京定制网站建设,外贸建站上海,衡水网站优化推广,做网站的项目介绍我正在尝试使用httpClient库上传图像(多部分/表单数据) . 我可以使用httpPost方法和byteArrayRequestEntity上传图像 . 以下是我使用的代码#xff1a;File file new File(imageFilePath);HttpClient client new HttpClient();PostMethod method new PostMethod(http…我正在尝试使用httpClient库上传图像(多部分/表单数据) . 我可以使用httpPost方法和byteArrayRequestEntity上传图像 . 以下是我使用的代码File file new File(imageFilePath);HttpClient client new HttpClient();PostMethod method new PostMethod(https://domain/link/folderId/files?access_tokenaccessToken);method.addRequestHeader(Content-Type,multipart/form-data;boundaryAaB03x);String boundary AaB03x;StringBuilder builder new StringBuilder();builder.append(--);builder.append(boundary\r\n);builder.append(Content-Disposition: form-data; name\file\; filename\photo.jpg\);builder.append(\r\n);builder.append(Content-Type: image/jpeg);builder.append(\r\n);builder.append(\r\n);ByteArrayOutputStream baos new ByteArrayOutputStream();baos.write(builder.toString().getBytes(utf-8));builder.setLength(0);InputStream is new FileInputStream(file);byte[] buffer new byte[4096];int nbRead is.read(buffer);while(nbRead 0) {baos.write(buffer, 0, nbRead);nbRead is.read(buffer);}is.close();builder.append(\r\n);builder.append(--);builder.append(boundary);builder.append(--);builder.append(\r\n);baos.write(builder.toString().getBytes(utf-8));method.setRequestEntity(new ByteArrayRequestEntity(baos.toByteArray(), multipart/form-data; boundary\ boundary \));System.out.println(method.getRequestEntity().toString());client.executeMethod(method);但我正在研究的项目要求我使用httpRequest而不是Http PostMethod . 我尝试使用basicHttpEntityEnclosingRequest但同样的setEntity方法只接受一个httpEntity(我使用的是ByteArrayRequestEntity) .任何人都可以帮我修改代码以便它使用HttpRequest(或其子类型)而不是PostMethod