HTTP一次发送同个文件夹下的多文件
针对这种上传
/**
*
* @param url 图片上传地址
* @param map 需要上传的文件路径集合
* @param name 表格的key
* @return ApiResult
*
* @throws IOException
*/
public static String uploadFiles(String url, Map<String,String> map,String name ,String params) throws IOException {
String boundary = UUID.randomUUID().toString(); // 边界标识 随机生成
String prefix = "--", end = "\r\n";
URL urlGet = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlGet.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", DEFAULT_USER_AGENT);
conn.setRequestProperty("Charsert", "UTF-8");
// 定义数据分隔线
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
/**
* 当文件不为空,把文件包装并且上传
*/
OutputStream out = new DataOutputStream(conn.getOutputStream());
// 定义最后数据分隔线
// 这里就是多张图片的地址啥的
Set<String> keySet = map.keySet();
//遍历存放所有key的Set集合
Iterator<String> it =keySet.iterator();
while(it.hasNext()){ //利用了Iterator迭代器**
//得到每一个key
String key = it.next();
//通过key获取对应的value
String value = map.get(key);
System.out.println(key+"="+value);
File file = new File(value);
StringBuffer sb = new StringBuffer();
sb.append(prefix).append(boundary).append(end);
/**
* 这里重点注意: name里面的值为服务器端需要key 只有这个key 才可以得到对应的文件
* filename是文件的名字,包含后缀名的 比如:abc.png
*/
sb.append("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + file.getName() + "\"" + end);
sb.append("Content-Type: image/png");
sb.append(end).append(end);
byte[] mediaDatas = sb.toString().getBytes();
out.write(mediaDatas);
InputStream is = new FileInputStream(file);
byte[] bytes = new byte[8192];//8k
int len = 0;
while ((len = is.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
IOUtils.closeQuietly(is);
out.write("\r\n".getBytes());
}
if (TextUtil.isNotEmpty(params)) {
StringBuilder paramData = new StringBuilder();
paramData.append("--").append(boundary).append("\r\n");
paramData.append("Content-Disposition: form-data;name=\"description\";");
byte[] paramDatas = paramData.toString().getBytes();
out.write(paramDatas);
out.write(params.getBytes(Charset.forName("UTF-8")));
}
byte[] end_data = ("\r\n--" + boundary + "--\r\n").getBytes();
out.write(end_data);
out.flush();
IOUtils.closeQuietly(out);
// 定义BufferedReader输入流来读取URL的响应
InputStream in = conn.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
String valueString = null;
StringBuffer bufferRes = null;
bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null){
bufferRes.append(valueString);
}
IOUtils.closeQuietly(in);
// 关闭连接
if (conn != null) {
conn.disconnect();
}
return bufferRes.toString();
}
调用:
List<To> list = FileTo.getFileTo();
Map<String, String> map = new HashMap<>();
for (HLWaterFileTo hlWaterFileTo : list) {//上传扫描的材料
for (int i = 0; i < FileTo.getImgData().length; i++) {
String imgbase64 = FileTo.getImgData()[i];
String pngnamestr = "change"+"_"+i+"_" + UUID.randomUUID().toString();
boolean a = Base64Util.Base64ToImage(imgbase64, imgFilePath + "\\" + pngnamestr + ".png");
System.out.println("生成png" + a + ":" + imgFilePath + "\\" + pngnamestr + ".png");
String filePath =imgFilePath + "\\" + pngnamestr + ".png" ;
map.put("filePath"+i, filePath);
}
}
//toDo
//http一次上传多张图片
String fileId = HttpUtil.uploadFiles(url, map, "files", "");