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

创新互联公司专注于邻水网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供邻水营销型网站建设,邻水网站制作、邻水网页设计、邻水网站官网定制、成都小程序开发服务,打造邻水网络公司原创品牌,更为您提供邻水网站排名全网营销落地服务。
Struts1.3 jQuery1.3 ajaxupload.3.2.js(一个JQuery的插件,实现Ajax上传的效果)
COS(O’relly的一个性能很棒的上传组件)
JSP页面:
- <%@ 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
 - {
 - //存绝对路径
 - //String filePath = "C://upload";
 - //存相对路径
 - String filePath = request.getRealPath("/")+"upload";
 - File uploadPath = new File(filePath);
 - //检查文件夹是否存在 不存在 创建一个
 - if(!uploadPath.exists())
 - {
 - uploadPath.mkdir();
 - }
 - //文件***容量 5M
 - int fileMaxSize = 5*1024*1024;
 - //文件名
 - String fileName = null;
 - //上传文件数
 - 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 + "个文件!");
 - 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;
 - }
 - }
 
Copyright © 2009-2022 www.wtcwzsj.com 青羊区广皓图文设计工作室(个体工商户) 版权所有 蜀ICP备19037934号