Search code examples
androidfile-uploadmultipart

Upload file with different name


I want to upload images & send with different names.

Send images successfully but with the original name:

httpPostuplod = new HttpPost(URL);
MultipartEntity multiPart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

for (int i = 0; i < imglist.size(); i++) {      
    System.out.println(imglist.get(i));
    File file = new File(imglist.get(i));

    // multiPart.addPart("IMG_"+Constants_Fix.postImgcount+".png", new FileBody(new File(imglist.get(i))));
    ContentBody cbFile = new FileBody(file, "image/png");

    multiPart.addPart("IMG_"+Constants_Fix.postImgcount+".png",cbFile);

    Constants_Fix.postImgcount++;
}

httpPostuplod.setEntity(multiPart);

HttpResponse resuplod = httpclient.execute(httpPostuplod);

Solution

  • /** * Generated Random Number without specifying any range */

        Random rand = new Random();
                int numNoRange = rand.nextInt();
                String number = String.valueOf(numNoRange);
    
    File imageFile =  new File(path,number+"img.jpg");
    

    generate a Random Number and append the number to your image name