jquerymobile 下面 form 表单提交 和普通html没区别,最主要是 <form 要加一个 data-ajax='false' 否则 上传会失败

1  html代码

<!doctype html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1" charset="UTF-8">
    <link  rel="stylesheet" type="text/css" href="jquerymobile1.4.0-green/zms-green.css"/>
    <link rel="stylesheet" href="jquerymobile1.4.0-green/jQuery.mobile.icons.min.css" />
    <link rel="stylesheet" href="jquerymobile1.4.0-green/jquery.mobile.structure-1.4.0.css" />

    <link rel="stylesheet" href="css/my.css" />

    <script src="commond-plug/jquery.min.js" type="text/JavaScript"></script>
    <script src="jquerymobile1.4.0-green/jquery.mobile-1.4.0.min.js" type="text/javascript"></script>
    <title>HTML-ZMS</title>
    <script>
        $(document).ready(function () {

        });
    </script>

    <stytle>

    </stytle>
</head>

<body>

<div data-role="page">

    <div data-role="header" data-position="fixed" style="background: #ff6932;color: #ffffff;text-shadow: none;">
        <h1>Jquery mobile 1.4</h1>
        <!--   <a href="#" class="ui-btn">返回</a>-->
    </div>

    <div   class="ui-content">
        <div class="file-box">
            <form action="../servlet/phonegapUp" method="post" enctype="multipart/form-data"  data-ajax="false">

                <input type="text" id="zms" name="zms">
                <input type="text" id="value1" name="value1">
                <input type="text" id="value2" name="value2">

                <input type="file"   accept="image/png" name="fileField" id="fileField"  />

                <input type="submit" name="submit" class="btn" value="上22传" />
            </form>
        </div>
    </div>

    <div data-role="footer" data-position="fixed" style="background: #ff6932;color: #ffffff;text-shadow: none;">
        <h4>中兴长天(南昌)信息技术有限公司</h4>
    </div>

</div>

</body>
</html>

2  服务端代码  新建一个servlet,修改 dopost代码

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");    
        Date date = new Date();//获取当前时间    
        SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");    
        //SimpleDateFormat sdfFolder = new SimpleDateFormat("yyMM");    
        String newfileName = sdfFileName.format(date);//文件名称    
        String fileRealPath = "";//文件存放真实地址    
        
        String firstFileName="";    
        
        // 获得容器中上传文件夹所在的物理路径    如果按日期存放,则可以在files\\后面继续加 sdFolder+"\\"
        String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "files\\";    
       /* System.out.println("路径" + savePath+"; name:"+name);    */
        System.out.println("路径" + savePath);    
        File file = new File(savePath);    
        if (!file.isDirectory()) {    
            file.mkdirs();    
        }    
    
        try {    
            DiskFileItemFactory fac = new DiskFileItemFactory();    
            ServletFileUpload upload = new ServletFileUpload(fac);    
            upload.setHeaderEncoding("UTF-8");    
            // 获取多个上传文件    
            List fileList = fileList = upload.parseRequest(request);    
            // 遍历上传文件写入磁盘    
            Iterator it = fileList.iterator();    
            while (it.hasNext()) {    
            FileItem obit = (FileItem)it.next();  
                //如果是普通  表单参数
             
                if(obit.isFormField()){ //普通域,获取页面参数
                    String field = obit.getFieldName();
                    
                 if(field.equals("value1"))
                 {
                System.out.println(obit.getString("UTF-8"));
                      
                 }
                 else if(field.equals("value2")){
                 
                System.out.println(obit.getString("UTF-8"));
                     
                 }
                }
                // 如果是 多媒体
               
                if(obit instanceof DiskFileItem){  
                    DiskFileItem item = (DiskFileItem) obit;    
                    // 如果item是文件上传表单域       
                    // 获得文件名及路径       
                    String fileName = item.getName();    
                    if (fileName != null) {    
                        firstFileName=item.getName().substring(item.getName().lastIndexOf("\\")+1);    
                        String formatName = firstFileName.substring(firstFileName.lastIndexOf("."));//获取文件后缀名    
                        fileRealPath = savePath + newfileName + formatName;//文件存放真实地址    
                            
                        BufferedInputStream in = new BufferedInputStream(item.getInputStream());// 获得文件输入流    
                        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(fileRealPath)));// 获得文件输出流    
                        Streams.copy(in, outStream, true);// 开始把文件写到你指定的上传文件夹    
                        //上传成功, 
                        if (new File(fileRealPath).exists()) {    
                            //虚拟路径赋值    
                           // fileRealResistPath=sdfFolder.format(date)+"/"+fileRealPath.substring(fileRealPath.lastIndexOf("\\")+1);    
                            //保存到数据库    
                            System.out.println("上传成功了, 您还可以做其他操作");    
                            
                            //System.out.println("虚拟路径:"+fileRealResistPath);   
                            response.getWriter().write(fileRealPath.substring(fileRealPath.lastIndexOf("\\")+1)); 
                        }    
                             
                    }     
                }  
            }     
        } catch (org.apache.commons.fileupload.FileUploadException ex) {  
           ex.printStackTrace();    
           System.out.println("没有上传文件");    
           return;    
        }     
     /*  response.getWriter().write("1");    */
}

data-ajax="false" 是重点,终于解决了

jquery mobile 表单提交 图片/文件 上传的更多相关文章

  1. node07---post请求、表单提交、文件上传

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 【ASP.NET Web API教程】5.3 发送HTML表单数据:文件上传与多部分MIME

    原文:[ASP.NET Web API教程]5.3 发送HTML表单数据:文件上传与多部分MIME 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面 ...

  3. JavaScript实现form表单的多文件上传

    form表单的多文件上传,具体内容如下 formData对象可以使用一系列的键值对来模拟一个完整的表单,然后使用Ajax来发送这个表单 使用<form>表单初始化FormData对象的方式 ...

  4. 基于hi-nginx的web开发(python篇)——表单处理和文件上传

    hi-nginx会自动处理表单,所以,在hi.py框架里,要做的就是直接使用这些数据. 表单数据一般用GET和POST方法提交.hi-nginx会把这些数据解析出来,放在form成员变量里.对pyth ...

  5. (27) java web的struts2框架的使用-基于表单的多文件上传

    和单个文件上传配置都是一样的,只是在action中接受参数时候,接受的是数组,不再是单个的文件. 一,action的实现: public class MutableFilesUpload extend ...

  6. (转)WebApi发送HTML表单数据:文件上传与多部分MIME

    5.3 Sending HTML Form Data5.3 发送HTML表单数据(2) 本文引自:http://www.cnblogs.com/r01cn/archive/2012/12/20/282 ...

  7. WebApi发送HTML表单数据:文件上传与多部分MIME

    5.3 Sending HTML Form Data5.3 发送HTML表单数据(2) 本文引自:http://www.cnblogs.com/r01cn/archive/2012/12/20/282 ...

  8. 学习SpringMVC必知必会(7)~springmvc的数据校验、表单标签、文件上传和下载

    输入校验是 Web 开发任务之一,在 SpringMVC 中有两种方式可以实现,分别是使用 Spring 自带的验证 框架和使用 JSR 303 实现, 也称之为 spring-validator 和 ...

  9. form表单系列中文件上传及预览

    文件上传及预览 Form提交 Ajax 上传文件 时机: 如果发送的[文件]:->iframe, jQurey(),伪Ajax 预览 import os img_path = os.path.j ...

随机推荐

  1. 算法精解:最小二乘法C实现

    计量经济学研究的直接目的是确定总体回归函数Yi=B1+B2Xi+ui,然而能够得到的只是来自总体的若干样本的观测值,要用样本信息建立的样本回归函数尽可能"接近"地去估计总体回归函数 ...

  2. 如何编写51单片机超声波测距SR04_lcd1602显示程序

    超声波测距在我们日常生活中很常见,比如说车在倒退的时候,为了防止车撞到障碍物,会在车尾加上一个超声波测距模块.在智能车比赛中,也有超声波测距模块等等.可见超声波非常的重要,接下来,我们上代码研究一下如 ...

  3. TCP/IP概述

    1) 链路层,有时也称作数据链路层或网络接口层,通常包括操作系统中的设备驱动程序和计算机中对应的网络接口卡.它们一起处理与电缆(或其他任何传输媒介)的物理接口细节. 2) 网络层,有时也称作互联网层, ...

  4. Cocos2d-swift V3.x 中的update方法

    在cocos2d V3.x中update方法如果实现,则会被自动调用;不用向早期的版本那样要显式schedule. 但是你还是要显式schedule其他方法或blocks使用node的schedule ...

  5. 介绍一种很棒的wince 如何替换系统声音的方法

    Topic:介绍一种很棒的wince 如何替换系统声音的方法(作者:Baiduluckyboy) //------------------------------------------------- ...

  6. objc直接通过指针访问对象实例变量

    我们现在来做一件被认为是very bad的事情,如题所示:无论实例变量是私有的.保护的都可以通过地址访问到,并且还可以修改之.这可以称之为所谓的"超级键值编码". 首先上代码: # ...

  7. java解决hash算法冲突

    看了ConcurrentHashMap的实现, 使用的是拉链法. 虽然我们不希望发生冲突,但实际上发生冲突的可能性仍是存在的.当关键字值域远大于哈希表的长度,而且事先并不知道关键字的具体取值时.冲突就 ...

  8. MongoDB学习笔记(二)

    一.Mongodb命令 说明:Mongodb命令是区分大小写的,使用的命名规则是驼峰命名法. 对于database和collection无需主动创建,在插入数据时,如果database和collect ...

  9. CALayer简介

    一.什么是CALayer * 在iOS系统中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. * 其实UIView之所以 ...

  10. java main方法执行sql语句

    public static void main(String[] args) throws Exception{ String driver = "oracle.jdbc.driver.Or ...