A long time ago, the andriod part of this article and the server part, the recent paper on the way, so wrote a few lines of simple code as follows.
Take the afnetwork for a moment. The specific code is as follows:
-(void) download{nsurlsessionconfiguration *config = [Nsurlsessionconfiguration defaultsessionconfiguration]; Afurlsessionmanager *manager = [[Afurlsessionmanager alloc] initwithsessionconfiguration:config]; NSString *urlstring = @ "Http:192.168.0.179:8080/myweb/download.do"; URLString = [URLString stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; Nsurl *url = [Nsurl urlwithstring:urlstring]; Nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; Nsurlsessiondownloadtask *task = [Manager downloadtaskwithrequest:request progress:nil Destination:^nsurl * (NSURL * TargetPath, Nsurlresponse *response) {//Specify the path saved by the download file//NSLog (@ "%@%@", TargetPath, Response.suggestedfilena ME); Save the download file in the cache path NSString *cachedir = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0]; NSString *retstr = [Response.suggestedfilename stringbyreplacingpercentescapesusingencoding:nSutf8stringencoding]; NSLog (@ "%@ \ n", retstr); NSString *path = [Cachedir stringbyappendingpathcomponent:retstr]; Nsurl *fileurl = [Nsurl Fileurlwithpath:path]; return FileURL; } completionhandler:^ (Nsurlresponse *response, Nsurl *filepath, Nserror *error) {NSLog (@ "%@%@", FilePath, error); }]; [Task resume];}1. Create a request using Nsurlrequest.
2. Use
Nsurlsessiondownloadtask
To create a task.
3. File name you want to use, name on the server, call
Response.suggestedfilename
Can
4. The file name contains Chinese garbled characters, the server side of the last code needs to be modified as follows, using Urlencoder:
Modify http://blog.csdn.net/nothingl3/article/details/44409319 (has been modified)
String Filename=file.getname ();
Note that the Urlencoder encode method is used to encode the file name Utf-8
String enfilename = Urlencoder.encode (FileName, "utf-8");
System.out.println (Enfilename);
Browser download
Response.AddHeader ("Content-disposition", "attachment;filename=" + enfilename);
5.iOS just need to encode the URL---"Utf-8:
NSString *retstr = [Response.suggestedfilename stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
IOS File Download (afnetwork three-party framework file name in the text characters garbled problem solved) four