热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

phonegap捕获视频上传

js代码functioncaptureVideo(){navigator.device.capture.captureVideo(captureSuccess,captureError,
//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 //对应的逻辑内容
// 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 indexFile;
//使用列表保存多个上传文件的文件名
private List indexFileFileName;
//使用列表保存多个上传文件的MIME类型
private List indexFileContentType;
//保存上传文件的目录,相对于Web应用程序的根路径,在struts.xml文件中配置 // extends FileUploadAction
private String uploadDir;
//保存上传文件的全路径,相对于Web应用程序的根路径,
public String uploadPath=null;

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

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

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

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

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

/**
* @param indexFileContentType the indexFileContentType to set
*/
public void setIndexFileContentType(List 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 {
//得到当前时间自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";

}

   

推荐阅读
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • WebSocket与Socket.io的理解
    WebSocketprotocol是HTML5一种新的协议。它的最大特点就是,服务器可以主动向客户端推送信息,客户端也可以主动向服务器发送信息,是真正的双向平等对话,属于服务器推送 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
  • 合并列值-合并为一列问题需求:createtabletab(Aint,Bint,Cint)inserttabselect1,2,3unionallsel ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • 本文分析了Wince程序内存和存储内存的分布及作用。Wince内存包括系统内存、对象存储和程序内存,其中系统内存占用了一部分SDRAM,而剩下的30M为程序内存和存储内存。对象存储是嵌入式wince操作系统中的一个新概念,常用于消费电子设备中。此外,文章还介绍了主电源和后备电池在操作系统中的作用。 ... [详细]
  • 使用freemaker生成Java代码的步骤及示例代码
    本文介绍了使用freemaker这个jar包生成Java代码的步骤,通过提前编辑好的模板,可以避免写重复代码。首先需要在springboot的pom.xml文件中加入freemaker的依赖包。然后编写模板,定义要生成的Java类的属性和方法。最后编写生成代码的类,通过加载模板文件和数据模型,生成Java代码文件。本文提供了示例代码,并展示了文件目录结构。 ... [详细]
author-avatar
此号我已不再用
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有