`
vtyi
  • 浏览: 84355 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

文件上传

阅读更多

<script type="text/javascript" src="<%=request.getContextPath() %>/static/js/sourcing/checkAndVerifyData.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/static/js/ajax/common.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/static/js/ajax/ajax.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/static/js/ajax/ajaxSelectOptionBuyingLead.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/static/js/ajax/OpenDialogKeywordPostBuyingLeadAjax.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/static/js/sourcing/uploadPhotoFileDialog.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/static/js/ajax/ajaxOpenTextTips.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/static/js/sourcing/prototype.js"></script> 

 

<table border="0" cellspacing="0" cellpadding="0">
                           
                            <tr>
                              <td class="padding_right_5px">
                     <iframe id='target_upload' name='target_upload' src=''
      style='display: none'></iframe>
     <form  name="form" method="post" enctype="multipart/form-data"
      action="../Upload" onsubmit="return startStatusCheck();"
      target="target_upload">
     <input id="importFile"
      name="importFile" type="file" >
     
     <div id="status"></div>
     <input type="hidden" id="imgsizeconfirm" value="${postBuyingLeadsMsglbl.msgFileSize}">
     <input type="hidden" id="imgtypeconfirm" value="${postBuyingLeadsMsglbl.msgFileFormat}">
                  
                              <table  border="0" cellspacing="0" cellpadding="0">
                              <tbody id="fileList">
                                  <tr>
                                    <td>
                               
                                    
                                    <h:commandButton id="submitButton" styleClass="button_gray_up"
                                    onmouseover="this.className='button_gray_down'"
                                    onmouseout="this.className='button_gray_up'"
                                     type="submit" 
                                      value="#{postBuyingLeadsMsglbl.lblBrowser}">                                     
                                    </h:commandButton>
                                    <h:inputHidden id="msgsubFileFormat" ></h:inputHidden>
                                    <h:inputHidden id="sizelblAccept" ></h:inputHidden>
                      
         
         <span style="display:none">
         <DIV id="uploadFiles" style="display:none">
         <img id="uploadFilesimgnotdisp" src="" width="10" height="10">
         </DIV>
         </span>
         
                   
                                    </td>
                                  <td>
                                   &nbsp;
                                  </td>
                                    <td>
                                    <div id="uploadfiledivdisplay" style="display:none">
                                   
                                    <h:commandButton styleClass="button_gray_up" id="post_lead_delete"
                                    onmouseover="this.className='button_gray_down'"
                                    onmouseout="this.className='button_gray_up'"
                                    type="button" value="#{postBuyingLeadsMsglbl.lblDelete}" onclick="clearValueSrc('#{postBuyingLeadsMsglbl.msgSureDelete}')">                                     
                                    </h:commandButton>
                                  
                        
         </div>          
                                    </td>
                                  </tr>
                                  </tbody>
                              </table>
                               </form>
                              </td>
                              </tr>
                          </table>

 

 

 

/uploadPhotoFileDialog.js

 

 var iTimer = null;
 var img=null;
 var flagboolreturn=false;
 var tipstr=null;
 function  checkimage(name){
  var msgtype=document.getElementById("imgtypeconfirm").value;
  var msgsize=document.getElementById("imgsizeconfirm").value;
  flagboolreturn=false;
  if(!(/^.+\.(gif|jpg)$/i.test(name))){
   alert(msgtype);
  }
  else
   return true;
  // changePicture(name);
  // img=window.opener.document.getElementById("imagePhotoPostUp");
   //img=document.getElementById("imagePhotoPostUp");
  // var img=new   Image();//document.getElementById("uploadFilesimgnotdisp");
  // img.src=name;
  /*
   if(ErrImgBig(msgsize,name)){ 
    
    flagboolreturn=true;
    return true;
   }
   else{
   
    flagboolreturn=false;
    if(img){
    if (window.navigator.userAgent.indexOf("Firefox")>=1){
     img.parentNode.removeChild(img);
    }
    else{
     img.removeNode(true);
    }
   }
    return false;
   }
   flagboolreturn=true;
   return true;
   
  }
  else{
   tipstr+=msgtype+"\n";
   alert(msgtype);
   flagboolreturn=false;
   if(img){
    if (window.navigator.userAgent.indexOf("Firefox")>=1){
     img.parentNode.removeChild(img);
    }
    else{
     img.removeNode(true);
    }
   }
   return false;
  }
  
  if(tipstr!=null){
   alert(tipstr);
   return false;
  }
  else
   return true;
   */
 }

 function  ErrImgBig(msgsize,srcname){
 // img=window.opener.document.getElementById("imagePhotoPostUp");
  //img=document.getElementById("imagePhotoPostUp");
  
  
  var imgs= document.getElementById("uploadFilesimgnotdisp");//new Image();//
  
  imgs.src=srcname;
  
  //var si=imgs.fileSize;
  //if(imgs.readyState!="complete")
     //addImg(imgs);
    // alert(imgs.readyState);
  var si=imgs.fileSize;
  //do{
   si=imgs.fileSize;
  //}while(imgs.readyState!="complete");
 // alert("img"+imgs.fileSize);
 // alert(imgs.fileSize);
  var maxfilesize = 102400;
 // var testimgsize=maxfilesize-
  //alert(img.src);
  var noknow=imgs.src;
  if(si < 0 || typeof(imgs) == 'underfined')return false;
  if  (imgs.fileSize>maxfilesize){
   //tipstr+=msgsize;
   alert(msgsize);
   return false;
  } else {
   
   return true;
  }
   }
  
  
   function addImg(imgObj){
   //  alert('in');
     if(typeof(imgObj) == 'object'){
           if(imgObj.fileSize > 102400 && imgObj.width !=0 && imgObj.height !=0){
      //          alert('add'+imgObj.fileSize);
                 clearTimeout(iTimer); iTimer = null;
            }else{
             
                 iTimer = setTimeout( "addImg" ,  1500 ); 
           }
     }
  }
  
 
 
 function clearValueSrc(confirmmsg){
  var objimg=document.getElementById("imagePhotoPostUp");
  
  if(objimg!=null){
   var srcvalue=objimg.src;
   
   if((/^.+\.(gif|jpg)$/i.test(srcvalue))){
    var okmsg = window.confirm(confirmmsg);
    if(okmsg){
     document.getElementById("pic2").innerHTML ="";
     $('ppe:post_buying_leads:submitButton').disabled = false;
     document.getElementById("uploadfiledivdisplay").style.display="none";
     //document.getElementById("imagePhotoPostUp").src="";
        //   document.getElementById("postbuyingleadform:uploadphotopostHidden").value="";
          }
   }
  }
  
 }


  var updater = null;


 function startStatusCheck()
  {
    var t=document.getElementById("importFile");
 //   t.fireEvent("onclick");
    if(!checkimage(t.value)){
     return false;
    }
    
  // t.click();
   
     $('ppe:post_buying_leads:submitButton').disabled = true;
     document.getElementById("uploadfiledivdisplay").style.display="";
    
   /*
     updater = new Ajax.Request(
                              
                                 'Upload',
                                 {asynchronous:true,  method: 'get',
                                 requestHeaders:[ 'Content-Type ', 'multipart/form-data '],
                                 onComplete:function(){alert('onSuccess:function');}});
  
  
   updater = new Ajax.PeriodicalUpdater(
                                 'status',
                                 'Upload',
                                 {asynchronous:true, frequency:1, method: 'get', onSuccess:function(request){alert('onSuccess:function');}});
  */
  //$('submitButton').disabled = false;
     return true;
    }  

 
 function followHandler(request){
  alert("followHandler");
 }
 
   function reportError(request)
   {
     document.getElementById('ppe:post_buying_leads:submitButton').disabled = false;
     alert("followHandler");

   }
 
   function killUpdate(message)
   {
    var msgOutput = "";
    document.getElementById("pic2").innerHTML =
      msgOutput.concat("<img id='imagePhotoPostUp' width='80' height='80' src='/emp-web/image?file=", message, "'>");
     document.getElementById("photoPathsetValue").value=message;
   }
     function killUpdateAlert()
   {
    var ms=document.getElementById("imgsizeconfirm").value; 
    document.getElementById("uploadfiledivdisplay").style.display="none";
    $('ppe:post_buying_leads:submitButton').disabled = false;
    alert(ms);
   }

 

 

/**
 *****************************************************************************
 * Copyright 2007 Hong Kong Trade Development Council. All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of HKTDC. You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license
 * agreement you entered into with HKTDC.
 *
 *****************************************************************************
 * Revision History
 *****************************************************************************
 * Date                   Author       Description
 * Jan 10, 2007           Finco Li  Upload Servlet for file AJAX upload
 *****************************************************************************
 */
package com.hktdc.emp.sg.fileupload.servlet;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.hktdc.emp.sg.fileupload.listener.FileUploadListener;
import com.hktdc.emp.sg.fileupload.listener.FileUploadListener.FileUploadStats;
import com.hktdc.emp.sg.fileupload.util.PhotoVideoLocationUtility;
import com.missiondata.fileupload.MonitoredDiskFileItemFactory;

public class UploadServlet extends HttpServlet {
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
 }

 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  HttpSession session = request.getSession();

  if ("status".equals(request.getParameter("c"))) {
   doStatus(session, response);
  } else {
   doFileUpload(session, request, response);
  }
 }

 private void doFileUpload(HttpSession session, HttpServletRequest request,
   HttpServletResponse response) throws IOException {
  
  try {
   FileUploadListener listener = new FileUploadListener(request
     .getContentLength());
   session.setAttribute("FILE_UPLOAD_STATS", listener
     .getFileUploadStats());
   FileItemFactory factory = new MonitoredDiskFileItemFactory(listener);
   ServletFileUpload upload = new ServletFileUpload(factory);
   List items = upload.parseRequest(request);
   
   boolean hasError = false;
   String fileURL = "";
   long filesizes=0;
   for (Iterator i = items.iterator(); i.hasNext();) {
    FileItem fileItem = (FileItem) i.next();
    filesizes+=fileItem.getSize();
   }
   if(filesizes>102400)
   {
    
    response
    .getWriter()
    .print(
      "<html><head><script type='text/javascript'>function killUpdateAlert() { window.parent.killUpdateAlert();"
        + " }</script></head><body onload='killUpdateAlert()'></body></html>");
    return ;
   }
   
   for (Iterator i = items.iterator(); i.hasNext();) {
    FileItem fileItem = (FileItem) i.next();
    
    if (!fileItem.isFormField()) {

     // *************************************************
     // This is where you would process the uploaded file
     // *************************************************
     fileURL = new PhotoVideoLocationUtility().storagePhoto("a",
       fileItem.getInputStream(), fileItem.getName()
         .substring(
           fileItem.getName().lastIndexOf(
             File.separator)+1));
     fileItem.delete();
    }
   }

   if (!hasError) {
    sendCompleteResponse(response, fileURL);
   } else {
    sendCompleteResponse(response,
      "Could not process uploaded file. Please see log for details.");
   }
  } catch (Exception e) {
   sendCompleteResponse(response, e.getMessage());
  }
 }

 private void doStatus(HttpSession session, HttpServletResponse response)
   throws IOException {
  // Make sure the status response is not cached by the browser
  response.addHeader("Expires", "0");
  response.addHeader("Cache-Control",
    "no-store, no-cache, must-revalidate");
  response.addHeader("Cache-Control", "post-check=0, pre-check=0");
  response.addHeader("Pragma", "no-cache");

  FileUploadListener.FileUploadStats fileUploadStats = (FileUploadListener.FileUploadStats) session
    .getAttribute("FILE_UPLOAD_STATS");
  if (fileUploadStats != null) {
   long bytesProcessed = fileUploadStats.getBytesRead();
   long sizeTotal = fileUploadStats.getTotalSize();
   long percentComplete = (long) Math
     .floor(((double) bytesProcessed / (double) sizeTotal) * 100.0);
   long timeInSeconds = fileUploadStats.getElapsedTimeInSeconds();
   double uploadRate = bytesProcessed / (timeInSeconds + 0.00001);
   double estimatedRuntime = sizeTotal / (uploadRate + 0.00001);
   
   response.getWriter().println("<b>Upload Status:</b><br/>");

   if (fileUploadStats.getBytesRead() != fileUploadStats
     .getTotalSize()) {
    response.getWriter().println(
      "<div class=\"prog-border\"><div class=\"prog-bar\" style=\"width: "
        + percentComplete + "%;\"></div></div>");
    response.getWriter().println(
      "Uploaded: " + bytesProcessed + " out of " + sizeTotal
        + " bytes (" + percentComplete + "%) "
        + (long) Math.round(uploadRate / 1024)
        + " Kbs <br/>");
    response.getWriter().println(
      "Runtime: " + formatTime(timeInSeconds) + " out of "
        + formatTime(estimatedRuntime) + " "
        + formatTime(estimatedRuntime - timeInSeconds)
        + " remaining <br/>");
   } else {
    response.getWriter().println(
      "Uploaded: " + bytesProcessed + " out of " + sizeTotal
        + " bytes<br/>");
    
    response.getWriter().println("Complete.<br/>");
   }
   System.out.println("--sizeTotal--"+sizeTotal);
  }

  if (fileUploadStats != null
    && fileUploadStats.getBytesRead() == fileUploadStats
      .getTotalSize()) {
   response.getWriter().println("<b>Upload complete.</b>");
  }
 }

 private void sendCompleteResponse(HttpServletResponse response,
   String message) throws IOException {
  if (message == null) {
   response
     .getOutputStream()
     .print(
       "<html><head><script type='text/javascript'>function killUpdate() { window.parent.killUpdate(''); }</script></head><body onload='killUpdate()'></body></html>");
  } else {
   //message = message.replaceAll("/", "//");
   response
     .getOutputStream()
     .print(
       "<html><head><script type='text/javascript'>function killUpdate() { window.parent.killUpdate('"
         + message
         + "'); }</script></head><body onload='killUpdate()'></body></html>");
  }
 }

 private String formatTime(double timeInSeconds) {
  long seconds = (long) Math.floor(timeInSeconds);
  long minutes = (long) Math.floor(timeInSeconds / 60.0);
  long hours = (long) Math.floor(minutes / 60.0);

  if (hours != 0) {
   return hours + "hours " + (minutes % 60) + "minutes "
     + (seconds % 60) + "seconds";
  } else if (minutes % 60 != 0) {
   return (minutes % 60) + "minutes " + (seconds % 60) + "seconds";
  } else {
   return (seconds % 60) + " seconds";
  }
 }
}

分享到:
评论

相关推荐

    基于SpringBoot的文件上传系统,前后端分离,单文件上传,多文件上传,大文件上传,断点续传,文件秒传,图片上传

    基于SpringBoot的文件上传系统,前后端分离,单文件上传,多文件上传,大文件上传,断点续传,文件秒传,图片上传 项目经过严格测试,确保可以运行! 采用前后端分离的方式进行开发,实现了几种常用的文件上传功能...

    php大文件上传,百兆文件上传,有进度条,有上传速度

    之前发布一个大文件上传,但是问题太多,此版本相对上一个稳定得多 1,将压缩包里的 php_uploadprogress.dll 文件复制到你的PHP 安装目录里的扩展库文件夹 就是 ext文件夹下面 2,在你的服务器上随便一个地方(最好...

    Vuejs文件上传组件多文件上传

    在Vue.js中,实现文件上传功能是常见的需求,尤其是在构建交互丰富的Web应用时。本篇将详细介绍Vue.js中实现多文件上传的相关知识点,以及如何使用`vue-upload-component`这个开源组件来帮助我们实现这一功能。 ...

    PHP文件上传类

    为了简化这个过程,开发者通常会编写一个专门的文件上传类,以实现对上传过程的全面控制和错误处理。以下是对"PHP文件上传类"的详细解析: 1. **类的结构**: - 一个PHP文件上传类通常包含一系列的方法,如`upload...

    文件上传下载需要的jar包 java文件上传下载

    在Java开发中,文件上传和下载是常见的功能需求,尤其在网络应用中,如网页、桌面应用或移动应用。为了实现这些功能,开发者通常需要引入特定的库或jar包。在这个场景下,"文件上传下载需要的jar包"指的是用于处理...

    java多文件上传实现

    在Java开发中,多文件上传是一项常见的功能,尤其在Web应用中,用户可能需要一次性上传多个文件,如图片、文档等。本知识点将详细介绍如何在Java中实现这一功能,以及结合Flash实现上传界面并显示上传进度条。 1. *...

    SpringMVC文件上传,多文件上传实例

    在这个“SpringMVC文件上传,多文件上传实例”中,我们将深入探讨如何在SpringMVC环境中实现文件上传功能,包括单个文件上传以及多个文件的批量上传。 1. **文件上传原理**: 文件上传是通过HTTP协议的POST请求来...

    php文件上传插件(简洁好用的上传插件)

    - **多文件上传**:支持单个文件和批量文件上传,提高用户操作效率。 2. **使用方法** 首先,你需要将压缩包中的PHP文件解压到你的项目目录中。然后,在需要实现文件上传功能的PHP脚本中引入该插件,通过调用其...

    jsp自行增加上传文件的批量文件上传

    本想做个批量文件上传的功能(设想是客户选择一个目录后,程序能判断需要上传的文件上传到服务器指定目录下),结果没有做出来,在网上找了许多版本的批量上传程序,但实验效果不好,有的还需要下载分,上传文件也不能...

    html5文件上传插件

    使用这个插件,开发者可以轻松集成到自己的项目中,实现高效、友好的文件上传功能。 HTML5文件上传的核心特性包括: 1. **多文件选择**:通过`&lt;input type="file" multiple&gt;`,用户可以在文件选择对话框中选取多个...

    bootstrap 文件上传组件 fileinput 实现文件批量上传

    Bootstrap文件上传组件FileInput是一款强大的前端文件上传工具,尤其适合于需要实现文件批量上传的场景。这个组件基于Bootstrap框架,提供了美观的用户界面和丰富的功能,使得文件上传操作变得直观且易于实现。以下...

    C# .NET FileUpload 多文件上传例子

    在.NET框架中,C#语言提供了丰富的功能来处理文件上传操作。对于初学者来说,理解并实现多文件上传是一项重要的技能,它可以帮助你构建交互性强、功能完善的Web应用程序。在这个"C# .NET FileUpload 多文件上传例子...

    多文件上传和展示

    【标题】:多文件上传和展示 在网页开发中,多文件上传功能是常见的需求,尤其是在数据管理和资源共享的场景下。Bootstrap-fileinput 是一个流行的、功能丰富的jQuery插件,专门用于实现美观且实用的文件上传功能,...

    JSP无组件文件上传

    在网络上找到的,纯JSP实现的文件上传程序,支持多文件的上传,例子是多文件的上传,稍微修改就可以变成单文件的上传或者更多文件的上传,控制成需要扩展名的文件上传,指定大小的文件上传等。程序目前上传文件存储...

    JSP多文件上传(同时上传)

    "JSP多文件上传"是一个常见的需求,特别是在文件分享、在线教育或文档管理等场景。这个功能允许用户一次性选择并上传多个文件,提高了用户体验,减少了多次点击和等待时间。 实现JSP多文件上传,我们需要了解以下几...

    EXTJS 多文件上传

    这个功能对于处理大量文件上传的场景非常有用,比如在内容管理系统、图像库或者文件分享平台等应用中。 `MultiFileUploadField` 的核心特点包括: 1. **多选支持**:用户可以通过浏览器的文件选择对话框一次选择多...

    jquery-大文件上传插件,支持分片上传,断点续传

    在IT领域,尤其是在Web开发中,文件上传是一个常见的需求,特别是在处理大文件如高清图片、视频或大型文档时。jQuery,作为一个广泛使用的JavaScript库,提供了一系列的插件来解决这个问题。"jquery-大文件上传插件...

    android 文件上传(多个或单个),图片上传

    在Android开发中,文件上传,特别是图片上传,是常见的需求之一。这涉及到用户选择本地文件,通常是图片,然后通过网络接口将这些文件发送到服务器。本文将深入探讨如何实现Android平台上的多文件和单文件上传,以及...

    通用 万能 HttpHandler webRequest 文件上传

    总之,"通用 万能 HttpHandler webRequest 文件上传"是一个关于使用自定义HttpHandler和HttpWebRequest进行文件上传的技术主题。这个解决方案旨在提供一个高度可定制和可扩展的文件上传服务,以满足各种Web应用的...

    php大文件上传

    接着,实现大文件上传的关键在于分块处理。我们可以将大文件分割成多个小块,然后逐个上传。这种方式降低了单次请求的压力,有利于网络不稳定时的恢复。PHP中,可以通过`fread()`函数读取文件的特定部分,`cURL`库则...

Global site tag (gtag.js) - Google Analytics