Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.89 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use CGI;
  5. use CGI::Carp qw ( fatalsToBrowser );
  6. use File::Basename;
  7.  
  8. $CGI::POST_MAX = 1024 * 5000 * 5000;
  9.  
  10. my $safe_filename_characters = "a-zA-Z0-9_.-";
  11.  
  12. my $upload_dir = "$ENV{DOCUMENT_ROOT}/android/videos";
  13.  
  14. my $query = new CGI;
  15.  
  16. my $filename = $query->param("upload_file");
  17. #my $param2 = $query->param("param2");
  18. #my $param3 = $query->param("param3");
  19.  
  20.  
  21.  
  22. if (!$filename) {
  23. print $query->header ( );
  24. print "ERROR MSG 123";
  25. exit;
  26. }
  27.  
  28. #prowerka za filename
  29. my ( $name, $path, $extension ) = fileparse ( $filename, '..*' );
  30. $filename = $name . $extension;
  31. $filename =~ tr/ /_/;
  32. $filename =~ s/[^$safe_filename_characters]//g;
  33. if ( $filename =~ /^([$safe_filename_characters]+)$/ ) {
  34. $filename = $1;
  35. } else {
  36. die "Filename contains invalid characters";
  37. }
  38.  
  39.  
  40. my $upload_filehandle = $query->upload("upload_file");
  41.  
  42. if (!$upload_filehandle)
  43. {
  44. die "Configuration file could not be loaded";
  45. }
  46. print $query->header;
  47. open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
  48. binmode UPLOADFILE;
  49.  
  50. while ( <$upload_filehandle> )
  51. {
  52. print UPLOADFILE;
  53. }
  54.  
  55. close UPLOADFILE;
  56.  
  57. package com.androidexample.uploadtoserver;
  58.  
  59. import java.io.DataOutputStream;
  60. import java.io.File;
  61. import java.io.FileInputStream;
  62. import java.net.HttpURLConnection;
  63. import java.net.MalformedURLException;
  64. import java.net.URL;
  65. import android.app.Activity;
  66. import android.app.ProgressDialog;
  67. import android.os.Bundle;
  68. import android.util.Log;
  69. import android.view.View;
  70. import android.view.View.OnClickListener;
  71. import android.widget.Button;
  72. import android.widget.TextView;
  73. import android.widget.Toast;
  74.  
  75. public class UploadToServer extends Activity {
  76.  
  77. TextView messageText;
  78. Button uploadButton;
  79. int serverResponseCode = 0;
  80. ProgressDialog dialog = null;
  81.  
  82. String upLoadServerUri = null;
  83.  
  84. /********** File Path *************/
  85. final String uploadFilePath = "/storage/sdcard0/DCIM/100ANDRO/";
  86. final String uploadFileName = "DSC_0003.jpg";
  87.  
  88. @Override
  89. public void onCreate(Bundle savedInstanceState) {
  90.  
  91. super.onCreate(savedInstanceState);
  92. setContentView(R.layout.activity_upload_to_server);
  93.  
  94. uploadButton = (Button)findViewById(R.id.uploadButton);
  95. messageText = (TextView)findViewById(R.id.messageText);
  96.  
  97. messageText.setText("Uploading file path :- '/storage/sdcard0/DCIM/100ANDRO/"+uploadFileName+"'");
  98.  
  99. /************* Php script path ****************/
  100. upLoadServerUri = "http://www.suntrainer.com/cgi-bin/temp/upload.pl";
  101.  
  102. uploadButton.setOnClickListener(new OnClickListener() {
  103. @Override
  104. public void onClick(View v) {
  105.  
  106. dialog = ProgressDialog.show(UploadToServer.this, "", "Uploading file...", true);
  107.  
  108. new Thread(new Runnable() {
  109. public void run() {
  110. runOnUiThread(new Runnable() {
  111. public void run() {
  112. messageText.setText("uploading started.....");
  113. }
  114. });
  115.  
  116. uploadFile(uploadFilePath + "" + uploadFileName);
  117.  
  118. }
  119. }).start();
  120. }
  121. });
  122. }
  123.  
  124. public int uploadFile(String sourceFileUri) {
  125.  
  126.  
  127. String fileName = sourceFileUri;
  128.  
  129. HttpURLConnection conn = null;
  130. DataOutputStream dos = null;
  131. String lineEnd = "rn";
  132. String twoHyphens = "--";
  133. String boundary = "*****";
  134. int bytesRead, bytesAvailable, bufferSize;
  135. byte[] buffer;
  136. int maxBufferSize = 1 * 1024 * 1024;
  137. File sourceFile = new File(sourceFileUri);
  138.  
  139. if (!sourceFile.isFile()) {
  140.  
  141. dialog.dismiss();
  142.  
  143. Log.e("uploadFile", "Source File not exist :"
  144. +uploadFilePath + "" + uploadFileName);
  145.  
  146. runOnUiThread(new Runnable() {
  147. public void run() {
  148. messageText.setText("Source File not exist :"
  149. +uploadFilePath + "" + uploadFileName);
  150. }
  151. });
  152.  
  153. return 0;
  154.  
  155. }
  156. else
  157. {
  158. try {
  159.  
  160. // open a URL connection to the Servlet
  161. FileInputStream fileInputStream = new FileInputStream(sourceFile);
  162. URL url = new URL(upLoadServerUri);
  163.  
  164. // Open a HTTP connection to the URL
  165. conn = (HttpURLConnection) url.openConnection();
  166. conn.setDoInput(true); // Allow Inputs
  167. conn.setDoOutput(true); // Allow Outputs
  168. conn.setUseCaches(false); // Don't use a Cached Copy
  169. conn.setRequestMethod("POST");
  170. conn.setRequestProperty("Connection", "Keep-Alive");
  171. conn.setRequestProperty("ENCTYPE", "multipart/form-data");
  172. conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
  173. conn.setRequestProperty("upload_file", fileName);
  174.  
  175. dos = new DataOutputStream(conn.getOutputStream());
  176.  
  177. dos.writeBytes(twoHyphens + boundary + lineEnd);
  178. dos.writeBytes("Content-Disposition: form-data; name="upload_file";filename=""
  179. + fileName + """ + lineEnd);
  180.  
  181. dos.writeBytes(lineEnd);
  182.  
  183. // create a buffer of maximum size
  184. bytesAvailable = fileInputStream.available();
  185.  
  186. bufferSize = Math.min(bytesAvailable, maxBufferSize);
  187. buffer = new byte[bufferSize];
  188.  
  189. // read file and write it into form...
  190. bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  191.  
  192. while (bytesRead > 0) {
  193.  
  194. dos.write(buffer, 0, bufferSize);
  195. bytesAvailable = fileInputStream.available();
  196. bufferSize = Math.min(bytesAvailable, maxBufferSize);
  197. bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  198.  
  199. }
  200.  
  201. // send multipart form data necesssary after file data...
  202. dos.writeBytes(lineEnd);
  203. dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  204.  
  205. // Responses from the server (code and message)
  206. serverResponseCode = conn.getResponseCode();
  207. String serverResponseMessage = conn.getResponseMessage();
  208.  
  209. Log.i("uploadFile", "HTTP Response is : "
  210. + serverResponseMessage + ": " + serverResponseCode);
  211.  
  212. if(serverResponseCode == 200){
  213.  
  214. runOnUiThread(new Runnable() {
  215. public void run() {
  216.  
  217. String msg = "File Upload Completed. nn";
  218.  
  219. messageText.setText(msg);
  220. Toast.makeText(UploadToServer.this, "File Upload Complete.",
  221. Toast.LENGTH_SHORT).show();
  222. }
  223. });
  224. }
  225.  
  226. //close the streams //
  227. fileInputStream.close();
  228. dos.flush();
  229. dos.close();
  230.  
  231. } catch (MalformedURLException ex) {
  232.  
  233. dialog.dismiss();
  234. ex.printStackTrace();
  235.  
  236. runOnUiThread(new Runnable() {
  237. public void run() {
  238. messageText.setText("MalformedURLException Exception : check script url.");
  239. Toast.makeText(UploadToServer.this, "MalformedURLException",
  240. Toast.LENGTH_SHORT).show();
  241. }
  242. });
  243.  
  244. Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
  245. } catch (Exception e) {
  246.  
  247. dialog.dismiss();
  248. e.printStackTrace();
  249.  
  250. runOnUiThread(new Runnable() {
  251. public void run() {
  252. messageText.setText("Got Exception : see logcat ");
  253. Toast.makeText(UploadToServer.this, "Got Exception : see logcat ",
  254. Toast.LENGTH_SHORT).show();
  255. }
  256. });
  257. Log.e("Upload file to server Exception", "Exception : "
  258. + e.getMessage(), e);
  259. }
  260. dialog.dismiss();
  261. return serverResponseCode;
  262.  
  263. } // End else block
  264. }
  265. }
  266.  
  267. // read file and write it into form...
  268. bufferSize = maxBufferSize;
  269. buffer = new byte[bufferSize];
  270. bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  271.  
  272. // create a buffer of maximum size
  273. bytesAvailable = fileInputStream.available();
  274.  
  275. HttpClient httpclient = new DefaultHttpClient();
  276. httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
  277.  
  278. HttpPost httppost = new HttpPost(upLoadServerUri);
  279. File file = new File(uploadFilePath + "" + uploadFileName);
  280.  
  281. MultipartEntity mpEntity = new MultipartEntity();
  282. ContentBody cbFile = new FileBody(file);
  283. mpEntity.addPart("upload_file", cbFile);
  284. StringBody contentString = new StringBody("me_again");
  285. mpEntity.addPart("new_file_name",contentString);
  286.  
  287. httppost.setEntity(mpEntity);
  288. System.out.println("executing request " + httppost.getRequestLine());
  289. HttpResponse response = httpclient.execute(httppost);
  290. HttpEntity resEntity = response.getEntity();
  291.  
  292. System.out.println(response.getStatusLine());
  293. if (resEntity != null) {
  294. System.out.println(EntityUtils.toString(resEntity));
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement