gpt4 book ai didi

java - 使用 Android 上传文件时如何重命名文件?

转载 作者:行者123 更新时间:2023-11-30 03:55:02 30 4
gpt4 key购买 nike

我有以下代码在工作:

      MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("userfile", new FileBody(f));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);

问题是 File f 的名称是“abc-temp.jpg”,我希望它在上传时是“xyz.jpg”。不过,我不想重命名设备上的文件,用于上传。

执行此操作的最佳方法是什么?

最佳答案

肯定有可能,我已经做到了。

                    FileBody mFileBody = new FileBody(f);
String mUploadFileName = "xyz.jpg";

FormBodyPart mFormBodyPart = new FormBodyPart("userfile", mFileBody)
{
@Override
protected void generateContentDisp(ContentBody body) {
StringBuilder buffer = new StringBuilder();
buffer.append("form-data; name=\"");
buffer.append("userfile");
buffer.append("\"");
buffer.append("; filename=\""+mUploadFileName+"\"");
addField(MIME.CONTENT_DISPOSITION, buffer.toString());
}
};
multipartContent.addPart(mFormBodyPart);

关于java - 使用 Android 上传文件时如何重命名文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13484045/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com