空间站

北极心空

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks

在Action里面取得路径:
String pathMyDir = this.getServlet().getServletContext().getRealPath("/uploads/");

File targetFolder = new File(pathMyDir);
        if (targetFolder.isFile() || !targetFolder.exists())
       {
            targetFolder.mkdir();
       }

为避免重复文件名
FormFile file = theForm.getTheFile();

String fileName= file.getFileName();
            
int i = fileName.lastIndexOf(".");
      if(i != -1)
     {
        fileName = fileName.substring(i);
     }else
     {
        fileName = ".bin";
      }
     fileName = new Date().getTime()+fileName;

最后别忘了file.destroy();

其他的就用struts里面自带的例子搞定.
InputStream stream = file.getInputStream();
OutputStream bos = new FileOutputStream(pathMyDir+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();



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=250969

posted on 2007-08-06 10:56 芦苇 阅读(793) 评论(0)  编辑  收藏 所属分类: Struts

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


网站导航: