简体   繁体   中英

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

/** * 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM