我有以下代码在工作:
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/