jqueryuploadify的使用-成都快上网建站

jqueryuploadify的使用

由于项目是存储图片的,要使用到上传的功能,而且个人想做好点。所以在网上搜了下资料,觉得这个jquery 的插件还不错。

成都创新互联自2013年创立以来,先为阿巴嘎等服务建站,阿巴嘎等地企业,进行企业商务咨询服务。为阿巴嘎企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

首先放个工程的分布图

 

jquery uploadify的使用

 

项目是拿以前搭建好的SSH的框架,不过里面只用到struts2。

用到的jar的话大家自己去下载吧。jquery uploadify的版本是2.1.4的。

index.jsp下面的代码是

 

  1.  
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  3. <% 
  4.     String path = request.getContextPath(); 
  5.     String basePath = request.getScheme() + "://" + request.getServerName() + ":" 
  6.             + request.getServerPort() + path + "/"; 
  7. %> 
  8.  
  9.      
  10.         "> 
  11.         首页 
  12.      
  13.  
  14.  
  15.  
  16.  
  17.         
  18.         $(document).ready(function() { 
  19.             $("#uploadify").uploadify({ 
  20.                 'uploader'       : 'uploadify/uploadify.swf',   //flash,上传时的显示 
  21.                 'script'         : 'show.action',               //提交处理的地址,默认是php的 
  22.                 'fileDataName'   : 'file',                      //在处理的地址接收的名字 
  23.                 'cancelImg'      : 'uploadify/cancel.png',      //上传时的取消按钮 
  24.                 'folder'         : '/p_w_picpath',                    //上传放到服务器的哪个路径,(一开始的时候可以的,但是后来不知道修改了什么就在后台也要加上这个文件夹了,那样这个属性就没意思了) 
  25.                 'queueID'        : 'upLoadDiv',                 //显示附件在哪个DIV 
  26.                 'fileDesc'       : '图片文件',                  //选择文件的最后一个框框显示的提示 
  27.                 'fileExt'        : '*.jpg;*.png;*.bmp',         //只允许什么类型的文件上传 
  28.                 'sizeLimit'      : 1024*1024*1,                 //上传文件的大小byte单位的如果是在struts2上面用的话,要在struts.xml里面加上 
  29.                 'queueSizeLimit' : 2,                           //最多可以选择多少个文件 
  30.                 'auto'           : false,                       //自动上传 
  31.                 'multi'          : true,                        //多文件上传 
  32.                 'simUploadLimit' : 2,                           //一次上传多少个文件 
  33.                 'buttonText'     : 'BROWSE',                    //上传的button文字 
  34.                // 'scriptData'   :{'name':'xxx','id':'sss'},    //参数json类型 
  35.                 //'method'       :'get',                        //提交方法类型,默认post,有参数就要get             
  36.                  'onError'       :function(event,queueID,fileObj,errorObj){ 
  37.                             //一般这个的话会在上传上来文件的那个div上显示,但出来的错误是英文的 
  38.                             alert("文件:"+fileObj.name+"上传失败!失败的原因是:"+errorObj.type); 
  39.                 }, 
  40.                 'onAllComplete'  :function(event,data){//ajax返回,所有文件上传之后的回调 
  41.                     alert("一共上传:"+data.filesUploaded+"个文件,错误数:"+data.errors+".上传速度:"+data.speed+"kb/s"); 
  42.                 } 
  43.             });   
  44.         });   
  45.        
  46.     
  47.     
  48.          
   
  •           

      
  •         上传 
  •         取消所有上传 
  •     
  •  
  • struts.xml里面的代码挺少的

    struts2默认文件上传的大小是2M,大于2M的上传不了。所以我们要设置一下。

    1.  
    2.  
    3.  
    4.      
    5.      
    6.          
    7.          
    8.      
    9.  

     

    Action类里面的代码:

    导包的话大家自己导吧。

    1. public class Show { 
    2.  
    3.     private File file; // 前台传过来的文件 
    4.     private String fileFileName; // 文件名 
    5.     private String fileContentType; // 文件类型 
    6.  
    7.     public void uploadImg() throws IOException  { 
    8.         HttpServletResponse response = ServletActionContext.getResponse(); 
    9.         HttpServletRequest request = ServletActionContext.getRequest(); 
    10.         // 写到服务器上 
    11.         response.setCharacterEncoding("utf-8"); 
    12.         try { 
    13.         String path = request.getRealPath("/p_w_picpath"); 
    14.         FileInputStream in = new FileInputStream(file); 
    15.         FileOutputStream out = new FileOutputStream(new File(path + "/" + fileFileName)); 
    16.         byte[] b = new byte[1024]; 
    17.         while ((in.read(b) > -1)) { 
    18.             out.write(b); 
    19.         } 
    20.         in.close(); 
    21.         out.close(); 
    22.         response.getWriter().write("上传成功!"); 
    23.         }catch (Exception e) { 
    24.             e.printStackTrace(); 
    25.             response.getWriter().write("上传失败!请联系管理员或者重新上传!"); 
    26.         } 
    27.     } 
    28.  
    29.     public File getFile() { 
    30.         return file; 
    31.     } 
    32.  
    33.     public void setFile(File file) { 
    34.         this.file = file; 
    35.     } 
    36.  
    37.     public String getFileFileName() { 
    38.         return fileFileName; 
    39.     } 
    40.  
    41.     public void setFileFileName(String fileFileName) { 
    42.         this.fileFileName = fileFileName; 
    43.     } 
    44.  
    45.     public String getFileContentType() { 
    46.         return fileContentType; 
    47.     } 
    48.  
    49.     public void setFileContentType(String fileContentType) { 
    50.         this.fileContentType = fileContentType; 
    51.     } 

    最后发个成品的图片

     

     

    jquery uploadify的使用

     

     

    jquery uploadify的使用

     

     

    jquery uploadify的使用

     

    有什么错误的地方请大家指点一下,共同成长!


    当前标题:jqueryuploadify的使用
    本文路径:http://kswjz.com/article/gcisgc.html
    扫二维码与项目经理沟通

    我们在微信上24小时期待你的声音

    解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流