设为首页 加入收藏

TOP

JAVA物理路径上传附件功能
2014-11-14 21:30:38 来源: 作者: 【 】 浏览:17
Tags:JAVA 物理 路径 上传 附件 功能

  public ActionForward add(ActionMapping mapping, ActionForm form,


  HttpServletRequest request, HttpServletResponse response) {


  TruckBaseForm tbf = (TruckBaseForm)form;


  FormFile filename = tbf.getFilename();


  String tempname = "" ;


  if ( filename.getFileName().trim().length() > 0 ){


  // 取得上传时间


  Date dt = new Date() ;


  long updatetime=dt.getTime();


  try{


  //开始上传文件


  String filePath = this.getServlet().getServletContext().getRealPath("/") ; //取得当前路径


  InputStream stream = filename.getInputStream() ; //把文件读入


  // ByteArrayOutputStream baos = new ByteArrayOutputStream() ;


  int position = filename.getFileName().indexOf(".") ;


  String type = filename.getFileName().substring(position);


  tempname =updatetime+type ;


  OutputStream bos = new FileOutputStream(filePath + "truckimg\\" + tempname ) ;


  //request.setAttribute("fileName",filePath + "/" + CharFilter.toChinese((String)filename.getFileName()) ) ;


  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() ;


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇如何实现Ext的checkBox的横排效果 下一篇如何让Ext的RowExpander实现异步..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: