做app 開發(fā)避免不了上傳文件魄懂,圖片等存到服務(wù)器上,客戶端只是實(shí)現(xiàn)單純的上傳圖片充蓝,還有傳入其他的參數(shù)(待更新)芽突。
首先來看客戶端的代碼
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服務(wù)器端代碼
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);
// 轉(zhuǎn)存文件
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>