Phonegap FileTransfer 通过https协议上传文件

1、upload的第五个参数是 是否debug模式的意思,设置为true。但是如果你的https链接已经购买了SSL证书,则建议还是把debug关闭。

	var ft = new FileTransfer();
	ft.upload(fileURI, encodeURI(url), function() {
		//alert('上传成功!');
	}, function() {
		//alert('上传失败!');
	}, options,true);

我的后台是java的,采用commons-fileupload.-1.0.jar,但报错:

org.apache.commons.fileupload.FileUploadBase$UnknownSizeException: the request was rejected because it's size is unknown

phonegap的官网说FileTransfer是支持http和https两种协议的,所以我猜想是不是commons-fileupload的版本太低,尝试换了1.2.2的版本,果然可以了。

如果报:java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream 错误,请把commons-io-2.0.1.jar也配上。

附上我后台处理的代码,原本有点复杂,简化一下,如下:

	public ActionForward uploadPic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
	try{
		Hashtable files = form.getMultipartRequestHandler().getFileElements(); 
		FormFile formFile=null;
		for(Enumeration e = files.keys(); e.hasMoreElements();){
			String inputName = (String) e.nextElement();
			formFile = (FormFile)files.get(inputName);
			try {
				byte[] filebyte = formFile.getFileData();
				String fileName = formFile.getFileName();

				//保存文件,写你保存文件的逻辑
				
			} catch (FileNotFoundException e1) {
				log.error(e1.getMessage(),e1);
			} catch (IOException e1) {
				log.error(e1.getMessage(),e1);
			}
			if (formFile==null||formFile.getFileSize() <= 0) {
				continue;
			}
		}
	}catch(Exception e){
		log.error(e.getMessage(),e);
	}
		return null;
	}

记得还要给你这个Action配上ActionForm!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值