Guest User

Untitled

a guest
Nov 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. path = ConfigurationManager.AppSettings["fileDownloaderPath"];
  2.  
  3. context.Response.Clear();
  4. context.Response.ContentType = "application/json";
  5. string isfile = "";
  6. isfile = context.Request.QueryString["isFile"];
  7.  
  8. if (isfile.Equals("true"))
  9. {
  10. string fileName = "";
  11. fileName = context.Request.QueryString["fileName"];
  12.  
  13. string sha = "";
  14. sha = context.Request.QueryString["sha"];
  15.  
  16. string fullPath = path + removeInvalidChar(fileName);
  17. log.Debug(fullPath);
  18. StreamReader sr = File.OpenText(fullPath);
  19.  
  20. string wsSha = GetSha(sr.ReadToEnd());
  21. sr.Close();
  22. context.Response.AddHeader("Content-Disposition", "attachment; filename=" + wsSha);
  23.  
  24. if (wsSha.Equals(sha))
  25. {
  26. context.Response.Write("");
  27. context.Response.Flush();
  28. }
  29. else
  30. {
  31. context.Response.WriteFile(fullPath);
  32. context.Response.Flush();
  33. }
  34. }
  35.  
  36. new DownloadFileAndAnalyzeItAsyncTask(a).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, a.getFileName());
  37.  
  38. @Override
  39. protected Void doInBackground(String... params) {
  40. Realm realm = null;
  41. newSha = null;
  42. try {
  43. realm = Realm.getDefaultInstance();
  44.  
  45. publishProgress(5);
  46. String url = String.format("%sFileDownloader.ashx?filename=%s.txt&isFile=true&cache=%s", RuntimeHelper._baseWebServiceIP, params[0], UUID.randomUUID());
  47. OkHttpClient client = new OkHttpClient.Builder()
  48. .connectTimeout(30, TimeUnit.SECONDS)
  49. .writeTimeout(60, TimeUnit.SECONDS)
  50. .readTimeout(30, TimeUnit.SECONDS)
  51. .retryOnConnectionFailure(true)
  52. .build();
  53. publishProgress(10);
  54. Request request = new Request.Builder().url(url)
  55. .addHeader("Content-Type", "application/json")
  56. .addHeader("Connection", "close")
  57. .build();
  58. publishProgress(15);
  59. Response response = client.newCall(request).execute();
  60. publishProgress(20);
  61.  
  62. ResponseBody responseBody = response.body();
  63. publishProgress(25);
  64. try {
  65. String myHeader = response.header("Contenwt-Disposition");
  66. if (myHeader != null) {
  67. int position = myHeader.indexOf("filename=");
  68. position += "filename=".length();
  69. newSha = myHeader.substring(position);
  70. }
  71. } catch (Exception ex) {
  72. newSha = null;
  73. }
  74. if (responseBody == null) {
  75. return null;
  76. }
  77.  
  78. InputStream is = responseBody.byteStream();
  79.  
  80. OutputStream outputStream = new FileOutputStream(new File(SyncActivity.this.getFilesDir(), fileName + ".txt"));
  81.  
  82. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  83. StringBuilder result;
  84. String line = reader.readLine();
  85. result = new StringBuilder(line);
  86. while ((line = reader.readLine()) != null) {
  87. result.append(line);
  88. }
  89. Log.d(TAG, result.toString());
  90. response.close();
  91.  
  92. OutputStreamWriter writer = new OutputStreamWriter(outputStream);
  93. writer.write(result.toString());
  94. writer.flush();
  95. writer.close();
  96.  
  97. outputStream.flush();
  98. outputStream.close();
  99. response.close();
  100.  
  101. Response response = client.newCall(request).execute();
Add Comment
Please, Sign In to add comment