CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2EE / EJB / JMS

struts如何实现上传文件的重命名????

楼主fasi123()2006-03-06 14:26:04 在 Java / J2EE / EJB / JMS 提问

public   ActionForward   execute(ActionMapping   mapping,  
                                                                    ActionForm   form,  
                                                                    HttpServletRequest   request,  
                                                                    HttpServletResponse   response)  
          throws   Exception   {  
   
                  String   dir=servlet.getServletContext().getRealPath("/upload");  
   
                  HtmlFileForm   hff   =   (HtmlFileForm)   form;  
   
                  //   org.apache.struts.upload.FormFile   contains   the   uploaded   file  
                  FormFile   file   =   hff.getFile();  
   
                  //   If   no   file   was   uploaded   (e.g.   first   form   load),   then   display   View  
                  if   (file   ==   null   )   {  
                                  return   mapping.findForward("success");  
   
                  }  
   
                  //   Get   the   name   and   file   size  
                  String   fname   =   file.getFileName();  
                  String   size   =   Integer.toString(file.getFileSize())   +   "   bytes";  
   
                  InputStream   streamIn   =   file.getInputStream();  
                  OutputStream   streamOut   =   new   FileOutputStream(dir   +   "/"+fname);  
   
                  int   bytesRead   =   0;  
                  byte[]   buffer   =   new   byte[8192];  
                  while   ((bytesRead   =   streamIn.read(buffer,   0,   8192))   !=   -1)   {  
                          streamOut.write(buffer,   0,   bytesRead);  
                  }  
   
                  streamOut.close();  
                  streamIn.close();  
                  //   Populate   the   form   bean   with   the   results   for   display   in   the   View  
                  hff.setFname(fname);  
                  hff.setSize(size);  
   
                  //   Clean   up   our   toys   when   done   playing  
                  file.destroy();  
   
                  //   Forward   to   default   display  
                  return   mapping.findForward("success");  
   
          }  
  ------------------------------------------------------------------------------------  
  以上代码是孙卫琴的《精通struts:基于mvc的java   web设计与开发》中关于文件上传的列子,偶的想法是  
   
  ,把上传的文件要根据当前系统时间进行重命名后在传到相应的upload目录。    
    例如:现在是2006年03月06日14:23:21,用户通过浏览指定某一个目录下的a.txt文件,当用户点上传按  
   
  纽后,上传到服务器的upload目录下,就成了20060306142321.txt这个文件,请问在上边的方法中该怎么  
   
  实现,请路过的高人朋友说一下。谢谢。 问题点数:100、回复次数:7Top

1 楼jfy3d(剑事 http://www.migti.com)回复于 2006-03-06 16:34:16 得分 0

OutputStream   streamOut   =   new   FileOutputStream(dir   +   "/"+fname);  
   
  fnameTop

2 楼li_d_s(鄙视那些不懂Java却跑来乱骂的人,.NET没啥了不起)回复于 2006-03-06 16:59:17 得分 0

FormFile   file   =   noticeEditForm.getAttachFile();//取得上传的文件  
  if(null   !=   file   &&   null   !=   file.getFileName()   &&   0   !=   file.getFileName().trim().length()){  
  String   fileName   =   Feedback.getFormatedCurrentDate(conn)   +   staff.getId()   +   StringUtil.getExtendName(file.getFileName());  
  try   {  
  InputStream   stream   =   file.getInputStream();//把文件读入  
  String   filePath   =   servlet.getServletContext().getRealPath("/");//取当前系统路径  
  ByteArrayOutputStream   baos   =   new   ByteArrayOutputStream();  
  OutputStream   bos   =   new   FileOutputStream(filePath   +   "attachment\\"   +   fileName);//建立一个上传文件的输出流.  
  notice.setIsAnnex("1");  
  notice.setFilePath("./attachment/"   +   fileName);  
  int   bytesRead   =   0;  
  byte[]   buffer   =   new   byte[8192];  
  while   (   (bytesRead   =   stream.read(buffer,   0,   8192))   !=   -1)   {  
  bos.write(buffer,   0,   bytesRead);//将文件写入服务器  
  }  
  bos.close();  
  stream.close();  
  }catch(Exception   e){  
  errors.add("uploadFail",   new   ActionError("error.bulletin.uploadFail"));  
  e.printStackTrace();  
  }  
  }else{  
  //errors.add("fileRequired",   new   ActionError("error.bulletin.fileRequired"));  
  }Top

3 楼li_d_s(鄙视那些不懂Java却跑来乱骂的人,.NET没啥了不起)回复于 2006-03-06 17:00:23 得分 0

代码有点乱,就是把上传的文件读出来写到新的你命名的文件Top

4 楼jeffaple(坤)回复于 2006-03-06 17:08:21 得分 0

路过,学习,我还没看到这边。。。Top

5 楼zhh1981(**的猪头)回复于 2006-03-06 17:14:39 得分 100

 
  public   ActionForward   execute(ActionMapping   mapping,  
                                                                    ActionForm   form,  
                                                                    HttpServletRequest   request,  
                                                                    HttpServletResponse   response)  
          throws   Exception   {  
   
                  String   dir=servlet.getServletContext().getRealPath("/upload");  
   
                  HtmlFileForm   hff   =   (HtmlFileForm)   form;  
   
                  //   org.apache.struts.upload.FormFile   contains   the   uploaded   file  
                  FormFile   file   =   hff.getFile();  
   
                  //   If   no   file   was   uploaded   (e.g.   first   form   load),   then   display   View  
                  if   (file   ==   null   )   {  
                                  return   mapping.findForward("success");  
   
                  }  
   
                  //   Get   the   name   and   file   size  
                  String   fname   =   file.getFileName();  
                  java.text.SimpleDateFormat   sdf   =   new   java.text.SimpleDateFormat("yyyyMMddHHmmss");  
                    java.util.Date     mydate=new   java.util.Date();  
                  String   s=sdf.format(mydate);  
                  fname     =   s   +   fname   .substring(fname.indexOf("."));  
                  String   size   =   Integer.toString(file.getFileSize())   +   "   bytes";  
   
                  InputStream   streamIn   =   file.getInputStream();  
                  OutputStream   streamOut   =   new   FileOutputStream(dir   +   "/"+fname);  
   
                  int   bytesRead   =   0;  
                  byte[]   buffer   =   new   byte[8192];  
                  while   ((bytesRead   =   streamIn.read(buffer,   0,   8192))   !=   -1)   {  
                          streamOut.write(buffer,   0,   bytesRead);  
                  }  
   
                  streamOut.close();  
                  streamIn.close();  
                  //   Populate   the   form   bean   with   the   results   for   display   in   the   View  
                  hff.setFname(fname);  
                  hff.setSize(size);  
   
                  //   Clean   up   our   toys   when   done   playing  
                  file.destroy();  
   
                  //   Forward   to   default   display  
                  return   mapping.findForward("success");  
   
          }  
  Top

6 楼fasi123()回复于 2006-03-07 13:11:55 得分 0

楼上的朋友,代码好像不行啊。。。Top

7 楼fasi123()回复于 2006-03-07 13:17:32 得分 0

没错,是偶错怪楼上的高人啦。偶马上结帖。。。。。。Top

相关问题

  • 文件重命名
  • 重命名文件
  • 文件重命名
  • 文件重命名
  • perl怎样实现批量文件重命名!
  • 请教:在ASP.net中如何实现文件重命名呢?
  • c#中文件重命名怎样实现?
  • unix系统可以实现对文件的重命名吗?
  • 怎样重命名文件?
  • 怎样重命名文件

关键词

  • 文件
  • struts
  • view
  • streamout
  • streamin
  • bytesread
  • hff
  • htmlfileform
  • fname
  • uploaded

得分解答快速导航

  • 帖主:fasi123
  • zhh1981

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo