上传不同名称的文件

时间:2011-11-30 04:17:03

标签: android file-upload multipart

我想上传图片&发送不同的名字。

成功发送图像,但原始名称为:

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

1 个答案:

答案 0 :(得分:0)

/ **              *生成随机数而不指定任何范围              * /

    Random rand = new Random();
            int numNoRange = rand.nextInt();
            String number = String.valueOf(numNoRange);

File imageFile =  new File(path,number+"img.jpg");

生成一个随机数并将数字附加到您的图像名称