/** * @Declaration : - (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest * *)request @Description : Creates a download task for the specified URL request and saves the * results to a file. * * @param request An NSURLRequest object that provides the URL, cache policy, request type, body * data or body stream, and so on. * @return Return Value The new session download task. @Discussion After you create the task, you * must start it by calling its resume method. */ public DownloadTask downloadTaskWithRequest(NSURLRequest request) { NSURL nsURL = request.URL(); URL url = nsURL.getUrl(); downloadTask = new DownloadTask(url); downloadTasks.add(downloadTask); return downloadTask; }
/** * @Declaration : - (NSURLSessionDownloadTask *)downloadTaskWithRequest:(NSURLRequest *)request * completionHandler:(void (^)(NSURL *location, NSURLResponse *response, NSError * *error))completionHandler @Description : Creates a download task for the specified URL request, * saves the results to a file, and calls a handler upon completion. * * @param request An NSURLRequest object that provides the URL, cache policy, request type, body * data or body stream, and so on. * @param completionHandler The completion handler to call when the load request is complete. This * handler is executed on the delegate queue. Unless you have provided a custom delegate, this * parameter must not be nil, because there is no other way to retrieve the response data. * @return Return Value The new session download task. @Discussion After you create the task, you * must start it by calling its resume method. */ public DownloadTask downloadTaskWithRequestCompletionHandler( NSURLRequest request, PerformBlock.VoidBlockNSURLNSURLResponseNSError completionHandler) { NSURL nsURL = request.URL(); URL url = nsURL.getUrl(); downloadTask = new DownloadTask(url); downloadTasks.add(downloadTask); return downloadTask; }
public void uploadFile() throws IOException { String fileName = url.path().toString(); DataOutputStream dos = null; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; FileInputStream fileInputStream = new FileInputStream(fileName); // Open a HTTP connection to the URL request = (HttpURLConnection) url.getUrl().openConnection(); request.setDoInput(true); // Allow Inputs request.setDoOutput(true); // Allow Outputs request.setUseCaches(false); // Don't use a Cached Copy request.setChunkedStreamingMode(1024); request.setRequestMethod("POST"); request.setRequestProperty("requestection", "Keep-Alive"); request.setRequestProperty("ENCTYPE", "multipart/form-data"); request.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); request.setRequestProperty("uploaded_file", fileName); dos = new DataOutputStream(request.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes( "Content-Disposition: form-data; name=\"file\";filename=\"" + fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); // create a buffer of maximum size bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // close the streams // fileInputStream.close(); dos.flush(); dos.close(); }