Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Apache HttpClient中设置自定义文件名_Java_Httpclient_Apache Httpcomponents - Fatal编程技术网

Java 在Apache HttpClient中设置自定义文件名

Java 在Apache HttpClient中设置自定义文件名,java,httpclient,apache-httpcomponents,Java,Httpclient,Apache Httpcomponents,我正在使用Apache HttpClient通过多端口上传文件,我需要上传不同文件名的文件。。下面是我的代码 FileBody uploadFilePart = new FileBody(binaryFile); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("bin", uploadFilePart); reqEntity.addPart("comment", comment); httpPost.se

我正在使用Apache HttpClient通过多端口上传文件,我需要上传不同文件名的文件。。下面是我的代码

FileBody uploadFilePart = new FileBody(binaryFile);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", uploadFilePart);
reqEntity.addPart("comment", comment);
httpPost.setEntity(reqEntity);

HttpResponse response = httpclient.execute(httpPost);
HttpEntity resEntity = response.getEntity();

System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
    System.out.println("Response content length: " +
                       resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
谢谢你的帮助

谢谢,
Surez

您可以使用以下构造函数设置文件名

public FileBody(File file,
                String filename,
                String mimeType,
                String charset)

仅供参考,该方法在最近的版本中已被弃用,但您仍可以在构造函数中设置名称。