android中小弟我要上传6张图片给后台,底下代码是上传一张的,小弟我要如何改才能上传6张,新人一枚求大神教教小弟我,多谢

android中我要上传6张图片给后台,底下代码是上传一张的,我要怎么改才能上传6张,新人一枚求大神教教我,谢谢!
本帖最后由 cst11041 于 2015-09-15 14:27:14 编辑
 private Context mcontext;// 调用此方法的activity名称

    /**
     * 将文件上传到服务器自定义的目录下
     *
     * @param context  调用此方法的activity名称
     * @param file     要上传的文件
     * @param savepath 服务器文件的存放路径 例如?? savepath="/img"); 目录请按示例格式要求??
     */
    public String UploadFile(Context context, File file, String savepath, Map<String, String> params, String requestUrl) {
        System.out.println("cropfile--" + file);
        System.out.println("savepath--" + savepath);
        this.mcontext = context;
        Log.i(mcontext.toString(), "upload file");
        try {
            // 上传文件
            FormFile formfile = new FormFile(file.getName(), file, "file", "application/octet-stream");
//            String str = SocketHttpRequester.post(requestUrl, params, formfile, "UTF-8");
            String str = HttpUploadFile(requestUrl, params, formfile);
            return str;
        } catch (Exception e) {

            return "";
        }
    }

    String HttpUploadFile(String requestUrl, Map<String, String> params, FormFile file) throws Exception {
        return HttpUploadFile(requestUrl, new FormFile[]{file}, params);
    }
------解决思路----------------------
一张一张的上传呗!
------解决思路----------------------
就只用上传,还需要获取返回来的值么?
------解决思路----------------------
以数组的形式。不过,服务器那边得支持才行。
图片上传就是拼一个form表单,再进行提交罢了。
以下是我自己写的拼接表单的方法:

public void writeFile(String name, String mimeType, File file)
throws java.io.IOException {
if (file == null) {
throw new IllegalArgumentException("File cannot be null.");
}
if (!file.exists()) {
throw new IllegalArgumentException("File does not exist.");
}
if (file.isDirectory()) {
throw new IllegalArgumentException("File cannot be a directory.");
}
/*
 * //文件上传限制 if (file.length() > 60000000) {// 60000KB throw new
 * IllegalArgumentException("File too BIG."); }
 */

writeFile(name, mimeType, file.getCanonicalPath(), new FileInputStream(
file));
}

private void writeFile(String name, String mimeType, String fileName,
InputStream is) throws java.io.IOException {
if (is == null) {
throw new IllegalArgumentException("Input stream cannot be null.");
}
if (fileName == null 
------解决思路----------------------
 fileName.length() == 0) {
throw new IllegalArgumentException(
"File name cannot be null or empty.");
}
/*
 * --boundary\r\n Content-Disposition: form-data; name="<fieldName>";
 * filename="<filename>"\r\n Content-Type: <mime-type>\r\n \r\n
 * <file-data>\r\n
 */
// write boundary
out.writeBytes(PREFIX);
out.writeBytes(boundary);
out.writeBytes(NEWLINE);
// write content header
out.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"; filename=\"" + URLEncoder.encode(fileName, "utf-8")
+ "\"");
// out.writeBytes("Charset:utf-8");
out.writeBytes(NEWLINE);
if (mimeType != null) {
out.writeBytes("Content-Type: " + mimeType);
out.writeBytes(NEWLINE);
}
out.writeBytes(NEWLINE);

// write content
byte[] data = new byte[1024];
int r = 0;
try {
while ((r = is.read(data, 0, data.length)) != -1) {
out.write(data, 0, r);
}
// close input stream, but ignore any possible exception for it

is.close();
} catch (Exception e) {
e.printStackTrace();
}
out.writeBytes(NEWLINE);
out.flush();
}


调用时:

for (int i = 0; i < allFiles.size(); i++) {
String file = allFiles.get(i);
writeFile("file_array_name[]", "*/*", new File(file));
}

------解决思路----------------------
以上请求头中使用的常量:
private static final String NEWLINE = "\r\n";
private static final String PREFIX = "--";
private static String boundary = "Anything-Android";//此处字符串任意