1

I have the following code working:

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

The problem is that the name of the File f is "abc-temp.jpg" and I want it to be "xyz.jpg" when I upload it. I don't want to rename the file on the device though, only for uploading.

What is the best way to go about doing this?

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194

2 Answers2

1

It is surely possible, In fact I have done it.

                    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);
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
0

I am not sure that it will work, but you can create a class which will inherit from FileBody and will override getFilename() method.

I think it should do the trick.

Victor Ronin
  • 22,758
  • 18
  • 92
  • 184