你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開辟中文件的上傳和下載功效的根本完成

iOS開辟中文件的上傳和下載功效的根本完成

編輯:IOS開發綜合

文件的上傳

解釋:文件上傳應用的時POST要求,平日把要上傳的數據保留在要求體中。本文引見若何不借助第三方框架完成IOS開辟中得文件上傳。

  因為進程較為龐雜,是以本文只貼出部門症結代碼。

主掌握器的症結代碼:

YYViewController.m
#import "YYViewController.h"

#define YYEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]

@interface YYViewController ()

@end


@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)upload:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType data:(NSData *)data parmas:(NSDictionary *)params
{
    // 文件上傳
    NSURL *url = [NSURL URLWithString:@"http://192.168.1.200:8080/YYServer/upload"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
   
    // 設置要求體
    NSMutableData *body = [NSMutableData data];
   
    /***************文件參數***************/
    // 參數開端的標記
    [body appendData:YYEncode(@"--YY\r\n")];
    // name : 指定參數名(必需跟辦事器端堅持分歧)
    // filename : 文件名
    NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename];
    [body appendData:YYEncode(disposition)];
    NSString *type = [NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType];
    [body appendData:YYEncode(type)];
   
    [body appendData:YYEncode(@"\r\n")];
    [body appendData:data];
    [body appendData:YYEncode(@"\r\n")];
   
    /***************通俗參數***************/
    [params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        // 參數開端的標記
        [body appendData:YYEncode(@"--YY\r\n")];
        NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key];
        [body appendData:YYEncode(disposition)];

        [body appendData:YYEncode(@"\r\n")];
        [body appendData:YYEncode(obj)];
        [body appendData:YYEncode(@"\r\n")];
    }];
   
    /***************參數停止***************/
    // YY--\r\n
    [body appendData:YYEncode(@"--YY--\r\n")];
    request.HTTPBody = body;
   
    // 設置要求頭
    // 要求體的長度
    [request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"];
    // 聲明這個POST要求是個文件上傳
    [request setValue:@"multipart/form-data; boundary=YY" forHTTPHeaderField:@"Content-Type"];
   
    // 發送要求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSLog(@"%@", dict);
        } else {
            NSLog(@"上傳掉敗");
        }
    }];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Socket 完成斷點上傳
   
    //apache-tomcat-6.0.41/conf/web.XmlRss/ target=_blank class=infotextkey>Xml 查找 文件的 mimeType
//    UIImage *image = [UIImage imageNamed:@"test"];
//    NSData *filedata = UIImagePNGRepresentation(image);
//    [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}];
   
    // 給當地文件發送一個要求
    NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:fileurl];
    NSURLResponse *repsonse = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];
   
    // 獲得mimeType
    NSLog(@"%@", repsonse.MIMEType);
    [self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{
                                                                                              @"username" : @"999",
                                                                                              @"type" : @"XmlRss/ target=_blank class=infotextkey>Xml"}];
}

@end

彌補解釋:

文件上傳要求數據格局

2015111091114601.png (654×353)

部門文件的MIMEType

2015111091158899.png (652×361)

多線程斷點下載
解釋:本文引見多線程斷點下載。項目中應用了蘋果自帶的類,完成了同時開啟多條線程下載一個較年夜的文件。由於完成進程較為龐雜,所以上面貼出完全的代碼。

完成思緒:下載開端,創立一個和要下載的文件年夜小雷同的文件(假如要下載的文件為100M,那末就在沙盒中創立一個100M的文件,然後盤算每段的下載量,開啟多條線程下載各段的數據,分離寫入對應的文件部門)。

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615443536.png (305×260)

項目頂用到的重要類以下:

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615443584.png (218×208)

完成的完成代碼以下:

主掌握器中的代碼:

#import "YYViewController.h"
#import "YYFileMultiDownloader.h"

@interface YYViewController ()
@property (nonatomic, strong) YYFileMultiDownloader *fileMultiDownloader;
@end


@implementation YYViewController
-  (YYFileMultiDownloader *)fileMultiDownloader
{
    if (!_fileMultiDownloader) {
        _fileMultiDownloader = [[YYFileMultiDownloader alloc] init];
        // 須要下載的文件長途URL
        _fileMultiDownloader.url = @"http://192.168.1.200:8080/MJServer/resources/jre.zip";
        // 文件保留到甚麼處所
        NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSString *filepath = [caches stringByAppendingPathComponent:@"jre.zip"];
        _fileMultiDownloader.destPath = filepath;
    }
    return _fileMultiDownloader;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.fileMultiDownloader start];
}

@end

自界說一個基類

YYFileDownloader.h文件
#import <Foundation/Foundation.h>

@interface YYFileDownloader : NSObject
{
    BOOL _downloading;
}
/**
 * 所須要下載文件的長途URL(銜接辦事器的途徑)
 */
@property (nonatomic, copy) NSString *url;
/**
 * 文件的存儲途徑(文件下載到甚麼處所)
 */
@property (nonatomic, copy) NSString *destPath;

/**
 * 能否正鄙人載(有無鄙人載, 只要下載器外部才曉得)
 */
@property (nonatomic, readonly, getter = isDownloading) BOOL downloading;

/**
 * 用來監聽下載進度
 */
@property (nonatomic, copy) void (^progressHandler)(double progress);

/**
 * 開端(恢復)下載
 */
- (void)start;

/**
 * 暫停下載
 */
- (void)pause;
@end

YYFileDownloader.m文件

#import "YYFileDownloader.h"

@implementation YYFileDownloader
@end

下載器類繼續自YYFileDownloader這個類

YYFileSingDownloader.h文件

#import "YYFileDownloader.h"

@interface YYFileSingleDownloader : YYFileDownloader
/**
 *  開端的地位
 */
@property (nonatomic, assign) long long begin;
/**
 *  停止的地位
 */
@property (nonatomic, assign) long long end;
@end

YYFileSingDownloader.m文件

#import "YYFileSingleDownloader.h"
@interface YYFileSingleDownloader() <NSURLConnectionDataDelegate>
/**
 * 銜接對象
 */
@property (nonatomic, strong) NSURLConnection *conn;

/**
 *  寫數據的文件句柄
 */
@property (nonatomic, strong) NSFileHandle *writeHandle;
/**
 *  以後已下載數據的長度
 */
@property (nonatomic, assign) long long currentLength;
@end


@implementation YYFileSingleDownloader

- (NSFileHandle *)writeHandle
{
    if (!_writeHandle) {
        _writeHandle = [NSFileHandle fileHandleForWritingAtPath:self.destPath];
    }
    return _writeHandle;
}

/**
 * 開端(恢復)下載
 */
- (void)start
{
    NSURL *url = [NSURL URLWithString:self.url];
    // 默許就是GET要求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 設置要求頭信息
    NSString *value = [NSString stringWithFormat:@"bytes=%lld-%lld", self.begin + self.currentLength, self.end];
    [request setValue:value forHTTPHeaderField:@"Range"];
    self.conn = [NSURLConnection connectionWithRequest:request delegate:self];
   
    _downloading = YES;
}

/**
 * 暫停下載
 */
- (void)pause
{
    [self.conn cancel];
    self.conn = nil;
   
    _downloading = NO;
}


#pragma mark - NSURLConnectionDataDelegate 署理辦法
/**
 *  1. 當接收到辦事器的呼應(連通了辦事器)就會挪用
 */
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
   
}

/**
 *  2. 當接收到辦事器的數據就會挪用(能夠會被挪用屢次, 每次挪用只會傳遞部門數據)
 */
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // 挪動到文件的尾部
    [self.writeHandle seekToFileOffset:self.begin + self.currentLength];
    // 從以後挪動的地位(文件尾部)開端寫入數據
    [self.writeHandle writeData:data];
   
    // 累加長度
    self.currentLength += data.length;
   
    // 打印下載進度
    double progress = (double)self.currentLength / (self.end - self.begin);
    if (self.progressHandler) {
        self.progressHandler(progress);
    }
}

/**
 *  3. 當辦事器的數據接收終了後就會挪用
 */
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // 清空屬性值
    self.currentLength = 0;
   
    // 封閉銜接(不再輸出數據到文件中)
    [self.writeHandle closeFile];
    self.writeHandle = nil;
}

/**
 *  要求毛病(掉敗)的時刻挪用(要求超時\斷網\沒有網, 普通指客戶端毛病)
 */
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
   
}

@end

設計多線程下載器(應用HMFileMultiDownloader能開啟多個線程同時下載一個文件)

一個多線程下載器只下載一個文件

YYFileMultiDownloader.h文件

#import "YYFileDownloader.h"

@interface YYFileMultiDownloader : YYFileDownloader
 
@end

YYFileMultiDownloader.m文件

#import "YYFileMultiDownloader.h"
#import "YYFileSingleDownloader.h"

#define YYMaxDownloadCount 4

@interface YYFileMultiDownloader()
@property (nonatomic, strong) NSMutableArray *singleDownloaders;
@property (nonatomic, assign) long long totalLength;
@end


@implementation YYFileMultiDownloader

- (void)getFilesize
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    request.HTTPMethod = @"HEAD";
   
    NSURLResponse *response = nil;
#warning 這裡要用異步要求
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    self.totalLength = response.expectedContentLength;
}

- (NSMutableArray *)singleDownloaders
{
    if (!_singleDownloaders) {
        _singleDownloaders = [NSMutableArray array];
       
        // 取得文件年夜小
        [self getFilesize];
       
        // 每條途徑的下載量
        long long size = 0;
        if (self.totalLength % YYMaxDownloadCount == 0) {
            size = self.totalLength / YYMaxDownloadCount;
        } else {
            size = self.totalLength / YYMaxDownloadCount + 1;
        }
       
        // 創立N個下載器
        for (int i = 0; i<YYMaxDownloadCount; i++) {
            YYFileSingleDownloader *singleDownloader = [[YYFileSingleDownloader alloc] init];
            singleDownloader.url = self.url;
            singleDownloader.destPath = self.destPath;
            singleDownloader.begin = i * size;
            singleDownloader.end = singleDownloader.begin + size - 1;
            singleDownloader.progressHandler = ^(double progress){
                NSLog(@"%d --- %f", i, progress);
            };
            [_singleDownloaders addObject:singleDownloader];
        }
       
        // 創立一個跟辦事器文件等年夜小的暫時文件
        [[NSFileManager defaultManager] createFileAtPath:self.destPath contents:nil attributes:nil];
       
        // 讓self.destPath文件的長度是self.totalLengt
        NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.destPath];
        [handle truncateFileAtOffset:self.totalLength];
    }
    return _singleDownloaders;
}

/**
 * 開端(恢復)下載
 */
- (void)start
{
    [self.singleDownloaders makeObjectsPerformSelector:@selector(start)];
   
    _downloading = YES;
}

/**
 * 暫停下載
 */
- (void)pause
{
    [self.singleDownloaders makeObjectsPerformSelector:@selector(pause)];
    _downloading = NO;
}

@end

彌補解釋:若何取得將要下載的文件的年夜小?

https://www.ios5.online/ios/UploadFiles_8070/201703/2017031615443567.png (802×205)

【iOS開辟中文件的上傳和下載功效的根本完成】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved