<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    upload.jsp:
    在這個(gè)文件里,使用了新swfload的上傳方式,將文件流發(fā)送到struts2的doMultipleUploadUsingList.action,uploadStart的這個(gè)function,是將頁(yè)面上的"id"元素的值一起帶過去,上傳過程中,會(huì)在divStatus的DIV中顯示上傳進(jìn)度條。

    <%@ 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是一個(gè)可以通用的struts2 action,可以接收批量或單個(gè)上傳過來的圖片。并且可以選擇生成相應(yīng)壓縮圖。圖片生成的命名方式是從xxx_01、xxx_02、xxx_03一直自動(dòng)排列下去。壓縮圖為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;
     }
        
     }


    這個(gè)action還調(diào)用了一個(gè)類DirList ,是用于掃描當(dāng)前文件夾里的圖片,并且根據(jù)當(dāng)前的命名情況來命名新上傳的圖片,如原來已經(jīng)有了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()); 
      }
     }


     

    posted on 2008-11-02 06:25 lzj520 閱讀(2555) 評(píng)論(0)  編輯  收藏 所屬分類: 個(gè)人學(xué)習(xí)日記Struts2
    主站蜘蛛池模板: 光棍天堂免费手机观看在线观看| 久久精品一本到99热免费| 黑人大战亚洲人精品一区| 日本一道本不卡免费| 国产亚洲精品影视在线| 久久精品亚洲乱码伦伦中文| 亚洲免费观看网站| 色婷婷综合缴情综免费观看| 久久亚洲春色中文字幕久久久| 日韩高清在线免费看| 国产好大好硬好爽免费不卡| 亚洲av中文无码字幕色不卡| 久久精品国产亚洲AV麻豆~| 国产又大又黑又粗免费视频 | 91免费福利精品国产| 狠狠入ady亚洲精品| 亚洲经典在线观看| 久久亚洲AV永久无码精品| 大地资源在线观看免费高清| 精品免费久久久久国产一区| 亚洲色欲色欲www在线播放 | 亚洲精品美女在线观看播放| | 3344永久在线观看视频免费首页| 羞羞视频免费观看| 91亚洲自偷在线观看国产馆| 91麻豆国产自产在线观看亚洲| 最近2019中文字幕免费看最新| 久久久久久久99精品免费 | 91福利视频免费| 你懂的免费在线观看| 亚洲AV无码XXX麻豆艾秋| 亚洲第一页在线播放| 亚洲国产第一站精品蜜芽| 亚洲国产天堂久久久久久| 午夜视频免费观看| 91成年人免费视频| 18禁美女裸体免费网站| 成人A片产无码免费视频在线观看| 福利片免费一区二区三区| 亚洲欧美国产欧美色欲|