【问题标题】:Setting Custom File Name in Apache HttpClient在 Apache HttpClient 中设置自定义文件名
【发布时间】:2012-02-01 09:09:55
【问题描述】:

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

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);

帮助赞赏!

谢谢, 苏雷兹

【问题讨论】:

    标签: java httpclient apache-httpcomponents


    【解决方案1】:

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

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

    【讨论】:

    • 仅供参考,该方法在最近的版本中已被弃用,但您仍然可以在构造函数中设置名称。
    猜你喜欢
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    相关资源
    最近更新 更多