在使用phonegap拍照後上傳照片的時候,我在後臺一直用request.getInputStream()去接收圖片文件,但總是拿不到,糾結了很長時間,翻閱各種資料,最終在某篇文章中發現了端倪。
因爲後臺是用springmvc搭建的,而在設計之初在spring裏面定義了文件上傳的方式是用MultipartFile來上傳,因此在用phonegap上傳,並在後臺獲取文件流一直獲取不到,最終幡然醒悟,使用MultipartFile解決了問題
一、配置MultipartFile文件:
SpringMVC 用的是 的MultipartFile來進行文件上傳 所以我們首先要配置MultipartResolver:用於處理表單中的file
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>32505856</value><!-- 上傳文件大小限制爲31M,31*1024*1024 -->
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
二 phonegap拍照上傳
//拍照
navigator.camera.getPicture(function(imageURI){
//phonegap定義上傳參數類
var options = new FileUploadOptions(); //文件參數選項
options.fileKey = "file";//向服務端傳遞的file參數的parameter name
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);//文件名,如果是從相冊選擇照片,可能選擇的文件會沒有後綴,在這裏加上+".jpg"
options.mimeType = "image/jpg";//文件格式,默認爲image/jpeg
var ft = new FileTransfer();//文件上傳類
ft.onprogress = function (progressEvt) {//顯示上傳進度條
if (progressEvt.lengthComputable) {
navigator.notification.progressValue(Math.round(( progressEvt.loaded / progressEvt.total ) * 100));
}
}
navigator.notification.progressStart("提醒", "當前上傳進度");
ft.upload(imageURI, encodeURI(pre_url+"/senyuanmespda/qc/product/uploadPhoto.do?serialNo="+serialNo), function () {
navigator.notification.progressStop();//停止進度條
navigator.notification.alert("文件上傳成功!", null, "提醒");
}, null, options,true);
}, function(message){
navigator.notification.alert("拍照失敗,原因:" + message, null, "警告");
}, {
quality: 50,
destinationType: navigator.camera.DestinationType.DATA_URI,//FILE_URI爲選擇本地照片
} );
文件傳輸插件地址
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git
http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer.git
camera插件地址
http://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git
notificaion插件地址
http://git-wip-us.apache.org/repos/asf/cordova-plugin-dialogs.git
三 java後臺接收
package com.sygroup.controller.qc.product;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.sygroup.util.Const;
import com.sygroup.util.DBUtil;
import com.sygroup.util.ReturnClientEntry;
@Controller
@RequestMapping("/qc/product")
public class ProductCheckController {
@RequestMapping(value = "/uploadPhoto",method = RequestMethod.POST)
@ResponseBody
public Object uploadPhoto(@RequestParam(value="serialNo",required=false) String serialNo,
@RequestParam("file") MultipartFile multiFile,//獲取的MultipartFile文件,參數爲'file'
HttpServletRequest request,
HttpServletResponse respone) {
ReturnClientEntry returnClientEntry = new ReturnClientEntry();
//這個是獲取配置路徑
String hostQcPath = DBUtil.getColumnByConfig("CONFIG_VALUE", "SM_CONFIG", "CONFIG_CODE='"+Const.HOST_QC_PRODUCT_PHOTO_PATH+"'");
String serialNoDir = hostQcPath+"/"+serialNo+"/";
//文件夾不存在就新建
File folder = new File(serialNoDir);
if (!folder .exists() && !folder .isDirectory()){
folder.mkdir();
}
//保存到目錄的路徑
String filePath = serialNoDir+multiFile.getOriginalFilename();
try {
//文件讀取並寫入
multiFile.transferTo(new File(filePath));
returnClientEntry.setSuccess(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
returnClientEntry.setSuccess(false);
returnClientEntry.setErrors(Tools.getErrorCauseMessage(e));
}
return returnClientEntry;
}
}