java ios压缩图片,Java、ios图片上传
IOS客服端代碼
@interface ViewController ()
{
NSString *boundary;
NSString *fileParam;
NSString *baseUrl;
NSString *fileName;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
boundary = @"----------V2ymHFg03ehbqgZCaKO6jy";
fileParam = @"file";
baseUrl = @"http://url/from/server";
fileName = @"image.png";//此文件提前放在可讀寫區(qū)域
}
//請(qǐng)求方法
-(void)method4{
NSURL *uploadURL;
//文件路徑處理(隨意)
NSLog(@"請(qǐng)求路徑為%@",uploadURL);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
//body
NSData *body = [self prepareDataForUpload];
//request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:uploadURL];
[request setHTTPMethod:@"POST"];
// 以下2行是關(guān)鍵,NSURLSessionUploadTask不會(huì)自動(dòng)添加Content-Type頭
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
NSString *message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"message: %@", message);
[session invalidateAndCancel];
}];
[uploadTask resume];
});
}
//生成bodyData
-(NSData*) prepareDataForUpload
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *uploadFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];//將圖片放在了documents中??
NSString *lastPathfileName = [uploadFilePath lastPathComponent];
NSMutableData *body = [NSMutableData data];
NSData *dataOfFile = [[NSData alloc] initWithContentsOfFile:uploadFilePath];
if (dataOfFile) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", fileParam, lastPathfileName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/zip\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:dataOfFile];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
return body;
}
Java服務(wù)器端代碼
//創(chuàng)建一個(gè)臨時(shí)文件存放要上傳的文件,第一個(gè)參數(shù)為上傳文件大小,第二個(gè)參數(shù)為存放的臨時(shí)目錄
DiskFileItemFactory factory = new DiskFileItemFactory(1024*1024*5,new File("D:\\temp1"));
// 設(shè)置緩沖區(qū)大小為 5M
factory.setSizeThreshold(1024 * 1024 * 5);
// 創(chuàng)建一個(gè)文件上傳的句柄
ServletFileUpload upload = new ServletFileUpload(factory);
//設(shè)置上傳文件的整個(gè)大小和上傳的單個(gè)文件大小
upload.setSizeMax(1024*1024*50);
upload.setFileSizeMax(1024*1024*5);
String[] fileExts = {"doc","zip","rar","jpg","txt"};
try { //把頁面表單中的每一個(gè)表單元素解析成一個(gè)
FileItem List items = upload.parseRequest(request);
for (FileItem fileItem : items) {
//如果是一個(gè)普通的表單元素(type不是file的表單元素)
if(fileItem.isFormField()){
System.out.println(fileItem.getFieldName());
//得到對(duì)應(yīng)表單元素的名字
System.out.println(fileItem.getString());
// 得到表單元素的值
}else{ //獲取文件的后綴名
String fileName = fileItem.getName();//得到文件的名字
String fileExt = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
if(Arrays.binarySearch(fileExts, fileExt)!=-1){
try { //將文件上傳到項(xiàng)目的upload目錄并命名,getRealPath可以得到該web項(xiàng)目下包含/upload的絕對(duì)路徑//
fileItem.write(new File(request.getServletContext().getRealPath("/upload")+"/" + UUID.randomUUID().toString()+"."+fileExt));
fileItem.write(new File("D:/test2.png"));
logger.info("文件上傳路徑:"+request.getServletContext().getRealPath("/upload")+"/" + UUID.randomUUID().toString()+"."+fileExt);
} catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("該文件類型不能夠上傳");
}
}
}
} catch (FileUploadBase.SizeLimitExceededException e) {
System.out.println("整個(gè)請(qǐng)求的大小超過了規(guī)定的大小...");
} catch (FileUploadBase.FileSizeLimitExceededException e) {
System.out.println("請(qǐng)求中一個(gè)上傳文件的大小超過了規(guī)定的大小...");
}catch (FileUploadException e) {
e.printStackTrace();
}
總結(jié)
以上是生活随笔為你收集整理的java ios压缩图片,Java、ios图片上传的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机动画制作 教学设计,《设置动画效果
- 下一篇: 整合php和mysql lnmp,安装配