我的Blog我做主^_^

走向一条通往JAVA的不归路...

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  64 随笔 :: 68 文章 :: 77 评论 :: 0 Trackbacks

用于解决在一个页面上传多个文件的问题.

<script type="text/javascript">
function showDialog(url){
 //弹出一个showModalDialog,并以returnValue来获取返回值
 var returnValue = window.showModalDialog(url);
 //alert(returnValue);
 if(returnValue!=null){
  //for(var i=0;i<returnValue.length;i++){
   //document.all.info.innerHTML = returnValue[i]+"<br>";
  //}
  //输出返回值
  document.getElementById("IMAGENAME").value = document.getElementById("IMAGENAME").value + returnValue + "|";
  //document.all.imgname.innerHTML=returnValue;
 }
}
</script>

上传示例






在示例图中的"上传图片"是一个button按钮,给它一个onclick事件,当点击时触发:
<td height="25">
         <input name="IMAGENAME" type="text" id="IMAGENAME" />
         <input type="button" name="Submit3" value="上传图片" onclick="showDialog('automobile/up.jsp')" />
</td>
弹出图片上传页:


图片上传页:
<base target="_self"/>
<body>
<form action="/qcbst/v_uploadautomobileimage" name="myform" method="post" enctype="multipart/form-data" >
<table width="100%"  border="0" align="center">
  <tr>
    <td colspan="2">图片上传</td>
  </tr>
  <tr>
    <td width="30%"><div align="right">汽车图片:</div></td>
    <td width="70%"><input type="file" name="imgname" size="20"><input type="submit" value="上传"></td>
  </tr>
  <tr>
    <td><div align="right">上传信息:</div></td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>

当点击上传后.图片上传到服务器,然后,关闭当前页,返回图片的名称给调用页:

上传servlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setCharacterEncoding("GBK");
  response.setContentType("text/html");
  UploadBean up = new UploadBean();//--------文件上传类
  String path = request.getRealPath(request.getContextPath()).substring(0,request.getRealPath(request.getContextPath()).lastIndexOf("\\"));
    
     try{
         File file = new File(path+"/img/automobile");//
         if(!file.exists()){
           file.mkdir();
         }
     }catch(Exception e){      
     }
     up.setObjectPath(path+"/img/automobile/");//
     //设置上传文件大小
     up.setSize(10000*1024);
     //可上传文件后缀名
     up.setSuffix(".JPG.JPGE.jpg.jpge.gif.GIF");
     try{
      up.setSourceFile(request);
     }catch(Exception e){
      response.getWriter().write("图片上传失败!!!");
     }
     String [] saSourceFile = up.getSourceFile();
     String [] saObjectFile = up.getObjectFileName();
     String [] saDescription = up.getDescription();
    
     int iCount = up.getCount();
     String sObjectPath = up.getObjectPath();
    
     if(saDescription != null && saDescription[0].indexOf("ERR") == -1){
      response.getWriter().write("<script language='javascript'>window.returnValue = '");
      response.getWriter().write(saObjectFile[0]);
      response.getWriter().write("';window.close();</script>");

     }else{
      
      response.getWriter().write("图片上传失败!!!");
     }
 }
 上传完成后:
OK,完成了!!!



posted on 2007-01-16 16:17 java_蝈蝈 阅读(2120) 评论(0)  编辑  收藏

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


网站导航: