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

081102 struts2 + swfupload 批量上传图片

阅读更多
upload.jsp:
在这个文件里,使用了新swfload的上传方式,将文件流发送到struts2的doMultipleUploadUsingList.action,uploadStart的这个function,是将页面上的"id"元素的值一起带过去,上传过程中,会在divStatus的DIV中显示上传进度条。

<%@ page  contentType="text/html;  charset=utf-8"%>
<html>
<head>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <link href="<%=request.getContextPath() %>/admin/uploadpic/js/default.css" rel="stylesheet" type="text/css" />
 <script type="text/javascript" src="<%=request.getContextPath() %>/admin/uploadpic/js/swfupload.js"></script>
 <script type="text/javascript" src="<%=request.getContextPath() %>/admin/uploadpic/js/handlers.js"></script>
 <script type="text/javascript" src="<%=request.getContextPath() %>/admin/uploadpic/js/fileprogress.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath() %>/admin/uploadpic/js/swfupload.queue.js"></script>
 <script type="text/javascript">
  var swfu;
  window.onload = function () { 
   var settings = {
    // Backend Settings
    file_post_name : "upload",
    upload_url: "<%=request.getContextPath() %>/admin/product/doMultipleUploadUsingList.action", // Relative to the SWF file
    post_params: {"product1": "1"},        
          use_query_string:false,
    // File Upload Settings
    file_size_limit : "100 MB",
    file_types : "*.*",
    file_types_description : "All Files",
    file_upload_limit : 100,
    file_queue_limit : 0,
    custom_settings : {
     progressTarget : "fsUploadProgress",
     cancelButtonId : "btnCancel"
    },
    debug: false,

    // The event handler functions are defined in handlers.js
    file_queued_handler : fileQueued,
    file_queue_error_handler : fileQueueError,
    file_dialog_complete_handler : fileDialogComplete,
    upload_start_handler : uploadStart,
    upload_progress_handler : uploadProgress,
    upload_error_handler : uploadError,
    upload_success_handler : uploadSuccess,
    upload_complete_handler : uploadComplete,
    queue_complete_handler : queueComplete,
    // Flash Settings
    flash_url : "<%=request.getContextPath() %>/admin/uploadpic/js/swfupload_f8.swf" // Relative to this file
    
   };
   swfu = new SWFUpload(settings);
  }
   function uploadStart(file) {
   document.getElementById("back").innerHTML='';
   var post_params = this.settings.post_params;
   post_params.id = document.getElementById("id").value;
   this.setPostParams(post_params);
   return true;
   }           
   <style type="text/css">
   #content button,input,span{
   margin: 5 5 5 5;
   }
   #back{
   width:810;
   height:500;
   float:left;
   text-align:center;
   vertical-align:middle;
   overflow:auto;
   }
   #img2{
   float:left;
   margin: 1 1 1 1;
   text-align:center;
   vertical-align:middle;
   display: table-cell;
   display: block;
   font-size: 68px;
   width:78;
   height:78;
   border: 1px solid #B10000;
   }
   #img2 img{
   vertical-align:middle;
   cursor: pointer;
   }
   #img2 img hover{
   cursor: pointer;
   }  
</style>
</head>
  <div id="SWFUploadTarget" >
<body>

<div id="content">
  <span>1、Product ID</span><input type="text" name="id" id="id" value= <%=request.getParameter("id") %> readonly>
  <button id="btnBrowse" type="button" style="padding: 6px;" onClick="swfu.selectFiles(); this.blur();javascript:document.getElementById('divStatus').innerHTML='';">
  </div>
 </div>
    <p>
   <fieldset class="flash" id="fsUploadProgress">
   <legend>Upload Progress</legend>
   </fieldset>
  <div id="divStatus"></div>
   <div>
    <input id="btnCancel" type="button" value="Cancel All Upload" onClick="swfu.cancelQueue();" disabled="disabled" style="font-size: 8pt;" />

   </div>

<div id="back"></div>
</body>
<html>

MultipleFileUploadUsingListAction是一个可以通用的struts2 action,可以接收批量或单个上传过来的图片。并且可以选择生成相应压缩图。图片生成的命名方式是从xxx_01、xxx_02、xxx_03一直自动排列下去。压缩图为xxx_01_70

package com..web.action;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.web.commons.util.DirList;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

@SuppressWarnings("serial")
public class MultipleFileUploadUsingListAction extends ActionSupport {

    private String id;
    private File[] uploads;
    private String[] uploadFileNames;
    private String[] uploadContentTypes;
   
    public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public File[] getUpload() { return this.uploads; }
    public void setUpload(File[] upload) { this.uploads = upload; }

    public String[] getUploadFileName() { return this.uploadFileNames; }
    public void setUploadFileName(String[] uploadFileName) { this.uploadFileNames = uploadFileName; }

    public String[] getUploadContentType() { return this.uploadContentTypes; }
    public void setUploadContentType(String[] uploadContentType) { this.uploadContentTypes = uploadContentType; }   
 
  public String upload() throws Exception{
   try{
  String productname=id;
  String url = ServletActionContext.getServletContext().getRealPath("Personalizedphoto")+"\\"+id+"\\";
   for (int i=0;i<uploads.length;i++) {
  FileInputStream fis=new FileInputStream(uploads[i]); 
    if(!(new File(url).isDirectory())) 
    { 
    new File(url).mkdir();  
    }
   int temp=1,temp2=1;
   String myFileName = productname+"_0"+temp;
   DirList dirlist = new DirList();
   String[] dir =dirlist.list(url);  
   for(int j=0;j<dir.length;j++){
      String[] split = dir[j].split("\\.");
          if(split[1].equals("jpg")&&split[0].split("\\_").length==3){  
          String[] split2=split[0].split("\\_");
       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;
    }
    
    FileOutputStream fos=new FileOutputStream(url+myFileName+"_800.jpg");  
  byte[] buffer=new byte[1024];
     int len=0;
     while((len=fis.read(buffer))>0){
         fos.write(buffer, 0, len);
     }

     java.io.File file = new java.io.File(url+myFileName+"_800.jpg");
     String newurl=url+myFileName+"_70.jpg";  
     java.awt.Image src = javax.imageio.ImageIO.read(new java.io.File(url+myFileName+"_800.jpg"));
    
     float tagsize=70;
     int old_w=src.getWidth(null);                             
     int old_h=src.getHeight(null);
     int new_w=0;
     int new_h=0;                           
     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);     java.awt.image.BufferedImage tag = new java.awt.image.BufferedImage(new_w,new_h,java.awt.image.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);                                               
     newurl=url+myFileName+"_130.jpg";  
     tagsize=130;
     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 java.awt.image.BufferedImage(new_w,new_h,java.awt.image.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); 
     newurl=url+myFileName+"_180.jpg";  
     tagsize=180;
     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 java.awt.image.BufferedImage(new_w,new_h,java.awt.image.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);                                               
     newurl=url+myFileName+"_500.jpg";  
     tagsize=500;
     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 java.awt.image.BufferedImage(new_w,new_h,java.awt.image.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);  
     newimage.close();
   } 
   }catch(Exception e){}
   this.setId(id);
   return SUCCESS;
 }
    
 }


这个action还调用了一个类DirList ,是用于扫描当前文件夹里的图片,并且根据当前的命名情况来命名新上传的图片,如原来已经有了10张,那上传之后的就从第11开始命名。

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;
 }
 
 private void deleteFile(File file){
  if(file.exists()){
  if(file.isFile()){
  file.delete();
  }else if(file.isDirectory()){
  File files[] = file.listFiles();
  for(int i=0;i<files.length;i++){
  this.deleteFile(files[i]);
  }
  }
  file.delete();
  }else{
  }
  }
}

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()); 
  }
 }


 

分享到:
评论
1 楼 wxq594808632 2009-07-08  
发个demo..看看效果

相关推荐

    struts2+swfupload实现大文件多文件上传

    Struts2和SwfUpload是两个在Web开发中用于处理用户交互的重要工具,它们结合使用可以实现高效的大文件和多文件上传功能。Struts2是一个基于MVC(Model-View-Controller)架构的Java框架,它提供了强大的控制层支持,...

    SWFUpload批量上传图片

    这个"SWFUpload批量上传图片"的项目是一个基于MyEclipse的工程实例,旨在帮助开发者理解和实现批量上传图片的功能。下面我们将深入探讨SWFUpload的工作原理、主要特性以及如何在MyEclipse环境中进行集成和应用。 一...

    struts1+swfuploadv2.2.0.1文件批量上传

    struts1+swfuploadv2.2.0.1文件批量上传 测试过成功 用的最新版本的 swfuploadv2.2.0.1,现在已经出了2.5版本的,但是还是测试版本的,可能会不稳定,所以我做了swfuploadv2.2.0.1这个版本的,大文件上传,90MB的文件...

    SWFupload_struts1.rar_java 批量上传_struts1批量下载_swfupload struts1

    标题 "SWFupload_struts1.rar_java 批量上传_struts1批量下载_swfupload struts1" 提供的信息表明,这是一个与Java编程语言、Struts1框架以及SWFUpload工具相关的项目或教程。SWFUpload是一个流行的老牌JavaScript和...

    swfupload批量上传文件

    在本项目中,我们看到“swfupload批量上传文件”这一标题,表明这是一个利用SWFUpload实现批量文件上传的解决方案。SWFUpload允许用户在不刷新页面的情况下选择并上传多个文件,提高了用户体验,尤其是在处理大文件...

    java swfupload,上传,图片上传,文件上传,批量上传

    Java中的SWFUpload是一种流行的文件上传工具,尤其适用于处理图片和文件的批量上传。这个工具在Web开发中被广泛使用,因为它提供了用户友好的界面和高效的数据传输能力。SWFUpload是一个基于Flash的组件,它允许用户...

    SWFUpload上传

    本项目是struts2和swfupload结合,实现单个文件上传、上传后预览(index.jsp),和批量上传(multiple.jsp),里面都有代码注释和一个整理的说明文档(SWFUpload说明文档),讲的非常非常详细,都是本人亲自整理。...

    SWFUPLOAD批量上传组件

    SWFUpload是一款强大的JavaScript与Flash结合的文件上传组件,它被广泛应用于Web应用程序中,以实现大文件的批量上传功能。这款组件通过在浏览器端利用Flash技术,绕过了JavaScript单文件上传的限制,使得用户可以一...

    SWFupload_struts_1.rar_struts1 swfuplo_swfupload_swfupload stru

    它尤其适用于处理大文件和批量上传,解决了传统HTML表单上传方式的一些局限性,如文件大小限制、进度显示不足等问题。Struts 1 是一个非常流行的Java Web框架,它基于Model-View-Controller(MVC)设计模式,帮助...

    swfupload 多文件上传 使用框架struts2

    这个库利用Flash技术,能够在不重新加载页面的情况下实现文件的批量上传,提高了用户体验。在本篇文章中,我们将深入探讨如何在Struts2框架下集成SwfUpload实现这一功能。 首先,让我们理解SwfUpload的工作原理。...

    SWFupload_文件批量上传

    SWFupload是一款强大的JavaScript库,专门用于实现网页上的文件批量上传功能。它通过Flash插件在浏览器端提供用户友好的界面,支持多文件选择、进度条显示、预览功能等,极大地提升了用户在上传大量文件时的体验。...

    extjs与swfupload实现java文件批量上传 s2sh

    这里我们将深入探讨如何在Java环境中,结合Struts2、Spring(s2sh框架)以及ExtJS和SWFUpload实现文件批量上传。 **1. ExtJS**:这是一个用于创建桌面级Web应用的JavaScript框架,提供了丰富的组件和强大的数据管理...

    swfupload多文件上传jsp+servlet(SSH可用)

    SwfUpload是一个基于Flash的小型控件,用户可以通过它在浏览器端选择多个文件并进行批量上传。其优点在于能够避免浏览器的同源策略限制,提供良好的用户体验,如进度条显示、错误处理等。在后台,SwfUpload通过...

    Struts2使用SWF上传下载

    SWFUpload是一个流行的JavaScript库,它允许在浏览器中实现文件的上传功能,尤其适用于处理大文件和批量上传的场景。在Struts2中集成SWFUpload,可以创建出用户友好的、具有高级上传功能的网页。 一、Struts2与...

    swfupload+ext2

    其主要优点是支持预览、进度条显示以及批量上传,为用户提供了一种优雅的上传体验。SwfUpload 使用Adobe Flash插件作为其后台处理机制,因此可以绕过浏览器对文件大小和类型的限制,提供了更强大的上传能力。在实际...

    Struts 做的批量上传

    Struts 做的批量上传任何文件

    SwfUpload多文件上传

    2. **多文件上传**:SwfUpload的核心功能是支持多个文件的批量上传。用户可以通过选择多个文件来一次性上传,减少了重复操作。 3. **进度条显示**:SwfUpload可以显示上传进度,让用户了解文件上传的状态,提高了...

    swfupload多文件上传带进度条javaweb项目

    SwfUpload 是一个流行的JavaScript库,它允许用户在浏览器端实现文件的批量上传,同时提供进度条展示上传进度,提高了用户体验。在这个JavaWeb项目中,SwfUpload与Java、JSP技术相结合,构建了一个高效、易用的文件...

    swfupload demo

    SWFUpload 是一个流行的JavaScript库,它允许在网页上实现复杂的文件上传功能,尤其适用于大文件上传和多文件批量上传。这个库利用Adobe Flash技术来提供用户友好的界面,同时通过JavaScript与后端服务器进行交互。...

Global site tag (gtag.js) - Google Analytics