1 | final long fileSize = sourceFile.length();
|
2 | // open a URL connection to the Servlet
|
3 | FileInputStream fileInputStream = new FileInputStream(sourceFile);
|
4 | URL url = new URL(upLoadServerUri);
|
5 | Authenticator.setDefault (new Authenticator() {
|
6 | protected PasswordAuthentication getPasswordAuthentication() {
|
7 | return new PasswordAuthentication ("user", "pw".toCharArray());
|
8 | }
|
9 | });
|
10 |
|
11 | long sentBytes = 0;
|
12 | HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
|
13 | SSLContext context = SSLContext.getInstance("TLS");
|
14 | context.init(null, new X509TrustManager[]{new NullX509TrustManager()}, new SecureRandom());
|
15 | HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
|
16 |
|
17 | // Open a HTTP connection to the URL
|
18 | conn = (HttpURLConnection) url.openConnection();
|
19 | conn.setDoInput(true); // Allow Inputs
|
20 | conn.setDoOutput(true); // Allow Outputs
|
21 |
|
22 | conn.setUseCaches(false); // Don't use a Cached Copy
|
23 | conn.setRequestMethod("POST");
|
24 | conn.setRequestProperty("Connection", "Keep-Alive");
|
25 | conn.setRequestProperty("ENCTYPE", "multipart/form-data");
|
26 | conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
|
27 | conn.setRequestProperty("uploaded_file", fileName);
|
28 |
|
29 | dos = new DataOutputStream(conn.getOutputStream());
|
30 |
|
31 | dos.writeBytes(twoHyphens + boundary + lineEnd);
|
32 | dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd);
|
33 |
|
34 | dos.writeBytes(lineEnd);
|
35 |
|
36 | // create a buffer of maximum size
|
37 | bytesAvailable = fileInputStream.available();
|
38 |
|
39 | bufferSize = Math.min(bytesAvailable, maxBufferSize);
|
40 | buffer = new byte[bufferSize];
|
41 |
|
42 | // read file and write it into form...
|
43 | bytesRead = fileInputStream.read(buffer, 0, bufferSize);
|
44 |
|
45 | while (bytesRead > 0) {
|
46 | dos.write(buffer, 0, bufferSize);
|
47 | bytesAvailable = fileInputStream.available();
|
48 | bufferSize = Math.min(bytesAvailable, maxBufferSize);
|
49 | bytesRead = fileInputStream.read(buffer, 0, bufferSize);
|
50 | sentBytes += bytesRead;
|
51 | progressBar(fileSize,sentBytes);
|
52 | }
|
53 | // send multipart form data necesssary after file data...
|
54 | dos.writeBytes(lineEnd);
|
55 | dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
56 |
|
57 | // Responses from the server (code and message)
|
58 | int serverResponseCode = conn.getResponseCode();
|
59 | String serverResponseMessage = conn.getResponseMessage();
|
60 |
|
61 | //Log.i(TAG, "HTTP Response is : "
|
62 | // + serverResponseMessage + ": " + serverResponseCode);
|
63 |
|
64 | if(serverResponseCode == 200)
|
65 | {
|
66 | //
|
67 | }
|
68 |
|
69 | //close the streams //
|
70 | fileInputStream.close();
|
71 | dos.flush();
|
72 | dos.close();
|