服務器端:java 服務器端參考鏈接:http://www.cnblogs.com/fjsnail/p/3491033.html
/*
* 采用spring提供的上傳文件的方法
*/
@RequestMapping(value = "/springUpload.json", method = RequestMethod.POST , produces = "application/json;charset=UTF-8")
@ResponseBody
public String springUpload(HttpServletRequest request)
throws IllegalStateException, IOException {
System.out.println("進入方法............................");
long startTime = System.currentTimeMillis();
// 將當前上下文初始化給 CommonsMutipartResolver (多部分解析器)
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
// 檢查form中是否有enctype="multipart/form-data"
if (multipartResolver.isMultipart(request)) {
// 將request變成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 獲取multiRequest 中所有的文件名
Iterator iter = multiRequest.getFileNames();
while (iter.hasNext()) {
// 一次遍歷所有文件
MultipartFile file = multiRequest.getFile(iter.next()
.toString());
//傳遞參數(shù)值
String str = multiRequest.getParameter("str");
System.out.println("str:" + str);
if (file != null) {
System.out.print("file----");
System.out.println(file.getName());
System.out.println(file);
String path = "E:/springUpload"
+ file.getOriginalFilename();
// 上傳
file.transferTo(new File(path));
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("方法三的運行時間:" + String.valueOf(endTime - startTime)
+ "ms");
return "/success";
}
swift Alamofire上傳
func testFileUpload() {
let url = "http://192.168.0.15:8080/dcfdServer/device/springUpload.json"
let imageData = UIImagePNGRepresentation(self.image)
Alamofire.upload(multipartFormData: { (multipartFormData) in
//傳遞多個圖片峰鄙,多次添加即可
multipartFormData.append(imageData!, withName: "12.png", fileName: "image", mimeType: "image/png")
let strData = String("str")?.data(using: String.Encoding.utf8)
//傳遞參數(shù)
multipartFormData.append(strData!, withName: "str")
}, to: url) { (result) in
switch result {
case .success(request: let request, streamingFromDisk: true, streamFileURL: nil):
request.responseString(completionHandler: { (response) in
debugPrint(response)
})
case .failure(let error):
debugPrint(error)
default: break
}
}
}
OC上傳:
- (void)uploadImages:(NSArray *)images{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *dict = @{@"str": @"str....."};
NSString *url = @"http://192.168.0.15:8080/dcfdServer/device/springUpload.json";
[manager POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
for (int i = 0; i < images.count ; i++) {
UIImage *image = images[i];
NSData *data = UIImagePNGRepresentation(image);
[formData appendPartWithFileData:data name:[NSString stringWithFormat:@"%d.png",i] fileName:[NSString stringWithFormat:@"fileName%d.png",i] mimeType:@"image/png"];
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"------%f",1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"-----success-%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"-----error-%@",error);
}];
}