熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

JSP如何實現文件上傳

2022-06-13   來源: JSP教程 

  程序如下

  package linkmanhelper;

  import javaio*;

  import javautil*;

  import javaxservlet

  import javaxservlet

  import javaxservletServletInputStream;

  public class UpLoadBean {

  private String SavePath = null;

  private String FileName = null;

  private String Sperator = null;

  private String ErrMsg = "正常返回";

  private byte[] bt = new byte[];

  private byte[] prebt = new byte[];

  private int t = ;

  private long FileSize = ;

  private boolean UserDefineFileName = false;

  /////////////////////////////////////////////////

  public UpLoadBean () {

  FileName = bringFileName ();

  }

  public UpLoadBean (String savepath) {

  SavePath = savepath;

  FileName = bringFileName ();

  }

  /////////////////////////////////////////////////

  // 文件名信息

  private String bringFileName () {

  Calendar dt = CalendargetInstance ();

  String str = "" + dtget (CalendarYEAR) + dtget (CalendarMONTH) + dtget (CalendarDAY_OF_MONTH);

  str = str + dtget (CalendarHOUR) + dtget (CalendarMINUTE) + dtget (CalendarSECOND);

  return str;

  }

  public void setFileName (String filename) {

  FileName = filename;

  UserDefineFileName = true;

  }

  public String getFileName () {

  return FileName;

  }

  /////////////////////////////////////////////////

  // 上傳文件主程序

  public boolean UpLoad (HttpServletRequest request) throws Exception {

  if (SavePath == null) {

  setErrorMessage ("未設置保存上傳文件的路徑");

  return false;

  }

  requestsetCharacterEncoding("utf");

  ServletInputStream in = requestgetInputStream ();

  ////////////////////////////////////////////

  // 獲取得一行的分隔標志

  t = inreadLine (bt btlength);

  if (t != ) {

  Sperator = new String (bt t);

  Sperator = Speratorsubstring ( );

  t = ;

  }

  ////////////////////////////////////////////

  // 取擴展名並設定文件全名

  do {

  t = inreadLine (bt btlength);

  String s = new String (bt t);

  int index = sindexOf ("filename=\"");

  if (index != ) {

  s = ssubstring (index + );

  index = sindexOf ("\"");

  s = ssubstring ( index);

  index = slastIndexOf ("");

  if (!UserDefineFileName) {

  FileName = FileName + ssubstring (index); // 設定文件全名

  }

  t = ;

  }

  } while (t != );

  ////////////////////////////////////////////

  // 取文件內容

  FileOutputStream out = new FileOutputStream (SavePath + "\\" + FileName);

  t = inreadLine (bt btlength);

  String s = new String (bt t);

  int i = sindexOf ("ContentType:");

  if (i == ) {

  setErrorMessage ("上傳的不是文件");

  return false;

  } else {

  inreadLine (bt btlength); // 去掉一個空行

  t = ;

  }

  boolean state = true;

  long trancsize = ;

  try {

  t = inreadLine (bt btlength);

  while (t != ) {

  s = new String (bt t);

  if (slength () > ) {

  s = ssubstring ( );

  if (sequals (Sperator)) {

  break;

  }

  }

  /*

  if (bt[] == && bt[] == && bt[] == && bt[] == && bt[] == ) {

  break;

  }

  */

  if (FileSize != ) {

  if (trancsize >= FileSize) {

  throw new Exception ("上傳文件太大");

  }

  }

  outwrite (bt t);

  trancsize += t;

  t = inreadLine (bt btlength);

  }

  } catch (Exception e) {

  state = false;

  outclose ();

  setErrorMessage (egetMessage ());

  File tmp = new File (SavePath FileName);

  if (!tmpdelete()) {

  setErrorMessage (egetMessage () + "清除未完成");

  }

  } finally {

  if (state) {

  outclose ();

  }

  return state;

  }

  }

  /////////////////////////////////////////////////

  // 上傳文件錯誤信息

  public void setErrorMessage (String errormessage) {

  ErrMsg = errormessage;

  }

  public String getErrorMessage () {

  return ErrMsg;

  }

  /////////////////////////////////////////////////

  // 文件大小信息

  public void setFileSize (long size) {

  FileSize = size;

  }

  public long getFileSize () {

  return FileSize;

  }

  }// end class


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20210.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.