Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- path = ConfigurationManager.AppSettings["fileDownloaderPath"];
- context.Response.Clear();
- context.Response.ContentType = "application/json";
- string isfile = "";
- isfile = context.Request.QueryString["isFile"];
- if (isfile.Equals("true"))
- {
- string fileName = "";
- fileName = context.Request.QueryString["fileName"];
- string sha = "";
- sha = context.Request.QueryString["sha"];
- string fullPath = path + removeInvalidChar(fileName);
- log.Debug(fullPath);
- StreamReader sr = File.OpenText(fullPath);
- string wsSha = GetSha(sr.ReadToEnd());
- sr.Close();
- context.Response.AddHeader("Content-Disposition", "attachment; filename=" + wsSha);
- if (wsSha.Equals(sha))
- {
- context.Response.Write("");
- context.Response.Flush();
- }
- else
- {
- context.Response.WriteFile(fullPath);
- context.Response.Flush();
- }
- }
- new DownloadFileAndAnalyzeItAsyncTask(a).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, a.getFileName());
- @Override
- protected Void doInBackground(String... params) {
- Realm realm = null;
- newSha = null;
- try {
- realm = Realm.getDefaultInstance();
- publishProgress(5);
- String url = String.format("%sFileDownloader.ashx?filename=%s.txt&isFile=true&cache=%s", RuntimeHelper._baseWebServiceIP, params[0], UUID.randomUUID());
- OkHttpClient client = new OkHttpClient.Builder()
- .connectTimeout(30, TimeUnit.SECONDS)
- .writeTimeout(60, TimeUnit.SECONDS)
- .readTimeout(30, TimeUnit.SECONDS)
- .retryOnConnectionFailure(true)
- .build();
- publishProgress(10);
- Request request = new Request.Builder().url(url)
- .addHeader("Content-Type", "application/json")
- .addHeader("Connection", "close")
- .build();
- publishProgress(15);
- Response response = client.newCall(request).execute();
- publishProgress(20);
- ResponseBody responseBody = response.body();
- publishProgress(25);
- try {
- String myHeader = response.header("Contenwt-Disposition");
- if (myHeader != null) {
- int position = myHeader.indexOf("filename=");
- position += "filename=".length();
- newSha = myHeader.substring(position);
- }
- } catch (Exception ex) {
- newSha = null;
- }
- if (responseBody == null) {
- return null;
- }
- InputStream is = responseBody.byteStream();
- OutputStream outputStream = new FileOutputStream(new File(SyncActivity.this.getFilesDir(), fileName + ".txt"));
- BufferedReader reader = new BufferedReader(new InputStreamReader(is));
- StringBuilder result;
- String line = reader.readLine();
- result = new StringBuilder(line);
- while ((line = reader.readLine()) != null) {
- result.append(line);
- }
- Log.d(TAG, result.toString());
- response.close();
- OutputStreamWriter writer = new OutputStreamWriter(outputStream);
- writer.write(result.toString());
- writer.flush();
- writer.close();
- outputStream.flush();
- outputStream.close();
- response.close();
- Response response = client.newCall(request).execute();
Add Comment
Please, Sign In to add comment