Guest User

Untitled

a guest
Apr 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. app.get('/', function(req, res){ //response to http server GET request at home url
  2. // when using this form to upload, no problems are experienced
  3. console.log('GET request at /');
  4. res.writeHead(200, {'Content-Type': 'text/html'});
  5. res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
  6. res.write('<input type="file" name="filetoupload"><br>');
  7. res.write('<input type="submit">');
  8. res.write('</form>');
  9. return res.end();
  10. });
  11.  
  12. app.post('/fileupload', function(req, res){ //response to http server POST request at home url
  13. console.log("POST request received at /fileupload");
  14. res.writeHead(200, {'Content-Type' : 'text/html'});
  15. var form = formidable.IncomingForm();
  16. form.uploadDir = __dirname + "/temp"
  17. form.keepExtensions = true;
  18.  
  19. form.parse(req, function(err, fields, files){
  20. console.log("Parsing...");
  21. if (err) {throw err};
  22. });
  23.  
  24. form.on('file', function(name, file){ // the 'file' event is never emitted when receiving an upload from android...
  25. console.log("Uploaded: " + file.name);
  26. });
  27.  
  28. form.on('end' , function(){
  29. console.log("Ending...");
  30. res.json();
  31. res.end();
  32. });
  33.  
  34. form.on('progress', function(bytesReceived, bytesExpected){
  35. console.log((bytesReceived/bytesExpected) * 100 + " %");
  36. })
  37.  
  38. form.on('error', function(err){
  39. console.log("There has been an error.");
  40. console.log(err);
  41. })
  42.  
  43. });
  44.  
  45. protected Boolean doInBackground(String... strings){
  46. //this asynchronously uploads a file that the user has selected
  47. // it returns true or false based on whether or not the upload was successful
  48.  
  49. for (String string: strings) { // strings is an array of strings that represents the paths of the files the user selected
  50. try{
  51. URL url = new URL(urlString); //urlString is a string that hold's the URL of the server I am hosting at 'fileupload'
  52. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  53.  
  54. connection.setConnectTimeout(4000);
  55.  
  56.  
  57. //POST request header
  58. connection.setRequestMethod("POST");
  59. connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
  60. connection.setRequestProperty("Accept-Language", "en-US, en;q=0.5");
  61.  
  62. connection.setDoOutput(true);
  63. DataOutputStream os = new DataOutputStream(connection.getOutputStream());
  64. BufferedReader outputReader = new BufferedReader(new FileReader(string));
  65.  
  66. String outputLine;
  67.  
  68. while ((outputLine = outputReader.readLine()) != null) {
  69. os.write(outputLine.getBytes());
  70. }
  71.  
  72. os.flush();
  73. os.close();
  74.  
  75. int responseCode = connection.getResponseCode();
  76. Log.i(TAG, "Response code: " + responseCode);
  77.  
  78. if (responseCode != HttpURLConnection.HTTP_OK) {
  79. return false;
  80. }
  81.  
  82. BufferedReader in = new BufferedReader(new InputStreamReader
  83. (connection.getInputStream()));
  84.  
  85. String inputLine;
  86. StringBuilder response = new StringBuilder();
  87. while ((inputLine = in.readLine()) != null) {
  88. response.append(inputLine);
  89. }
  90. in.close();
  91.  
  92. Log.i(TAG, response.toString());
  93.  
  94. connection.disconnect();
  95.  
  96. } catch (IOException e) {
  97. Log.e(TAG, e.toString());
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
Add Comment
Please, Sign In to add comment