-- Not confirmed --
confirmed just 1 file parameter.
public static void sendMultipart(HttpURLConnection connection, String[] filefield, File[] filepath, Map<String, String> textdata) throws IOException {
final String twoHyphens = "--";
final String boundary = "*****"+ UUID.randomUUID().toString()+"*****";
final String lineEnd = "\r\n";
final int maxBufferSize = 1024*1024*3;
DataOutputStream outputStream;
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
// connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
if(filefield != null) {
for(int i=0;i<filefield.length;i++){
String fileKey = filefield[i];
File filePath = filepath[i];
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + fileKey + "\"; filename=\"" + filepath[i].getName() + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: application/octet-stream" + lineEnd);
outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
outputStream.writeBytes(lineEnd);
FileInputStream fileInputStream = new FileInputStream(filepath[i]);
int bytesAvailable = fileInputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
}
}else{
}
for (Map.Entry<String, String> entry : textdata.entrySet()) {
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: text/plain"+lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(entry.getValue());
outputStream.writeBytes(lineEnd);
}
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
outputStream.close();
}
https://qiita.com/informationsea/items/778d9525c3aaded73577
댓글
댓글 쓰기