我希望能够使用我的FTP代码上传文件,然后能够使用随机生成的名称上传文件(最好是7个字符长,只有大写和小写字母)
这是我的ftp方法:
ftpManager = [[FTPManager alloc] init];
success = NO;
NSArray* serverData = nil;
FMServer* srv = [FMServer serverWithDestination:[@"IP" stringByAppendingPathComponent:@"/"] username:@"username" password:@"password"];
srv.port = self.portField.intValue;
switch (action) {
case upload:
success = [ftpManager uploadFile:fileURL toServer:srv];
break;
default;
break;
如何编辑上传到ftp服务器的文件名?
编辑:添加'fileURL'的位置 代码:
fileURL = [NSURL URLWithString:@"/tmp/tmpscr.png"];
action = upload;
[self runAction];
答案 0 :(得分:1)
我在NSArray上打了一个像
一样洗牌的类别@interface NSArray (RandomUtils)
-(NSArray *)arrayShuffled
@end
@implementation NSArray (RandomUtils)
-(NSArray *)arrayShuffled
{
NSUInteger count = [self count];
NSMutableArray *newArray = [NSMutableArray arrayWithArray:self];
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
NSUInteger nElements = count - index;
NSUInteger n = (arc4random_uniform((u_int32_t)nElements)) + index;
[newArray exchangeObjectAtIndex:index withObjectAtIndex:n];
}];
return newArray;
}
@end
并创建可怕的名称,如:
NSMutableArray *array = [@[] mutableCopy];
for (char i ='a'; i <='z'; ++i) {
[array addObject:[NSString stringWithFormat:@"%c", i]];
}
for (char i ='A'; i <='Z'; ++i) {
[array addObject:[NSString stringWithFormat:@"%c", i]];
}
for (int i = 0; i < 10; ++i) {
[array addObject:[NSString stringWithFormat:@"%i", i]];
}
NSArray *shuffeldArray = [array arrayShuffled];
NSString* name = [[shuffeldArray subarrayWithRange:NSMakeRange(0, 7)] componentsJoinedByString:@""];
答案 1 :(得分:1)
我有类似的要求,并在NSString
上写了一个类别类别,它返回给定长度的随机字符串:
修改强>
根据@NoahWitherspoon
的建议更新为使用arc4random_uniform
+ (NSString *)randomStringOfLength:(NSInteger)length {
char str[length+1];
for (int i=0; i<length; i++){
char rand = randomChar();
str[i]=rand;
}
str[length]='\0';
return [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
}
char randomChar(){
char randomLower = 'a' + arc4random_uniform(26);
char randomUpper = 'A' + arc4random_uniform(6);
char randomNumber = '0' + arc4random_uniform(9);
int randomVal = arc4random_uniform(100);
char randomAlpha = (randomVal % 2 == 0) ? randomLower : randomUpper;
//If you don't want a numeric as part of the string, just
//return randomAlpha
return (randomVal % 3 == 0) ? randomNumber : randomAlpha;
}
答案 2 :(得分:1)
以下是我通常使用的内容:
+ (NSURL*)randomFileURLInDocumentsFolder:(NSString*)folderName pathExtension:(NSString*)extension;{
NSString* folderPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:folderName];
// check if folder exists, else create
if (folderName && ![[NSFileManager defaultManager] fileExistsAtPath:folderPath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSURL* tmp = [NSURL fileURLWithPath:[folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%f%d.%@", [[NSDate date] timeIntervalSinceReferenceDate],arc4random()%1000, extension]]];
int count = 0;
while ([[NSFileManager defaultManager] fileExistsAtPath:[tmp path]]) {
tmp = [NSURL fileURLWithPath:[folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%f%d.%@", [[NSDate date] timeIntervalSinceReferenceDate],arc4random()%1000, extension]]];
count++;
}
return tmp;
}
答案 3 :(得分:-1)
我以非常简单的方式解决了这个问题,我将向大家展示以供参考:
NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
-(NSString *) randomStringWithLength: (int) len {
NSMutableString *randomString = [NSMutableString stringWithCapacity: len];
for (int i=0; i<len; i++) {
[randomString appendFormat: @"%C", [letters characterAtIndex: arc4random_uniform([letters length]) % [letters length]]]; }
return randomString;
}
然后我用这个方法生成长度为len
的随机混音NSString* myString = [self randomStringWithLength:7];
希望这有助于每个人!!!