这是前台代码<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>上传文件</title>
<script src="../js/jquery.js" type="text/javascript"></script>
<script src="../js/jquery.form.js" type="text/javascript"></script>
<script language="javascript">
      $(document).ready(function(){
       
          var validateForm = function() {
          
         var fileName = $('#theFile').val();
   var m=parseInt(fileName.toString().lastIndexOf("."))+1;
       var extVal=fileName.toString().substr(m);
   if(extVal!="doc") {
   alert('文件类型必须为Word文件!');
   return false;
   }
   $('#upMessage').html('文件上传中,请等待... ...');
   return true;
        };          var showResponse = function(data,status) { 
              $('#upMessage').fadeIn("fast",function(){
              });
      return true;
           };
       var options={
          target : '#upMessage',
   url : 'uploadProjectDemandFile',
   beforeSubmit: validateForm,
   success : showResponse,
   resetForm: true
   };  var validateForm1 = function() {
          
         var fileName = $('#theFile1').val();
   var m=parseInt(fileName.toString().lastIndexOf("."))+1;
       var extVal=fileName.toString().substr(m);
   if(extVal!="rar") {
   alert('文件类型必须为压缩文件!');
   return false;
   }
   $('#upMessage1').html('文件上传中,请等待... ...');
   return true;
        };          var showResponse1 = function(data,status) { 
              $('#upMessage1').fadeIn("fast",function(){
               
              });
      return true;
           };
       var options1={
          target : '#upMessage1',
   url : 'uploadProjectDemandFile',
   beforeSubmit: validateForm1,
   success : showResponse1,
   resetForm: true
   };
     $('#upForm1').ajaxForm(options1);      
     $('#upForm').ajaxForm(options); 
      });
</script>
<style type="text/css">
form span{
color:blue;
font-size:12;
}
input{
font-size:10;
}
pre{
color:red;
}
</style>
</head>
<body>
<table>
<tr>
<td width="50%">
<form  style="width:50%;float:left;" id="upForm" method="POST" action="uploadProjectDemandFile" enctype="multipart/form-data">
<table>
<tr>
<td width="120px"><span  style="width:100px;">上传文档(*.doc):</span></td>
<td><input type="file" name="file" id="theFile"  style="width:140px;"/></td>
<td><input class="inputbutton" type="submit" value="提交" />
<input type="hidden" name="type" value="upForm" width="100px"/>
<input type="hidden" name="overview_id" value="<%=request.getParameter("overview_id") %>"/></td>
</tr>
<tr>
<td align="center" colspan="3"><div id="upMessage" style="display:hidden"></div></td>
</tr>
</table>
</form>
</td>
 
<td>
<form style="width:50%;float:left;" id="upForm1" method="POST" action="uploadProjectDemandFile" enctype="multipart/form-data">
<table>
<tr>
<td width="120px"><span style="width:100px;">上传作品(*.rar):</span></td>
<td><input type="file" name="file" id="theFile1" style="width:140px;"/></td>
<td><input type="submit" value="提交" />
<input type="hidden" name="type" value="upForm1"/>
<input type="hidden" name="overview_id" value="<%=request.getParameter("overview_id") %>"/></td>
</tr>
<tr>
<td align="center" align="center" colspan="3"><div id="upMessage1" style="display:hidden"></div></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>-------------------------------------------------------------------------------------------
这是后台Action部分代码    debug测试 ie6后台能接受到零时文件,但是获取的临时文件的存储路径与真实保存路径总是不一样,导致保存时找不到该文件    ie9和chrome就没类似问题
private final static String UPLOADDIR = "..\\webapps\\cxcy";  
    private static final int BUFFER_SIZE = 16 * 1024;  
      
    private File file;  
    private String contentType; //上传文件的MIME类型  
    private String fileName;    //上传文件的文件名,该文件名不包括文件的后缀  
          
    private boolean success;    //标识上传是否成功  
      
  
//上传文件  
    public String uploadProjectDemandFile() throws IOException { 
    
     String originFileName = file.getName();  
        int lastIndexOfSeparator = originFileName.lastIndexOf("//");  
       
        if(lastIndexOfSeparator == -1){  
            fileName = file.getName();  
        }else{  
            fileName = originFileName.substring(lastIndexOfSeparator);  
        }  
        
        
        if(fileName == null || fileName.equals("")) {  
            success = false;  
            printMessage();  
            return "suc";  
        }         
        System.out.println("-----------================================-----------------------================================------------" );
        String lastStr = fileName.split("\\.")[1];
        String path = "\\uploadFile\\demand\\"+Tools.getNextID()+"."+lastStr;
        File docFile =new File(UPLOADDIR+path);
        if (file != null) {  
            //保存文件                    
            copy(file, docFile);  
            //进行其他处理  
        }
        String id = request.getParameter("overview_id");
        ProjectOverview project = projectOverviewService.findProjectOverviewById(id);
        logger.debug("通过overviewid : " + id +" : "+ project.getOverview_name());
        System.out.println("----------------------"+project.getOverview_id());
       
        if(request.getParameter("type").equals("upForm")){
         String delProjectFile = project.getOverview_demand_link();
         delExistedFile(delProjectFile);
         project.setOverview_demand_link(path);
        }else{
         String delProjectFile = project.getOverview_design_link();
         delExistedFile(delProjectFile);
         project.setOverview_design_link(path);
        }
        projectOverviewService.update(project);
        success = true;  
        //message = "上传文件成功。";  
        printMessage();  
        return "suc";  
    }  
      
    private void delExistedFile(String delProjectFilePath) {
     if(delProjectFilePath!=null){
       File docFile =new File(UPLOADDIR+delProjectFilePath);
       docFile.delete();
     }
    }/** 
 * 判断上传文件夹中是否有同名文件 
 * @return 
 */  
    private boolean hasSameFile() {  
          
        return false;  
    }  
      
//输出信息到response  
    private void printMessage() throws IOException {  
        ActionContext ctx = ActionContext.getContext();  
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);   
        response.setContentType("text/plain;charset=utf-8");  
        response.setHeader("Cache-Control", "no-cache");
          
        PrintWriter pw = null;  
        try {  
            StringBuffer sb = new StringBuffer();  
            if(success) {  
                sb.append("上传文件成功!");                 
            }else {  
                sb.append("上传文件失败");  
            }  
              
            pw = response.getWriter();  
            pw.print(sb.toString());  
              
        } catch (IOException e) {  
            e.printStackTrace();  
        }   
        finally {  
            if(pw != null) {  
             pw.flush();
                pw.close();  
                pw = null;  
            }  
        }  
    }  
  
//copy file  
    private static void copy(File src, File dst)  {  
        try  {  
           InputStream in = null ;  
           OutputStream out = null ;  
            try  { 
               in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);  
               out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);  
                byte [] buffer = new byte [BUFFER_SIZE];  
                while (in.read(buffer) > 0 )  {  
                   out.write(buffer);  
               }   
            } finally  {  
                if ( null != in)  {  
                   in.close();  
               }   
                 if ( null != out)  {  
                   out.close();  
               }   
           }   
        } catch (Exception e)  {  
           e.printStackTrace();  
       }   
   }现在就是ie6有这种问题   希望有大神能帮忙看看   

解决方案 »

  1.   

    jquery上传文件的插件的名称是:jquery.uploadify.js。
    多问问google或者度娘吧
    美艳的外表下,有的是惊喜!
      

  2.   

    你这ajaxForm的工作原理根据浏览器不同是不一样的,ie9和chrome支持ajax level 2 所以直接ajax上传文件,ie6不支持,如果没记错,他用个隐藏iframe来上传文件,但是你的叙述中有一点我觉得不可思议,一般ie6以后的浏览器因为安全问题隐藏客户端上传文件的绝对路径,也就是说ie9和chrome默认设置下服务器端是得不到客户端路径的(ie里通过下调安全级别可以得到),而ie6可以,不知道你服务器端处理上传文件用的什么,是怎么解析multipart的request的?你给的代码里看不到,只是你的
    lastIndexOfSeparator在ie6的情况下确实会出问题因为ie6里有绝对路径是不是应该判断"\\"而不是"//"
      

  3.   

    如果你找js上传文件的lib我推荐jQuery-File-Upload