程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> JSP如何實現文件上傳

JSP如何實現文件上傳

編輯:關於JSP

      程序如下:

      package linkman.helper;

      import java.io.*;

      import java.util.*;

      import javax.servlet.http.HttpServletRequest;

      import javax.servlet.http.HttpServletResponse;

      import javax.servlet.ServletInputStream;

      public class UpLoadBean {

      private String SavePath = null;

      private String FileName = null;

      private String Sperator = null;

      private String ErrMsg = "正常返回";

      private byte[] bt = new byte[4096];

      private byte[] prebt = new byte[4096];

      private int t = -1;

      private long FileSize = -1;

      private boolean UserDefineFileName = false;

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

      public UpLoadBean () {

      FileName = bringFileName ();

      }

      public UpLoadBean (String savepath) {

      SavePath = savepath;

      FileName = bringFileName ();

      }

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

      // 文件名信息

      private String bringFileName () {

      Calendar dt = Calendar.getInstance ();

      String str = "" + dt.get (Calendar.YEAR) + dt.get (Calendar.MONTH) + dt.get (Calendar.DAY_OF_MONTH);

      str = str + dt.get (Calendar.HOUR) + dt.get (Calendar.MINUTE) + dt.get (Calendar.SECOND);

      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;

      }

      request.setCharacterEncoding("utf-8");

      ServletInputStream in = request.getInputStream ();

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

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

      t = in.readLine (bt, 0, bt.length);

      if (t != -1) {

      Sperator = new String (bt, 0, t);

      Sperator = Sperator.substring (0, 28);

      t = -1;

      }

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

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

      do {

      t = in.readLine (bt, 0, bt.length);

      String s = new String (bt, 0, t);

      int index = s.indexOf ("filename="");

      if (index != -1) {

      s = s.substring (index + 10);

      index = s.indexOf (""");

      s = s.substring (0, index);

      index = s.lastIndexOf (".");

      if (!UserDefineFileName) {

      FileName = FileName + s.substring (index); // 設定文件全名

      }

      t = -1;

      }

      } while (t != -1);

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

      // 取文件內容

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

      t = in.readLine (bt, 0, bt.length);

      String s = new String (bt, 0, t);

      int i = s.indexOf ("Content-Type:");

      if (i == -1) {

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

      return false;

      } else {

      in.readLine (bt, 0, bt.length); // 去掉一個空行

      t = -1;

      }

      boolean state = true;

      long trancsize = 0;

      try {

      t = in.readLine (bt, 0, bt.length);

      while (t != -1) {

      s = new String (bt, 0, t);

      if (s.length () > 28) {

      s = s.substring (0, 28);

      if (s.equals (Sperator)) {

      break;

      }

      }

      /*

      if (bt[0] == 45 && bt[1] == 45 && bt[2] == 45 && bt[3] == 45 && bt[4] == 45) {

      break;

      }

      */

      if (FileSize != -1) {

      if (trancsize >= FileSize) {

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

      }

      }

      out.write (bt, 0, t);

      trancsize += t;

      t = in.readLine (bt, 0, bt.length);

      }

      } catch (Exception e) {

      state = false;

      out.close ();

      setErrorMessage (e.getMessage ());

      File tmp = new File (SavePath, FileName);

      if (!tmp.delete()) {

      setErrorMessage (e.getMessage () + ",清除未完成");

      }

      } finally {

      if (state) {

      out.close ();

      }

      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

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved