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");
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpdsy.html