新聞中心
這里有您想知道的互聯(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è)面:
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ include file="../../common/taglibs.jsp" %>
Ajax文件上傳示例
上傳成功的文件有:
- 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


咨詢
建站咨詢
