`
lzj520
  • 浏览: 215256 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

080813 结合使用SWFUpload和SmartUpload

阅读更多
前台使用SWFUpload:

<%@ page  contentType="text/html;  charset=GBK"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="com.yourcompany.util.*"%>
<html>
<head>
 <link href="js/default.css" rel="stylesheet" type="text/css" />
 <script type="text/javascript" src="js/swfupload.js"></script>
 <script type="text/javascript" src="js/handlers.js"></script>
   <script src="js/jquery.js" type="text/javascript"></script>
   <script type="text/javacript">
   function g(){
  $.ajax({
    url: 'upload2.jsp',
    date: {c: $('#c').val(), c: $('#c').val()},
    error: function() { alert"fail"},
    success: function(response) { ... }
    });
  }
   </script>
 <script type="text/javascript">

 var swfu;
  window.onload = function () {
  
   swfu = new SWFUpload({
    // Backend Settings
    upload_url: "upload2.jsp", // Relative to the SWF file
    post_params: {"product1": "1"},
        
        use_query_string:false,
    // File Upload Settings
    file_size_limit : "2048", // 2MB
    file_types : "*.jpg",
    file_types_description : "JPG Images",
    file_upload_limit : "0",

    // Event Handler Settings - these functions as defined in Handlers.js
    //  The handlers are not part of SWFUpload but are part of my website and control how
    //  my website reacts to the SWFUpload events.
    file_queue_error_handler : fileQueueError,
    file_dialog_complete_handler : fileDialogComplete,
    upload_progress_handler : uploadProgress,
    upload_start_handler : uploadStart,
    upload_error_handler : uploadError,
    upload_success_handler : uploadSuccess,
    upload_complete_handler : uploadComplete,

 


    // Flash Settings
    flash_url : "js/swfupload_f9.swf", // Relative to this file

    custom_settings : {
     upload_target : "divFileProgressContainer"
    },
    
    // Debug Settings
    debug: false
   });
  }

function uploadStart(file) {
var post_params = this.settings.post_params;
post_params.product = document.getElementById("product").value;
this.setPostParams(post_params);
return true;
}
 </script>
</head>
  <div id="SWFUploadTarget">
<body>

  <div>
   <form action="upload2.jsp" method="get">
    <button id="btnBrowse" type="button" style="padding: 5px;" onClick="swfu.selectFiles(); this.blur();"><img src="images/page_white_add.png" style="padding-right: 3px; vertical-align: bottom;">请选择图片<span style="font-size: 7pt;">(2 MB Max)</span></button>
                    请输入产品ID<input type="text" name="product" id="product" value="">
   </form>
  </div>
  <div id="divFileProgressContainer" style="height: 75px;"></div>
  <div id="thumbnails"></div>
 </div>
</body>
<html>

后台使用SmartUpload:

<%@ page contentType="text/html;charset=gb2312" language="java" import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,
java.sql.*,com.jspsmart.upload.*,java.util.*,com.yourcompany.util.*"%>
<%
SmartUpload mySmartUpload =new SmartUpload();
long file_size_max=4000000;
//add


//初始化
 mySmartUpload.initialize(pageContext);
//只允许上载此类文件
try {
mySmartUpload.setAllowedFilesList("jpg,gif");
//上载文件
mySmartUpload.upload();
} catch (Exception e){
%>
  <SCRIPT language=javascript>
  alert("只允许上传.jpg和.gif类型图片文件");
  window.location=''upfile.jsp'';
  </script>
<%
}
//try{

    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
    if (myFile.isMissing()){%>
   <SCRIPT language=javascript>
   alert("请先选择要上传的文件");
   window.location=''upfile.jsp'';
   </script>
    <%}
    else{
 
 String fileName2="",ext="",testvar="";
 String productname=mySmartUpload.getRequest().getParameter("product");
 String url="/uploadfile/"+productname+"/";      //应保证在根目录中有此目录的存在


 java.io.File file1 = new java.io.File(request.getRealPath("/") + url );
   if(!file1.exists())
  {
   file1.mkdir();
  }


      //String myFileName=myFile.getFileName(); //取得上载的文件的文件名
   ext= myFile.getFileExt();      //取得后缀名
   int file_size=myFile.getSize();     //取得文件的大小 
   String saveurl="";
   if(file_size<file_size_max){

int temp=1,temp2=0;
    String[] a=myFile.getFileName().split(".jpg");
    String myFileNameo = productname+"_00_pic_big";
 String myFileName = productname+"_0"+temp;
   
    Calendar calendar = Calendar.getInstance();
    String filename = String.valueOf(calendar.getTimeInMillis());
    saveurl=request.getRealPath("/")+url;
    saveurl+=myFileNameo+"."+ext;          //保存路径
   

   
    myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);
  //  java.io.File  f1   =   new  java.io.File (saveurl);
  //  java.io.File    f2   =   new   java.io.File (request.getRealPath("/")+url+myFileName+"_0_big.jpg");  
  //  f1.renameTo(f2); 
    //out.print(filename);
//-----------------------上传完成,开始生成缩略图------------------------- 
 
 
 
 
 com.yourcompany.util.DirList dirlist = new com.pixel.util.DirList();
  String[] dir =dirlist.list(request.getRealPath("/") + url );


for(int i=0;i<dir.length;i++){
  //for (String i : dir){
  String[] split = dir[i].split("\\.");
      if(split[1].equals("jpg")){  
      String[] split2=split[0].split("\\_");
       //split2[1]=01                
   if(Integer.parseInt(split2[1])>0&Integer.parseInt(split2[1])>=temp2){
   temp2=Integer.parseInt(split2[1])+1;
   }
   if(Integer.parseInt(split2[1])==0){
   temp2=1;
   }
     }

if(temp2<=10){
myFileName = productname+"_0"+temp2;
}else{
myFileName = productname+"_"+temp2;
}

 

   
 //myFileName = mySmartUpload.getRequest().getParameter("product");
    java.io.File file = new java.io.File(saveurl);        //读入刚才上传的文件
    String newurl=request.getRealPath("/")+url+myFileName+"_70_min."+ext;  //新的缩略图保存地址
    Image src = javax.imageio.ImageIO.read(file);                     //构造Image对象
    float tagsize=70;
    int old_w=src.getWidth(null);                                     //得到源图宽
    int old_h=src.getHeight(null);  
    int new_w=0;
    int new_h=0;                            //得到源图长
    int tempsize;
    float tempdouble;
    if(old_w>old_h){
     tempdouble=old_w/tagsize;
    }else{
     tempdouble=old_h/tagsize;
    }
    new_w=Math.round(old_w/tempdouble);
    new_h=Math.round(old_h/tempdouble);//计算新图长宽
    BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);       //绘制缩小后的图
    FileOutputStream newimage=new FileOutputStream(newurl);          //输出到文件流
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);      
    encoder.encode(tag);                                               //近JPEG编码
     
            //读入刚才上传的文件
    newurl=request.getRealPath("/")+url+myFileName+"_130_min."+ext;  //新的缩略图保存地址
    //Image src = javax.imageio.ImageIO.read(file);                     //构造Image对象
    tagsize=130;
  //  int old_w2=src2.getWidth(null);                                     //得到源图宽
  //  int old_h2=src2.getHeight(null);  
   // int new_w=0;
   // int new_h=0;                            //得到源图长
   // int tempsize;
  //  float tempdouble;
    if(old_w>old_h){
     tempdouble=old_w/tagsize;
    }else{
     tempdouble=old_h/tagsize;
    }
    new_w=Math.round(old_w/tempdouble);
    new_h=Math.round(old_h/tempdouble);//计算新图长宽
     tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);       //绘制缩小后的图
    newimage=new FileOutputStream(newurl);          //输出到文件流
    encoder = JPEGCodec.createJPEGEncoder(newimage);      
    encoder.encode(tag);                                               //近JPEG编码
   
     newurl=request.getRealPath("/")+url+myFileName+"_180_min."+ext;  //新的缩略图保存地址
    //Image src = javax.imageio.ImageIO.read(file);                     //构造Image对象
    tagsize=180;
  //  int old_w2=src2.getWidth(null);                                     //得到源图宽
  //  int old_h2=src2.getHeight(null);  
   // int new_w=0;
   // int new_h=0;                            //得到源图长
   // int tempsize;
  //  float tempdouble;
    if(old_w>old_h){
     tempdouble=old_w/tagsize;
    }else{
     tempdouble=old_h/tagsize;
    }
    new_w=Math.round(old_w/tempdouble);
    new_h=Math.round(old_h/tempdouble);//计算新图长宽
     tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);       //绘制缩小后的图
    newimage=new FileOutputStream(newurl);          //输出到文件流
    encoder = JPEGCodec.createJPEGEncoder(newimage);      
    encoder.encode(tag);                                               //近JPEG编码

     newurl=request.getRealPath("/")+url+myFileName+"_500_min."+ext;  //新的缩略图保存地址
    //Image src = javax.imageio.ImageIO.read(file);                     //构造Image对象
    tagsize=500;
  //  int old_w2=src2.getWidth(null);                                     //得到源图宽
  //  int old_h2=src2.getHeight(null);  
   // int new_w=0;
   // int new_h=0;                            //得到源图长
   // int tempsize;
  //  float tempdouble;
    if(old_w>old_h){
     tempdouble=old_w/tagsize;
    }else{
     tempdouble=old_h/tagsize;
    }
    new_w=Math.round(old_w/tempdouble);
    new_h=Math.round(old_h/tempdouble);//计算新图长宽
     tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);       //绘制缩小后的图
    newimage=new FileOutputStream(newurl);          //输出到文件流
    encoder = JPEGCodec.createJPEGEncoder(newimage);      
    encoder.encode(tag);                                               //近JPEG编码

 newurl=request.getRealPath("/")+url+myFileName+"_800_min."+ext;  //新的缩略图保存地址
    //Image src = javax.imageio.ImageIO.read(file);                     //构造Image对象
    tagsize=800;
  //  int old_w2=src2.getWidth(null);                                     //得到源图宽
  //  int old_h2=src2.getHeight(null);  
   // int new_w=0;
   // int new_h=0;                            //得到源图长
   // int tempsize;
  //  float tempdouble;
    if(old_w>old_h){
     tempdouble=old_w/tagsize;
    }else{
     tempdouble=old_h/tagsize;
    }
    new_w=Math.round(old_w/tempdouble);
    new_h=Math.round(old_h/tempdouble);//计算新图长宽
     tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);       //绘制缩小后的图
    newimage=new FileOutputStream(newurl);          //输出到文件流
    encoder = JPEGCodec.createJPEGEncoder(newimage);      
    encoder.encode(tag);                                               //近JPEG编码
 
 
  newimage.close(); 

   }
   else{
    out.print("<SCRIPT language=''javascript''>");
    out.print("alert(''上传文件大小不能超过"+(file_size_max/1000)+"K'');");
    out.print("window.location=''upfile.jsp;''");
    out.print("</SCRIPT>");
   }
  }
//}catch (Exception e){

//e.toString();

//}
%>



其中:
com.yourcompany.util.DirList dirlist = new com.yourcompany.DirList();
  String[] dir =dirlist.list(request.getRealPath("/") + url );


for(int i=0;i<dir.length;i++){
  //for (String i : dir){
  String[] split = dir[i].split("\\.");
      if(split[1].equals("jpg")){  
      String[] split2=split[0].split("\\_");
       //split2[1]=01                
   if(Integer.parseInt(split2[1])>0&Integer.parseInt(split2[1])>=temp2){
   temp2=Integer.parseInt(split2[1])+1;
   }
   if(Integer.parseInt(split2[1])==0){
   temp2=1;
   }
     }

if(temp2<=10){
myFileName = productname+"_0"+temp2;
}else{
myFileName = productname+"_"+temp2;
}


这部分调用一个方法,读取目录里的文件名,并重新按照01、02、03的顺序命名。
获取目录里的文件名的类:

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;
public class DirList {
 public String[] list(String thepath) {  
  File path = new File(thepath);  
  String[] list;  
  list = path.list();  
  Arrays.sort(list, new AlphabeticComparator());
  return list;
 }
}

class DirFilter implements FilenameFilter { 
 private Pattern pattern;  public DirFilter(String regex) {
  pattern = Pattern.compile(regex);  }
 public boolean accept(File dir, String name) {  
  // Strip path information, search for regex:  
  return pattern.matcher(new File(name).getName()).matches();
  }
 }

class AlphabeticComparator implements Comparator {
 public int compare(Object o1, Object o2) {  
  String s1 = (String) o1;    String s2 = (String) o2;   
  return s1.toLowerCase().compareTo(s2.toLowerCase()); 
  }
 }

分享到:
评论

相关推荐

    swfUpload和PHP上传文件

    要使用swfUpload,你需要在项目中包含相关的JavaScript和Flash文件,并配置相关参数,如上传URL、允许的文件类型和大小等。 在PHP端,我们需要编写处理上传文件的脚本。当swfUpload将文件发送到服务器时,这个脚本...

    使用SWFUpload和fileupload简化多文件上传(附源码)

    本文将深入探讨如何使用SWFUpload和FileUpload这两个JavaScript库来简化多文件上传的过程,并提供相关的源码示例。 **SWFUpload** 是一个开源的JavaScript库,它利用Flash技术实现了一种高级的文件上传功能。这个库...

    经验总结-asp.net程序中如何使用swfupload进行大文件上传

    本人呕心沥血的经验总结,如何使用swfupload进行大文件上传,用户可以不用编写任何程序,就能实现上传,里面附有完整的使用步骤:1,2,3及笔者自行开发的类和典型应用实例,使用上非常方便。欢迎下载!

    swfupload上传demo示例

    在本文中,我们将深入探讨SwfUpload的工作原理、如何与ThinkPHP框架结合使用,以及如何处理配置和SQL文件。 SwfUpload 的工作原理: SwfUpload 使用Adobe Flash技术,提供了一个友好的用户界面,用户可以选择多个...

    SwfUpload 多文件上传

    通过结合使用SwfUpload和Ext.Net,可以构建出功能强大且用户友好的文件上传界面。 总之,SwfUpload结合Ext.Net为.NET开发者提供了一个高效、可定制的多文件上传解决方案,提高了Web应用的交互性和用户体验。通过...

    swfupload使用实例和源代码

    **SWFUpload 使用详解** SWFUpload 是一个流行的JavaScript库,它允许在Web应用程序中实现高级的文件上传功能。这个库特别有用,因为它允许用户在后台上传多个文件,无需刷新页面,提供了一种友好的用户体验。在本...

    ASP+swfUpload使用实例

    在本实例中,我们关注的是如何在ASP中整合SWFUpload工具,这是一款流行的JavaScript和Flash结合的文件上传组件。SWFUpload允许用户在后台批量上传大文件,提供了丰富的自定义选项和进度条功能,极大地提升了用户体验...

    swfupload

    标签“swfupload”明确了讨论的主题,即关于SWFUpload的使用和开发。 在压缩包中的文件"swfupload_vr6.2.applicationdemo.asp.net.2.0",很可能是SWFUpload的一个版本6.2的ASP.NET 2.0应用程序示例。这表明我们可以...

    使用SWFUpload上传文件

    尽管现代浏览器倾向于使用HTML5的File API,但SWFUpload仍然是向后兼容和处理多文件上传的一个好选择。在使用时,要确保对所有相关事件进行适当的处理,并与后端服务紧密集成,以确保整个上传流程的顺利进行。

    SWFupload

    SWFupload的工作原理是结合了JavaScript的交互性和Flash的多媒体处理能力。JavaScript负责与用户的浏览器进行交互,如显示上传进度、错误提示等,而Flash则处理实际的文件读取和上传操作。这种方式使得SWFupload可以...

    swfUpload 使用简单例子

    这个库以其强大的特性、灵活性和与Flash的结合而闻名,尤其适用于需要批量上传大文件或者处理多文件上传的场景。SwfUpload 在其核心是基于Adobe Flash技术,因此在早期,它能够利用Flash插件的优势来提供超越传统...

    swfupload实例 结合c# 包含样式和用到的js flash

    SWFUpload是一个开源的JavaScript库,它允许网页在Flash的支持下实现文件的上传功能,尤其适合处理大文件和批量上传。在这个实例中,它被与C#后端结合使用,创建了一个可以上传多张图片的功能。这个功能对于网站的...

    mvc下swfupload实例

    2. **SwfUpload 和 MVC 的结合** 在MVC中使用SwfUpload,首先需要在视图(View)中引入SwfUpload的相关脚本和CSS文件。然后,创建一个HTML表单来触发SwfUpload的初始化,设置上传参数,如URL(控制器的Action方法)、...

    web项目使用swfupload插件实现多文件上传

    SWFUpload是一个非常流行的JavaScript插件,它利用Flash技术实现了在浏览器端进行多文件上传的功能,同时提供了对上传文件格式和大小的控制。本篇文章将详细探讨如何在Web项目中使用SWFUpload插件实现这一功能。 ...

    SWFUPLOAD使用实例 (JSP)

    通过分析和运行这个示例,你可以更好地理解和掌握SWFUpload的使用方法。 总结一下,SWFUpload是一个功能强大的文件上传解决方案,特别适合在JSP环境下实现多文件无刷新上传。通过合理配置和事件处理,它可以提供...

    swfupload使用

    使用SwfUpload时,开发者需要遵循以下步骤: 1. **引入库文件**:在HTML页面中引入SwfUpload的JavaScript和SWF文件。 2. **配置参数**:设置SwfUpload的配置选项,如文件类型限制、上传URL、最大文件大小等。 3. **...

    SWFUpload_V2.2.0

    总的来说,SWFUpload_V2.2.0是一个强大的文件上传解决方案,它结合了JavaScript和Flash的优点,为Web开发人员提供了可靠且可定制的上传工具。通过学习提供的文档和样例,开发者可以快速掌握并应用到自己的项目中。

    thinkphp+swfupload上传多张图片

    尽管SwfUpload现在已经不再更新,但在过去,它因为其优秀的用户体验和性能而被广泛使用。 本教程将介绍如何在ThinkPHP框架下结合SwfUpload实现多张图片的上传功能。首先,你需要在项目中引入SwfUpload的相关文件,...

    Javascript使用SWFUpload进行多文件上传

    要使用SWFUpload实现多文件上传,首先需要引入相应的Flash文件(swfupload.swf)和JavaScript文件(swfupload.js)。接着,需要编写一个JavaScript文件,例如MultiFileHandler.js,其中包含了文件上传过程中所触发的...

Global site tag (gtag.js) - Google Analytics