這篇文章主要講解了“JQuery和Struts如何實(shí)現(xiàn)Ajax文件上傳”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“JQuery和Struts如何實(shí)現(xiàn)Ajax文件上傳”吧!
創(chuàng)新互聯(lián)公司提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì),成都品牌網(wǎng)站建設(shè),一元廣告等致力于企業(yè)網(wǎng)站建設(shè)與公司網(wǎng)站制作,十年的網(wǎng)站開(kāi)發(fā)和建站經(jīng)驗(yàn),助力企業(yè)信息化建設(shè),成功案例突破近千家,是您實(shí)現(xiàn)網(wǎng)站建設(shè)的好選擇.
首先說(shuō)下使用的框架和插件:
Struts1.3 jQuery1.3 ajaxupload.3.2.js(一個(gè)JQuery的插件,實(shí)現(xiàn)Ajax上傳的效果)
COS(O’relly的一個(gè)性能很棒的上傳組件)
JSP頁(yè)面:
<%@ page language="java" pageEncoding="UTF-8"%> <%@ include file="../../common/taglibs.jsp" %>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的工具類(lèi):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("文件類(lèi)型: " + contentType); } System.out.println("共上傳" + fileCount + "個(gè)文件!"); return fileName; } }Cos上傳組件用到的重命名策略類(lèi):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; } }Ajax文件上傳示例
感謝各位的閱讀,以上就是“JQuery和Struts如何實(shí)現(xiàn)Ajax文件上傳”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)JQuery和Struts如何實(shí)現(xiàn)Ajax文件上傳這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!