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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| public void uploadFile(final String sourceFileUri) {
o136=new AsyncTask<Void,Void,Void>(){protected Void doInBackground(Void... params){
try { ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
Toast.makeText(appel.this, "pas de connexion", Toast.LENGTH_LONG).show();}
else{
String fileName = sourceFileUri;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize =2 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
// dialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
// dialog.setMessage("Source File not exist :"
// +uploadFilePath + "" + uploadFileName);
Toast.makeText(appel.this, "FILE NOT E ",
Toast.LENGTH_SHORT).show();}
});
// return 0;
}
else
{
try {
// open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename="
+ fileName + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
// String serverResponseMessage = conn.getResponseMessage();
// if(serverResponseCode == 200){
// runOnUiThread(new Runnable() {
// public void run() {
// String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
// +" http://www.androidexample.com/media/uploads/"
// +uploadFileName;
// dialog.setMessage(msg);
// Toast.makeText(appel.this, "File Upload Complete.",
// Toast.LENGTH_SHORT).show();
// }
// } );
// }
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
// dialog.dismiss();
ex.printStackTrace();
Toast.makeText(appel.this, "MalformedURLException",
Toast.LENGTH_SHORT).show();
// runOnUiThread(new Runnable() {
// public void run() {
// dialog.setMessage("MalformedURLException Exception : check script url.");
// Toast.makeText(appel.this, "MalformedURLException",
// Toast.LENGTH_SHORT).show();
// }
// });
}
}}}
catch (Exception ee) {
runOnUiThread(new Runnable() {
public void run() {
dialog.setMessage("Got Exception : see logcat ");
Toast.makeText(appel.this, "Got Exception : see logcat ",
Toast.LENGTH_SHORT).show();
}
});}
return null;}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
Toast.makeText(appel.this, "pas de connexion", Toast.LENGTH_LONG).show();}
else{
if(serverResponseCode == 200){
runOnUiThread(new Runnable() {
public void run() {
// String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
// +" http://www.androidexample.com/media/uploads/"
// +uploadFileName;
// dialog.setMessage(msg);
// Toast.makeText(appel.this, "File Upload Complete.",
// Toast.LENGTH_SHORT).show();
// }
// } );
// }
// dialog.dismiss();
String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
+" http://www.androidexample.com/media/uploads/"
+uploadFileName;
// dialog.setMessage(msg);
Toast.makeText(appel.this, "File Upload Complete.",
Toast.LENGTH_SHORT).show();
}});}else{ runOnUiThread(new Runnable() {
public void run() {
//dialog.setMessage("Got Exception : see logcat ");
Toast.makeText(appel.this, "Got Exception : see logcat ",
Toast.LENGTH_SHORT).show();
}
});}
//return serverResponseCode;
} } // End else block
}.execute();} |
Partager