首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>java教程>资讯:JAVA物理路径上传附件功能

JAVA物理路径上传附件功能

www.jz123.cn  2010-01-07   来源:   中国建站    责任编辑(袁袁)    我要投递新闻

  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() ;
  //上传文件完成
  }
  catch(Exception e){
  e.fillInStackTrace();
  }
  }
  TruckBase tb = this.makeTruckBase(tbf,tempname);
  //tbSEV.deleteTruckBase(tb);
  int truckid = Integer.parseInt(""+request.getParameter("truckid"));
  //保存后返回的int是主键的ID,记录过的(包括删除过的id的个数)
  int result = this.tbSEV.saveTruckBase(tb);
  List list = new ArrayList();
  //保存后再捞一遍数据
  list = tbSEV.getAllTruckBase(truckid);
  request.setAttribute("truckbaseList",list);
  list = (ArrayList)dataSEV.getDataDictionaryList();
  request.setAttribute("dataList",list);
  TruckCard td = new TruckCard();
  td = this.tcSEV.getTruckCard(truckid);
  request.setAttribute("truckCardList", td);
  return mapping.findForward("add");
  }


上一篇:普通Java类获取Spring的Bean的方法 下一篇:Java多线程优化之偏向锁原理分析

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图