phonegap上传及后台springmvc接收文件

在使用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;
	}
	
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值