给你一个我的文件
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import ="java.sql.*" %>
<%@ page import ="com.jspsmart.upload.*" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>
<%@ page import ="jxl.*" %>
<%@include file="/public/checksession.jsp"%>
<jsp:useBean id="time2" scope="session" class="com.hoten.util.Time" />
<jsp:useBean id="dbo" scope="session" class="com.hoten.db.DBOption" />
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%!
//这个参数需要修改
  String uploadPath="/usr/www/upload/";
   //String uploadPath="e:\\load\\";
//String iniFile="e:\\javacmppc.ini";
  private String addZero(int int1){
    String reStr;
    if (int1 < 10)
      reStr = "0" + String.valueOf(int1);
    else
      reStr = String.valueOf(int1);
    return reStr;
  }
%>
<%try {
  dbo.setConnection();
  ResultSet rs = null;
  int count=0;
  // Initialization
  String time=time2.getTime(12) ;
  mySmartUpload.initialize(pageContext);
  mySmartUpload.setTotalMaxFileSize(500000);
  mySmartUpload.upload();
  String strFile="";
  String destid = mySmartUpload.getRequest().getParameter("destid");
  String serviceid = mySmartUpload.getRequest().getParameter("serviceid");
  String msg = mySmartUpload.getRequest().getParameter("msg");  try {
    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
    strFile = myFile.getFileName();
    count = mySmartUpload.save(uploadPath);
  } catch (Exception e) {
    out.println(e.toString());
  }
  //文件传输完毕!
  //初始化excel接口
  //对文本文件进行处理
  if(strFile.substring(strFile.length()-3).equals("txt")){
    String sFile=uploadPath+strFile;
    String sLine="";
    FileInputStream pFileStream;
    BufferedReader pFileRead;
    pFileStream = new FileInputStream( sFile );
    pFileRead = new BufferedReader( new InputStreamReader( (InputStream)pFileStream ) );
    try{
      byte[] sMsgID = new byte[21];
      byte[] sDestTermID = new byte[21];
      String mobile="" ;
      String strSql="";
      int i = 0;
      while(true && sLine!=null){
        sLine = pFileRead.readLine();
        if(sLine!=null){
          mobile = sLine.trim();
        /*if( mobile.length() > 21 ) {
          System.arraycopy( mobile.getBytes(), 0, sDestTermID, 0, 21 );
        } else {
          System.arraycopy( mobile.getBytes(), 0, sDestTermID, 0, mobile.length() );
        }*/
        if(mobile.length()!=11){
          out.print("<script>alert('手机号必须为11位!');window.location='SendFile.jsp';</script>") ;
        }
        String moreg="" ;
        /*String sql="select region from hasms_region where mobilecode='"+moreg+"'";
        rs=dbo.selectOption(sql) ;
        if(rs.next()){
          moreg=rs.getString(1) ;
        }
        else{
          moreg=mobile.substring(0,7) ;
          sql="select region from hasms_region where mobilecode='"+moreg+"'";
          rs=dbo.selectOption(sql) ;
          if(rs.next() ){
            moreg=rs.getString(1) ;
          }
          else{
            moreg="";
          }
        }*/
        time=time2.getTime(15) ;
        moreg=com.hoten.util.Tools.getMobileArea(mobile) ;
        if(!moreg.equals("NULL")){
           strSql="insert into hasms_waitsend(sendtime,code,mobile,tomobile,msg,destid,serviceid,area)"+
               " values('"+time+"','NONE','"+mobile+"','"+mobile+"','"+msg+"','"+destid+"','"+serviceid+"','"+moreg+"')";
          try{
            dbo.insertOption(strSql,false) ;
            //out.println(strSql);
            }catch(Exception ex){
            //out.println(strSql);
            }
        }
        }
      }//end while
    }catch(Exception ex){
      out.println("发送完毕");
    }
    //stmt.executeBatch();
    out.print("<script>alert('发送成功!');window.location='SendFile.jsp';</script>") ;
    return;
  }
  //对EXCEL文件进行处理
  else if(!strFile.substring(strFile.length()-3).equals("xls")){//不是xsl后缀
    out.println("请上传excel或者文本文件。");
    return;
  }
  else{
    Workbook workbook = Workbook.getWorkbook(new java.io.File(uploadPath+strFile));
    Sheet sheet = workbook.getSheet(0);
    try{
      byte[] sMsgID = new byte[21];
      byte[] sDestTermID = new byte[21];
      String mobile="" ;
      String strSql="";
      int i = 0;
      while(true){
        Cell a1 = sheet.getCell(0,i++);
        mobile = a1.getContents();
        if( mobile.length() > 21 ) {
          System.arraycopy( mobile.getBytes(), 0, sDestTermID, 0, 21 );
        } else {
          System.arraycopy( mobile.getBytes(), 0, sDestTermID, 0, mobile.length() );
        }
        if(mobile.length()!=11){
          out.print("<script>alert('手机号必须为11位!');window.location='SendInfo.jsp';</script>") ;
        }
        String moreg=mobile.substring(0,3) ;
        String sql="select region from hasms_region where mobilecode='"+moreg+"'";
        rs=dbo.selectOption(sql) ;
        if(rs.next()){
          moreg=rs.getString(1) ;
        }
        else{
          moreg="";
        }
        strSql="insert into hasms_waitsend(sendtime,code,mobile,tomobile,msg,destid,serviceid,area)"+
               " values('"+time+"','NONE','"+mobile+"','"+mobile+"','"+msg+"','"+destid+"','"+serviceid+"','"+moreg+"')";
        try{
          dbo.insertOption(strSql,false) ;
          //out.println(strSql);
        }catch(Exception ex){
          //out.println(strSql);
        }      }//end while
    }catch(Exception ex){
      out.println("发送完毕");
    }
    //stmt.executeBatch();
    out.print("<script>alert('发送成功!');window.location='SendFile.jsp';</script>") ;
  }
  if(rs!=null)
     rs.close();
}
catch (Exception ex) {
  ex.printStackTrace();
}
finally {
  dbo.release();
}
%>

解决方案 »

  1.   

    可以用String path=req.getServletPath();
    //getServletPath()是得到服务器根目录下的文件路径
    if(path.lastIndexOf("\\")>0 || path.lastIndexOf("//")>0)
    {
    //在这里来取到文件名或路径名
    //记得要try catch
      

  2.   

    不好意思,这句写错了
    if(path.lastIndexOf("\\")>0 || path.lastIndexOf("//")>0)其实只要String path=req.getServletPath();就可以判断了。
    if(path.lastIndexOf("\\")