phonegap捕获视频上传

//js代码
function captureVideo() {  
				navigator.device.capture.captureVideo(captureSuccess, captureError, {limit: 2});  
			} 
			//captureVideo方法执行失败后回调函数  
			function captureError(error) {  
				var msg = 'An error occurred during capture: ' + error.code;  
				navigator.notification.alert(msg, null, 'Uh oh!');  
			}  
			function captureSuccess(mediaFiles) {  
				var i, path,len;  
				alert(mediaFiles.length);
				for (i = 0, len = mediaFiles.length; i < len; i += 1) {  
					//对应的逻辑内容       
					// path = mediaFiles[i].fullPath;
					uploadVideo(mediaFiles[i]);
			}         
}  
			
function uploadVideo(mediaFile) {
        var ft = new FileTransfer(), path = mediaFile.fullPath, name = mediaFile.name;
          ft.onprogress = showUploadingProgress;
           navigator.notification.progressStart("", "当前上传进度");

        var options = new FileUploadOptions();  
        options.fileKey = "indexFile";  //文件的键值
         options.fileName = path.substr(path.lastIndexOf('/') + 1);  //文件名
        options.mimeType = "multipart/form-data";  //MIME编码
        options.chunkedMode = false;  
        var params = {};
        params.value1 = "test";
        params.value2 = "param";
        options.params = params;
        
        ft
                .upload(
                        path,
                        encodeURI("http://192.168.1.105:8080/file/VideoUploadForIndex.action"),
                        function(result) {
                            console
                                    .log('Upload success:'
                                            + result.responseCode);
                            console.log(result.bytesSent + 'bytes sent');
                             navigator.notification.progressStop(); 
                             alert("视频上传成功");
                            $(".info").text(
                                    'Upload success:' + result.responseCode
                                            + "\n" + result.bytesSent
                                            + 'bytes sent');
                        }, function(error) {
                            alert("An error has occurred: Code = " + error.code);
                            console.log("upload error source " + error.source);
                            console.log("upload error target " + error.target);
                            $(".info").text(
                                    "upload error source " + error.source + "\n"
                                            + "upload error target " + error.target);
                            console.log('Error uploading file' + path + ':'
                                    + error.code);
                        }, options);
    }

phonegap捕获视频并上传,上传的action还未实现,参数的传递方式和值等,各位大牛有想法可以给点建议~

经过研究发现,上述JS代码中

  var options = new FileUploadOptions();  
        options.fileKey = "indexFile";  //文件的键值
         options.fileName = path.substr(path.lastIndexOf('/') + 1);  //文件名
        options.mimeType = "multipart/form-data";  //MIME编码方式
        options.chunkedMode = false;  


options中的参数传到Action中
indexFile

表示需要上传的文件,文件对应的文件名则为indexFileFileName

后台Action代码如下

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.gzx.fire.action.BaseAction;

public class FileUploadForIndexAction  extends BaseAction{
	  //使用列表接收上传文件
	  private List<File> indexFile;
	  //使用列表保存多个上传文件的文件名
	  private List<String> indexFileFileName;
	  //使用列表保存多个上传文件的MIME类型
	  private List<String> indexFileContentType;
	  //保存上传文件的目录,相对于Web应用程序的根路径,在struts.xml文件中配置 // extends FileUploadAction
	  private String uploadDir;
	//保存上传文件的全路径,相对于Web应用程序的根路径,
      public String uploadPath=null;

	/**
	 * @return the indexFile
	 */
	public List<File> getIndexFile()
	{
		return indexFile;
	}

	/**
	 * @param indexFile the indexFile to set
	 */
	public void setIndexFile(List<File> indexFile)
	{
		this.indexFile = indexFile;
	}

	/**
	 * @return the indexFileFileName
	 */
	public List<String> getIndexFileFileName()
	{
		return indexFileFileName;
	}

	/**
	 * @param indexFileFileName the indexFileFileName to set
	 */
	public void setIndexFileFileName(List<String> indexFileFileName)
	{
		this.indexFileFileName = indexFileFileName;
	}

	/**
	 * @return the indexFileContentType
	 */
	public List<String> getIndexFileContentType()
	{
		return indexFileContentType;
	}

	/**
	 * @param indexFileContentType the indexFileContentType to set
	 */
	public void setIndexFileContentType(List<String> indexFileContentType)
	{
		this.indexFileContentType = indexFileContentType;
	}

		/**
	 * @return the uploadPath
	 */
	public String getUploadPath()
	{
		return uploadPath;
	}

	/**
	 * @param uploadPath the uploadPath to set
	 */
	public void setUploadPath(String uploadPath)
	{
		this.uploadPath = uploadPath;
	}

		public String getUploadDir() {
			return uploadDir;
		}
	
	  public void setUploadDir(String uploadDir)
	  {
	    this.uploadDir = uploadDir;
	  }
	
	public String uploadIndexVideo() throws Exception
	{
		System.out.println("Begin...");
		if (indexFile == null || indexFileFileName == null)//上传的文件为空
		{
			 return null;
		}
			
	    String newFileName = null;

	    //循环处理多个上传文件
	    for (int i = 0; i < indexFile.size(); i++) 
	    {
	      //得到当前时间自1970年1月1日0时0分0秒开始流逝的毫秒数,将这个毫秒数作为上传文件新的文件名。
	      SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
	    	String now = sdf.format(new Date());
	      int index = indexFileFileName.get(i).lastIndexOf('.');
      
	      //得到保存上传文件的目录的真实路径
	      String temppath = ServletActionContext.getServletContext().getRealPath(getUploadDir());
	      
	      Map session=(Map)ServletActionContext.getContext().get("session");
	      String unitId=String.valueOf(session.get("unitId"));
	      String inspectPointId=String.valueOf(session.get("dailycheckinspectPointId"));
	      
	      String time=now.substring(0,6);
	      String path=temppath+"\\"+time;

	      File dir=new File(path);
	      //如果这个目录不存在,则创建它。
	      if(!dir.exists())
		        dir.mkdir();
		        
	      newFileName = now +"_"+indexFileFileName.get(i);//文件名为"时间_原文件名"
	      
	      uploadPath="\\upload\\"+unitId+"\\"+inspectPointId+"\\"+time+"\\"+newFileName;//文件的全路径
	      session.put("uploadPath", uploadPath);
	      BufferedOutputStream bos = null;
	      BufferedInputStream bis = null;
	      //读取保存在临时目录下的上传文件,写入到新的文件中。
	      try
	      {
	        FileInputStream fis = new FileInputStream(indexFile.get(i)); 
	        bis = new BufferedInputStream(fis);

	        FileOutputStream fos = new FileOutputStream(new File(path+"\\"+newFileName));
	        bos = new BufferedOutputStream(fos);

	        byte[] buf = new byte[4096];

	        int len = -1;
	        while ((len = bis.read(buf)) != -1)
	        {
	          bos.write(buf, 0, len);
	        }
	      }
	      finally
	      {
	        try
	        {
	          if (null != bis)
	            bis.close();
	        }
	        catch (IOException e)
	        {
	          e.printStackTrace();
	        }

	        try
	        {
	          if (null != bos)
	            bos.close();
	        }
	        catch (IOException e)
	        {
	          e.printStackTrace();
	        }
	      }
	    }
	    System.out.println("upload ok.");
	    
	   
	    return "succeed";

	}


   

你可能感兴趣的:(phonegap捕获视频上传)