熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

JAVA 物理路徑上傳附件功能

2013-11-23 19:26:01  來源: Java核心技術 

  public ActionForward add(ActionMapping mapping ActionForm form

  HttpServletRequest request HttpServletResponse response) {

  TruckBaseForm tbf = (TruckBaseForm)form;

  FormFile filename = tbfgetFilename();

  String tempname = ;

  if ( filenamegetFileName()trim()length() > ){

  //           取得上傳時間

  Date dt = new Date() ;

  long updatetime=dtgetTime();

  try{

  //開始上傳文件

  String filePath = thisgetServlet()getServletContext()getRealPath(/) ; //取得當前路徑

  InputStream stream = filenamegetInputStream() ; //把文件讀入

  // ByteArrayOutputStream baos = new ByteArrayOutputStream() ;

  int position = filenamegetFileName()indexOf() ;

  String type = filenamegetFileName()substring(position);

  tempname =updatetime+type ;

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

  //requestsetAttribute(fileNamefilePath + / + CharFiltertoChinese((String)filenamegetFileName()) ) ;

  int bytesRead = ;

  byte[] buffer = new byte[] ;

  while( (bytesRead = streamread(buffer) ) !=   ){

  boswrite(bufferbytesRead) ;

  }

  bosclose();

  streamclose() ;

  //上傳文件完成

  }

  catch(Exception e){

  efillInStackTrace();

  }

  }

  TruckBase tb = thismakeTruckBase(tbftempname);

  //tbSEVdeleteTruckBase(tb);

  int truckid = IntegerparseInt(+requestgetParameter(truckid));

  //保存後返回的int是主鍵的ID記錄過的(包括刪除過的id的個數)

  int result = thistbSEVsaveTruckBase(tb);

  List list = new ArrayList();

  //保存後再撈一遍數據

  list = tbSEVgetAllTruckBase(truckid);

  requestsetAttribute(truckbaseListlist);

  list = (ArrayList)dataSEVgetDataDictionaryList();

  requestsetAttribute(dataListlist);

  TruckCard td = new TruckCard();

  td = thistcSEVgetTruckCard(truckid);

  requestsetAttribute(truckCardList td);

  return mappingfindForward(add);

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26855.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.