lyyb2001

只是为方便自己找记录而已
posts - 57, comments - 27, trackbacks - 0, articles - 5
  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理

jsp实现文件上传

Posted on 2007-03-29 17:22 skycity 阅读(640) 评论(0)  编辑  收藏 所属分类: 基本开发

cos.jar + uploadbean.jar + filemover.jar
index.jsp
<%@ page contentType="text/html;charset=GBK" language="java" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>教育互动-升学考试</title>
<link href="css/default.css" rel="stylesheet" type="text/css">
</head>

<body >
<form name="frm" method="post" action="upload.jsp" ENCTYPE="multipart/form-data">

<table width="665" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="27" class="green">&nbsp;</td>
    <td height="27" class="green">上传试题:</td>
  </tr>
</table>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="132" ><img src="../image/form_cor1.gif" width="132" height="9"></td>
    <td ><img src="../image/form_top.gif" width="20" height="9"></td>
    <td width="20"><img src="../image/form_cor3.gif" width="20" height="9"></td>
  </tr>
  <tr>
    <td colspan="2" align="left" valign="top"><table width="95%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="27" align="right">考试名称:</td>
        <td>&nbsp;</td>
        <td class="formline"><span class="green">
          <input name="examName" type="text" value="" maxlength="40">
          &nbsp;<span class="red">*</span></span></td>
      </tr>
      <tr>
        <td height="27" align="right">简介:</td>
        <td></td>
        <td height="60" class="formline"><span class="green">
          <textarea name="introduce" cols="50" rows="3"></textarea>
          <span class="red">*</span></span></td>
      </tr>
      <tr>
        <td height="27" align="right">附件:</td>
        <td></td>
        <td ><span class="green">
          <input name="uploadfile" type="file">
        </span></td>
      </tr>
     
    </table></td>
   </tr>
  <tr >
    <td width="132" background="../image/form_bmbg.gif"><img src="../image/form_cor2.gif" width="132" height="9"></td>
    <td background="../image/form_bmbg.gif"><img src="../image/form_bmbg.gif" width="20" height="9"></td>
    <td><img src="../image/form_cor4.gif" width="20" height="9"></td>
  </tr>
</table>
<table width="665" height="40" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="140">&nbsp;</td>
    <td><span class="green">
      <input type="submit"  name="submit" value="上传文件" onclick="return uploadExcel()" >
      <input type="reset" name="reset" value="取 消">
    </span></td>
  </tr>
</table>
</form>
</body>
</html>

<script language="javascript">
function uploadExcel(){
 var upload=document.frm.uploadfile.value;
 var ext=upload.substring(upload.lastIndexOf(".")+1,upload.length);
 if(document.frm.examName.value==""){
  alert("考试名称为空或为空字符");
  document.frm.examName.focus();
  document.frm.examName.select();
  return false;
 }
 if(document.frm.introduce.value.replace(/\s/ig,'').length==0){
  alert("考试简介为空或为空字符");
  document.frm.introduce.focus();
  document.frm.introduce.select();
  return false;
 }
 if(document.frm.introduce.value.replace(/\s/ig,'').length > 200 || document.frm.introduce.value.replace(/\s/ig,'').length < 1){
  alert("考试简介在[1-200]之间");
  document.frm.introduce.focus();
  document.frm.introduce.select();
  return false;
 }
 if(upload==""){
  alert("请上传在线测试题目");
  document.frm.uploadfile.select();
  return false;
 }
 if(ext!="xls"){
  alert("你上传的文件不是excel文件");
  document.frm.uploadfile.select();
  return false;
 }else{
  if(confirm("确定提交在线测试吗?")){
   document.frm.submit();
  }
 }
}
</script>
upload.jsp实现如下:
<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javazoom.upload.*"%>
<%@ page import="uploadutilities.FileMover"%>
<%@ page import="java.text.SimpleDateFormat"%>
<% 
 request.setCharacterEncoding("GBK");//设置编码格式,就不用一个个转码了。
    FileMover fileMover = new FileMover();//你也可以使用自带的实例中jsp:useBean的形式。
    UploadBean upBean = new UploadBean();
    MultipartFormDataRequest mrequest = null;
    Hashtable files = null;
    if(MultipartFormDataRequest.isMultipartFormData(request)){
      try{
      mrequest = new MultipartFormDataRequest(request,null,10*1024*1024,MultipartFormDataRequest.COSPARSER,"GBK");//注意这里也要设置编码参数
      files = mrequest.getFiles();
      }catch(IOException e){
%>
<script language="javascript">
    alert("请上传小于10M的文件");
    history.back(-1);
</script>
<%
      }
   }
 String sServerFileName="";  //服务器文件名
  String sLocalFileName = ""; //本地文件名
 if(files!=null && !files.isEmpty()){
  UploadFile file=null;
    String examName=mrequest.getParameter("examName");
   String introduce=mrequest.getParameter("introduce");
  
  OnlineTestServiceImpl epsi=new OnlineTestServiceImpl();

  file = (UploadFile) files.get("uploadfile");
  sLocalFileName=file.getFileName();

  int ii= sLocalFileName.indexOf("."); //取文件名的后缀
  String sExt = sLocalFileName.substring(ii,sLocalFileName.length());
      //得到不重复的文件名
  java.util.Date dt = new java.util.Date(System.currentTimeMillis());
  SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  sServerFileName= fmt.format(dt);
  sServerFileName =sServerFileName + sExt;

  ExcelReader readExcel = new ExcelReader();
  File dir =new File("D:\\test1");
  if (!dir.exists()){
      dir.mkdirs();
  }
  upBean.setFolderstore("D:\\test1");//设置要上传的目录
  upBean.addUploadListener(fileMover);//增加filMover监听
  fileMover.setNewfilename(sServerFileName);//设置服务器上的文件名
  upBean.store(mrequest, "uploadfile");//上传

  }
 }
%>



Lyyb2001

只有注册用户登录后才能发表评论。


网站导航: