日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
JQuery和Struts實(shí)現(xiàn)Ajax文件上傳

首先說下使用的框架和插件:

創(chuàng)新互聯(lián)公司專注于鄰水網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供鄰水營(yíng)銷型網(wǎng)站建設(shè),鄰水網(wǎng)站制作、鄰水網(wǎng)頁(yè)設(shè)計(jì)、鄰水網(wǎng)站官網(wǎng)定制、成都小程序開發(fā)服務(wù),打造鄰水網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供鄰水網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

Struts1.3   jQuery1.3   ajaxupload.3.2.js(一個(gè)JQuery的插件,實(shí)現(xiàn)Ajax上傳的效果)

COS(O’relly的一個(gè)性能很棒的上傳組件)

JSP頁(yè)面:

 
 
 
  1. <%@ page language="java"  pageEncoding="UTF-8"%> 
  2. <%@ include file="../../common/taglibs.jsp" %> 
  3.  
  4.  
  5.    
  6.     
  7.     
  8.     Ajax文件上傳示例 
  9.      
  10.      #loading,ol{  
  11.       font-size:14px;  
  12.       display:none;  
  13.       color:orange;  
  14.       display:none;  
  15.      }  
  16.      ol{  
  17.       display:block;  
  18.      }  
  19.      
  20.   
  21.    
  22.     
  23.      
  24.      
  25.     
 
  •     
     
  •      
  •      
  •  
  •    
  •   

    上傳成功的文件有:

     
  •    
  •   

     

  •       
  •   

     
  •  
  •   
  •  
  •    
  •  
  • StrutsAction代碼:package com.kay.crm.web;  
  •  
  • import javax.servlet.http.HttpServletRequest;  
  • import javax.servlet.http.HttpServletResponse;  
  •  
  • import org.apache.struts.action.ActionForm;  
  • import org.apache.struts.action.ActionForward;  
  • import org.apache.struts.action.ActionMapping;  
  • import org.apache.struts.actions.DispatchAction;  
  • import org.springframework.stereotype.Controller;  
  •  
  • import com.kay.common.util.CosUtil;  
  •  
  • @Controller("/file")  
  • public class FileUploadAction extends DispatchAction {  
  •  
  •  public ActionForward upload(ActionMapping mapping, ActionForm form,  
  •    HttpServletRequest request, HttpServletResponse response) throws Exception {  
  •     
  •  
  •   String fileName = CosUtil.upload(request);  
  •   System.out.println(fileName);  
  •     
  •   return null;  
  •  }  
  • }Cos的工具類:package com.kay.common.util;  
  •  
  • import java.io.File;  
  • import java.io.IOException;  
  • import java.util.Enumeration;  
  •  
  • import javax.servlet.http.HttpServletRequest;  
  •  
  • import com.oreilly.servlet.MultipartRequest;  
  •  
  • public class CosUtil {  
  •  
  •  @SuppressWarnings({ "deprecation", "unchecked" })  
  •  public static String upload(HttpServletRequest request) throws IOException  
  •  {  
  •   //存絕對(duì)路徑  
  •   //String filePath = "C://upload";  
  •   //存相對(duì)路徑  
  •   String filePath = request.getRealPath("/")+"upload";  
  •   File uploadPath = new File(filePath);  
  •   //檢查文件夾是否存在 不存在 創(chuàng)建一個(gè)  
  •   if(!uploadPath.exists())  
  •   {  
  •    uploadPath.mkdir();  
  •   }  
  •   //文件***容量 5M  
  •   int fileMaxSize = 5*1024*1024;  
  •    
  •   //文件名  
  •   String fileName = null;  
  •   //上傳文件數(shù)  
  •   int fileCount = 0;  
  •   //重命名策略  
  •   RandomFileRenamePolicy rfrp=new RandomFileRenamePolicy();  
  •   //上傳文件  
  •   MultipartRequest mulit = new MultipartRequest(request,filePath,fileMaxSize,"UTF-8",rfrp);  
  •     
  •   String userName = mulit.getParameter("userName");  
  •   System.out.println(userName);  
  •     
  •   Enumeration filesname = mulit.getFileNames();  
  •        while(filesname.hasMoreElements()){  
  •             String name = (String)filesname.nextElement();  
  •             fileName = mulit.getFilesystemName(name);  
  •             String contentType = mulit.getContentType(name);  
  •               
  •             if(fileName!=null){  
  •              fileCount++;  
  •             }  
  •             System.out.println("文件名:" + fileName);  
  •             System.out.println("文件類型: " + contentType);  
  •               
  •        }  
  •        System.out.println("共上傳" + fileCount + "個(gè)文件!");  
  •          
  •        return fileName;  
  •  }  
  • }Cos上傳組件用到的重命名策略類:package com.kay.common.util;  
  •  
  • import java.io.File;  
  • import java.util.Date;  
  •  
  • import com.oreilly.servlet.multipart.FileRenamePolicy;  
  •  
  • public class RandomFileRenamePolicy implements FileRenamePolicy {  
  •  
  •  public File rename(File file) {  
  •    String body="";  
  •       String ext="";  
  •       Date date = new Date();  
  •       int pot=file.getName().lastIndexOf(".");  
  •       if(pot!=-1){  
  •           body= date.getTime() +"";  
  •           ext=file.getName().substring(pot);  
  •       }else{  
  •           body=(new Date()).getTime()+"";  
  •           ext="";  
  •       }  
  •       String newName=body+ext;  
  •       file=new File(file.getParent(),newName);  
  •       return file;  
  •  
  •  }  

  • 文章標(biāo)題:JQuery和Struts實(shí)現(xiàn)Ajax文件上傳
    本文URL:http://www.dlmjj.cn/article/dpjepjc.html