做app 开发避免不了上传文件,图片等存到服务器上,客户端只是实现单纯的上传图片,还有传入其他的参数(待更新)。
首先来看客户端的代码
1.获取本地图片
<pre><code>
func saveImage(currentImage:UIImage,imageName:NSString){
var imageData:NSData = UIImageJPEGRepresentation(currentImage, 0.5)
var fullPath:String = NSHomeDirectory().stringByAppendingPathComponent("Documents").stringByAppendingPathComponent(imageName as String)
imageData.writeToFile(fullPath as String, atomically: false)
var fileURL = NSURL(fileURLWithPath: fullPath)
//开始上传操作
}
</code></pre>
-
上传图片
<pre><code>
/**
上传照片:param: url 上传url
:param: path 图片path
**/
static func uploadImage(url:String,parameter:String,imagePath:NSURL ,requestDelegate:RequestDelegate){
Alamofire.upload(
.POST,
URLString: url,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(fileURL: imagePath, name: "file")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { request, response, JSON, error in
println(request)
println(response)
println(error)
println(JSON)
}
case .Failure(let encodingError):
println(encodingError)
}
}
)}
</code></pre>
接下来我们看Java服务器端代码
1.配置文件
maven 的pom.xml 配置
spring 配置
2.在controller 获取文件
<pre><code>
@RequestMapping(value = "/updateProfile.do")
@ResponseBodypublic JSONObject updateProfile(HttpServletRequest request) throws IOException{
if(request instanceof MultipartHttpServletRequest){
MultipartHttpServletRequest multipartHttpServletRequest=(MultipartHttpServletRequest) request;
FileUtils.saveFile(request, multipartHttpServletRequest.getFile("file"))
}
</code></pre>
3.保存文件 FileUtils 类
<pre><code>
package com.ambimmort.app.framework.uitls;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.stylesheets.LinkStyle;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**文件操作utils * * Created by Darker on 15/5/11. */
public class FileUtils {
private static String filePath;
private static ArrayList<String> filePaths=new ArrayList<>();
public static boolean saveFile(HttpServletRequest request,MultipartFile file)
{ // 判断文件是否为空
if (!file.isEmpty()) {
try {
String localFilePath=null;
filePath= "/uploads/" +UUID.randomUUID()+"_"+System.currentTimeMillis()+".png"; // 文件保存路径
localFilePath =request.getSession().getServletContext().getRealPath("/")+filePath;
System.out.println("filepath="+filePath);
// 转存文件
file.transferTo(new File(localFilePath));
setFilePath(filePath);
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
public static boolean saveFiles(HttpServletRequest request,Map<String,MultipartFile> files)
{ for (int i=0;i<files.size();i++){
//有一张图片没上传成功,则return false
MultipartFile multipartFile=files.get("file"+i);
if (saveFile(request,multipartFile)){
filePaths.add(getFilePath());
}else{
return false;
}
}
return true;
}
public static String getFilePath(){
return filePath ;
}
public static ArrayList<String> getFilePaths(){
return filePaths;
}
public static void setFilePath(String path){
filePath=path;
}
}
</code></pre>