1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
public class MultipartUtils {
private final String boundary = "*****";
private static final String LINE_FEED = "\r\n";
private HttpURLConnection httpConn;
private String charset;
private OutputStream outputStream;
private DataOutputStream writer;
private ArrayList<NameValuePair> params;
private String lineEnd = "\r\n";
private boolean firstHeader = true;
/**
* This constructor initializes a new HTTP POST request with content type
* is set to multipart/form-data
*
* @param requestURL
* @param charset
* @throws IOException
*/
public MultipartUtils(String requestURL, String charset, String wsse)
throws IOException {
this.charset = charset;
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("Connection", "Keep-Alive");
// httpConn.setRequestProperty("ENCTYPE", "multipart/form-data");
httpConn.setRequestProperty("User-Agent", "CodeJava Agent");
httpConn.setRequestProperty("x-wsse", wsse);
outputStream = httpConn.getOutputStream();
writer = new DataOutputStream(outputStream);
writer.writeBytes("--" + boundary + lineEnd);
}
/**
* Adds a form field to the request
*
* @param name field name
* @param value field value
*/
public void addFormField(String name, String value) {
try {
writer.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"" + lineEnd);
writer.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
writer.writeBytes("Content-Length: " + value.length() + lineEnd);
writer.writeBytes(lineEnd);
writer.writeBytes(value); // mobile_no is String variable
writer.writeBytes(lineEnd);
writer.writeBytes("--" + boundary + lineEnd);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Adds a upload file section to the request
*
* @param fieldName name attribute in <input type="file" name="..." />
* @param uploadFile a File to be uploaded
* @throws IOException
*/
public void addFilePart(String fieldName, File uploadFile)
throws IOException {
String fileName = uploadFile.getName();
FileInputStream fileInputStream = new FileInputStream(uploadFile);
int maxBufferSize = 3 * 1024 * 1024;
byte[] buffer;
writer.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
writer.writeBytes(lineEnd);
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
writer.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
/*
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
*/
}
/**
* Completes the request and receives response from the server.
*
* @return a list of Strings as response in case the server returned
* status OK, otherwise an exception is thrown.
* @throws IOException
*/
public List<String> finish() throws IOException {
List<String> response = new ArrayList<String>();
// writer.append(LINE_FEED).flush();
// writer.append("--" + boundary + "--").append(LINE_FEED);
writer.writeBytes(lineEnd);
writer.writeBytes("--" + boundary + "--" + lineEnd);
writer.close();
// checks server's status code first
int status = httpConn.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
Log.d("MultipartUtils", "Server returned OK");
Log.d("MultipartUtils", "message de retour : " + httpConn.getResponseMessage());
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();
httpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status + "with message : " + httpConn.getResponseMessage());
}
return response;
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
String result = new String();
String twoHyphens = "--";
String boundary = "*****";
for (NameValuePair pair : params)
{
result += "Content-Disposition: form-data; name=\""+pair.getName()+"\"" + lineEnd;
result += lineEnd;
result += pair.getValue();
result += lineEnd;
result += twoHyphens + boundary + lineEnd;
}
return result.toString();
}
} |
Partager