⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileupload.java

📁 初学者学习用的文件上传代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   *MIME傪庢傞
   *@return String
   *@param ContentType String
   *@since 01-00
   */
  private String getTypeMIME(String ContentType) {
    String value = new String();
    int pos = 0;
    pos = ContentType.indexOf("/");
    if (pos != -1) {
      return ContentType.substring(1, pos);
    }
    else {
      return ContentType;
    }
  }

  /**
   *SubTypeMIME傪庢傞
   *@return String
   *@param ContentType String
   *@since 01-00
   */
  private String getSubTypeMIME(String ContentType) {
    String value = new String();
    int start = 0;
    int end = 0;
    start = ContentType.indexOf("/") + 1;
    if (start != -1) {
      end = ContentType.length();
      return ContentType.substring(start, end);
    }
    else {
      return ContentType;
    }
  }

  /**
   *ContentDisp傪庢傞
   *@return String
   *@param dataHeader String
   *@since 01-00
   */
  private String getContentDisp(String dataHeader) {
    String value = new String();
    int start = 0;
    int end = 0;
    start = dataHeader.indexOf(":") + 1;
    end = dataHeader.indexOf(";");
    value = dataHeader.substring(start, end);
    return value;
  }

  /**
   *m_start偲m_end傪庢傞
   *@since 01-00
   */
  private void getDataSection() {
    boolean found = false;
    String dataHeader = new String();
    int searchPos = m_currentIndex;
    int keyPos = 0;
    int boundaryLen = m_boundary.length();
    m_startData = m_currentIndex;
    m_endData = 0;
    do {
      if (searchPos >= m_totalBytes) {
        break;
      }
      if (m_binArray[searchPos] == (byte) m_boundary.charAt(keyPos)) {
        if (keyPos == boundaryLen - 1) {
          m_endData = ( (searchPos - boundaryLen) + 1) - 3;
          break;
        }
        searchPos++;
        keyPos++;
      }
      else {
        searchPos++;
        keyPos = 0;
      }
    }
    while (true);
    m_currentIndex = m_endData + boundaryLen + 3;
  }

  /**
   *DataHeader傪庢傞
   *@return String
   *@since 01-00
   */
  private String getDataHeader() {
    int start = m_currentIndex;
    int end = 0;
    int len = 0;
    boolean found = false;

    while (!found) {
      if (m_binArray[m_currentIndex] == 13 &&
          m_binArray[m_currentIndex + 2] == 13) {
        found = true;
        end = m_currentIndex - 1;
        m_currentIndex = m_currentIndex + 2;
      }
      else {
        m_currentIndex++;
      }
    }
    String dataHeader = new String(m_binArray, start, (end - start) + 1);
    String eee = "";
    eee = eee + (char) m_binArray[start];
    String ddd = "";
    ddd = ddd + (char) m_binArray[end];
    return dataHeader;
  }

  /**
   *僼傽僀儖柤傪庢傞
   *@param filePathName String
   *@return String
   *@since 01-00
   */
  private String getFileName(String filePathName) {
    String token = new String();
    String value = new String();
    int pos = 0;
    int i = 0;
    int start = 0;
    int end = 0;
    pos = filePathName.lastIndexOf(47);
    if (pos != -1) {
      return filePathName.substring(pos + 1, filePathName.length());
    }
    pos = filePathName.lastIndexOf(92);
    if (pos != -1) {
      return filePathName.substring(pos + 1, filePathName.length());
    }
    else {
      return filePathName;
    }
  }

  /**
   *僼傽僀儖柤傪庢傞
   *@return String
   *@since 01-00
   */
  public String getpFileName() {
    return pFileName;
  }

  /**
   *僼傽僀儖ext柤傪庢傞
   *@return String
   *@since 01-00
   */
  public String getpFileExt() {
    return pFileExt;
  }

  /**
   *僼傽僀儖柤傪偣乕偲
   *@param fn 僼傽僀儖柤
   *@since 01-00
   */
  private void setpFileName(String fn) {
    pFileName = fn;
  }

  /**
   *僼傽僀儖ext柤傪偣乕偲
   *@param fe String 僼傽僀儖ext柤
    *@since 01-00
   */
  private void setpFileExt(String fe) {
    pFileExt = fe;
  }

  /**
   *僼傽僀儖path柤傪偣乕偲
   *@param fp 僼傽僀儖path柤
   *@since 01-00
   */
  private void setpFilePath(String fp) {
    pFilePath = fp;
  }

  /**
   *僼傽僀儖path柤傪偣乕偲
   *@return String
   *@since 01-00
   */
  public String getpFilePath() {
    return pFilePath;
  }

  /**
   *栚榐傪僠儏僢僋偡傞
   *@return boolean
   *@param directionaryName String
   *@since 01-00
   */
  public boolean checkDir(String directionaryName) {
    char posd = ' ';
    for (int i = 0; i < directionaryName.length(); i++) {
      posd = directionaryName.charAt(i);
      if (posd == '\\' || posd == '/' || posd == ':' || posd == ';' ||
          posd == '*' ||
          posd == '?' || posd == '\"' || posd == '<' || posd == '>' ||
          posd == '|') {
        return false;
      }
    }
    return true;
  }

  /**
   *栚榐傪嶍彍偡傞
   *@param DirPath 栚榐
   *@since 01-00
   */
  public void cleanDir(String DirPath) {
    java.io.File dirName = new java.io.File(DirPath);
    if (dirName.exists()) {
      int fileCount = dirName.list().length;
      java.io.File fileList[] = new java.io.File[fileCount];
      fileList = dirName.listFiles();
      for (int i = 0; i < fileList.length; i++) {
        if (fileList[i].isFile()) {
          fileList[i].delete();
        }
      }
    }
  }

  /**
   *栚榐傪怴婯偡傞
   *@param DirPath 栚榐
   *@since 01-00
   */
  public void createDir(String DirPath) {
    java.io.File dirName = new java.io.File(DirPath);
    if (!dirName.exists()) {
      dirName.mkdir();
    }
  }

  /**
   *upload僼傽僀儖傪専嶕偡傞
   *@return String
   *@param path String
   *@since 01-00
   */
  public String scanUploadedFile(String path) {
    String existFile = new String();
    existFile = "";
    java.io.File dirName = new java.io.File(path);
    if (dirName.exists()) {
      java.io.File fileList[] = new java.io.File[dirName.list().length];
      fileList = dirName.listFiles();
      for (int i = 0; i < fileList.length; i++) {
        if (fileList[i].isFile()) {
          existFile = fileList[i].getName();
        }
      }
    }
    else {
      existFile = "";
    }
    return existFile;
  }

  /**
   *僼傽僀儖傪copy
   *@param sourPath String
   *@param destPath String
   *@throws BP0600_CommonEx
   *@since 01-00
   */
  public void copyFile(String sourPath, String destPath) throws BP0600_CommonEx {
    try {
      java.io.File sourFile = new java.io.File(sourPath);
      java.io.File destFile = new java.io.File(destPath);
      int s=0;
      BufferedInputStream in1 = new BufferedInputStream(new FileInputStream(sourFile));
      BufferedOutputStream out1 = new BufferedOutputStream(new FileOutputStream(destPath));
      while ((s=in1.read())!=-1) {
        out1.write(s);
      }
      in1.close();
      out1.close();
    }
    catch (Exception e) {
      String className = this.getClass().getName();
      String MethodName = "copyFile";

      BP0300_OutputErrinfoBean errBean = new BP0300_OutputErrinfoBean();
      String errorTime = errBean.getSysDateTime();
      errBean.setErrCode("BP_COM_E_004");
      errBean.setErrDescription(className + ":" + MethodName + ":" +
                                BP0500_CommonErr.BP_COM_E_004 + e.getMessage());
      errBean.writeErrlogFile();

      BP0600_CommonEx commonex = new BP0600_CommonEx(className, MethodName,
          "BP_COM_E_004", "儊儌儕乕僆乕僶乕僼儘乕", e.getMessage());
      throw (commonex);
    }
  }

  /**
   *栚榐傪怴婯
   *@return String
   *@param DirPath String
   *@since 01-00
   */
  public String createTempDir(String DirPath) { //DirPath must exist
    int MaxV = 0;
    java.io.File dirName = new java.io.File(DirPath);
    if (dirName.exists()) {
      int fileCount = dirName.list().length;
      java.io.File fileList[] = new java.io.File[fileCount];
      fileList = dirName.listFiles();
      for (int i = 0; i < fileList.length; i++) {
        if (fileList[i].isDirectory()) {
          if (MaxV <= Integer.parseInt(fileList[i].getName())) {
            MaxV = MaxV + 1;
          }
        }
      }
      java.io.File tempDirName = new java.io.File(DirPath + "/" +
                                                  Integer.toString(MaxV));
      tempDirName.mkdir();
    }
    return Integer.toString(MaxV);
  }

  /**
   *僼傽僀儖偺僙僢僗傪庢傞
   *@return int KB
   */

  public int getFileSize(){
    return m_totalBytes/1024;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -