文件操作

package slydsq;

import corpinfflat.jObject;
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.Comparator;
import java.util.Collections;
import org.apache.tools.zip.*;
import java.sql.*;

public class file_class
    extends jObject {
  private String[] pathNameList;
  private static Object parseInt;
  public file_class() {
    ydsq_conn ydsq = ydsq_conn.getInstance();
    setcon(ydsq.getdbconn());
  }

  public String createFolder() {
    boolean result = false;
    int i = 0;
    String root = pubrequest.getParameter("root");
    String curpath = pubrequest.getParameter("curpath");
    String folderPath = pubrequest.getParameter("newname");
    try {
      if (root != null) {
        root = new String(root.getBytes("8859_1"), "GBK");
      }
      if (!root.endsWith("/")) {
        root += "/";
      }
      if (curpath != null) {
        curpath = new String(curpath.getBytes("8859_1"), "GBK");
      }
      if (!curpath.endsWith("/")) {
        curpath += "/";
      }
      if (folderPath != null) {
        folderPath = new String(folderPath.getBytes("8859_1"), "GBK");
      }
    }
    catch (UnsupportedEncodingException ex) {
    }
    if (curpath.equals("/")) {
      curpath = "";
    }
    File f = new File(root + curpath + folderPath);
    result = f.mkdirs();
    String old = folderPath;
    while (!result) {
      folderPath = old;
      i++;
      String s = String.valueOf(i);
      old = folderPath;
      folderPath = folderPath + "(" + s + ")";
      File f1 = new File(root + curpath + folderPath);
      result = f1.mkdirs();
    }

    return "/gxkj_left.jsp?curpath=" + curpath + folderPath + "&root=" + root;
  }

  /**创建文件**/
  public static boolean makeFile(String filepath) throws IOException {
    boolean result = false;
    File filen = new File(filepath);
    result = filen.createNewFile();
    filen = null;
    return result;
  }

  /**
   * 删除文件,目录
   * @param filepath:文件所在物理路径
   * @return
   */
  public String Del_File(String curpath) {
    File file = new File(curpath);
    if ( (file == null) || !file.isDirectory()) {
      file.delete();
    }
    else {
      File[] entries = file.listFiles();
      for (int i = 0; i < entries.length; i++) {
        if (entries[i].isDirectory()) {
          Del_File(entries[i].getAbsolutePath());
        }
        else {
          entries[i].delete();
        }
      }
      file.delete();
    }
    return "";
  }

  public String deleteFile() {
    String files = pubrequest.getParameter("selectfile");
    String dyjg = pubrequest.getParameter("dyjg");
    String usercode = pubrequest.getParameter("usercode");
    String root = pubrequest.getParameter("root");
    String curpath = pubrequest.getParameter("curpath");
    try {
      if (root != null) {
        root = new String(root.getBytes("8859_1"), "GBK");
      }
      if (!root.endsWith("/")) {
        root += "/";
      }
      if (curpath != null) {
        curpath = new String(curpath.getBytes("8859_1"), "GBK");
      }
      if (!curpath.endsWith("/")) {
        curpath += "/";
      }
      if (files != null) {
        files = new String(files.getBytes("8859_1"), "GBK");
      }
    }
    catch (UnsupportedEncodingException ex) {
    }
//    if (curpath.equals("/")) {
//      curpath = "";
//    }
    String[] file = files.split(split1);
    try {
      conn.setAutoCommit(false);
      ArrayList filelist = new ArrayList();
      for (int i = 0; i < file.length; i++) {
        this.Del_File(root + curpath + file[i]);
        String sql = "delete from wjsc where wjwz='" + curpath + "' and wjm='" +
            file[i] + "' and dyjg='" + dyjg + "'";
        st.executeUpdate(sql);
      }
      conn.commit();
    }
    catch (SQLException ex1) {
      try {
        conn.rollback();
      }
      catch (SQLException ex2) {
      }
      ex1.printStackTrace();
    }

    pubrequest.setAttribute("op_flag", "yes");
    pubrequest.setAttribute("url",
                            "ydxyservlet?methodname=gxkj&type=kjfile&curpath=" +
                            curpath +
                            "&root=" + root + "&dyjg=" + dyjg + "&usercode=" +
                            usercode);
    return "/result.jsp";
  }

  /**
   * 文件重命名
   * @param filepath:文件所在物理路径
   * @param destname:新文件名
   * @return
   */
//  private void deleteDirectory(File file) {
//  }

  public String renamefile() {
    String root = pubrequest.getParameter("root");
    String curpath = pubrequest.getParameter("curpath");
    String usercode = pubrequest.getParameter("usercode");
    String dyjg = pubrequest.getParameter("dyjg");
    String newname = pubrequest.getParameter("newname");
    try {
      if (root != null) {
        root = new String(root.getBytes("8859_1"), "GBK");
      }
      if (!root.endsWith("/")) {
        root += "/";
      }
      if (curpath != null) {
        curpath = new String(curpath.getBytes("8859_1"), "GBK");
      }
      if (!curpath.endsWith("/")) {
        curpath += "/";
      }
      if (newname != null) {
        newname = new String(newname.getBytes("8859_1"), "GBK");
      }
    }
    catch (UnsupportedEncodingException ex) {
    }
    if (curpath.equals("/")) {
      curpath = "";
    }
    boolean result = false;
    if (newname.equals("")) {
      this.Del_File(root + curpath);
      try {
        conn.setAutoCommit(false);
        String sql = "delete from wjsc where wjwz='" + curpath + "' and dyjg='" +
            dyjg + "'";
        st.executeUpdate(sql);
        conn.commit();
      }
      catch (SQLException ex1) {

      }

      result = true;
      nextjsp = "/gxkj_left.jsp?curpath=/&root=" + root + "&usercode=" +
          usercode + "&dyjg=" + dyjg;
    }
    else {
      File f = new File(root + curpath);
      String fileParent = f.getParent();
      String filename = f.getName();
      File rf = new File(fileParent + "/" + newname);
      if (f.renameTo(rf)) {
        result = true;
      }
      f = null;
      rf = null;

      if (result == true) {
        nextjsp = "/gxkj_left.jsp?curpath=" + fileParent + "/" + newname +
            "&root=" + root + "&usercode=" + usercode + "&dyjg=" + dyjg;
      }
      else {
        nextjsp = "/gxkj_left.jsp?curpath=" + curpath + "&root=" + root +
            "&usercode=" + usercode + "&dyjg=" + dyjg;
      }
    }
    return nextjsp;
  }

  /**显示所有文件
   *
   */
  public String showfile(String path) {
    File file = new File(path);
    String[] fileNameList = file.list(); //得到该目录下的所有文件名,包括目录
    int indexfile = 0;
    String[] fileList;
    int filejia_num = 0;
    int file_num = 0;
//    ArrayList list = new ArrayList();
    ArrayList dirlist = new ArrayList();
    ArrayList filelist = new ArrayList();
    String cur = "";
    if (fileNameList != null) {
      for (int i = 0; i < fileNameList.length; i++) {
        File tempfile = new File(path + "/" + fileNameList[i]);
        cur = path + "/" + fileNameList[i];
        if (tempfile.isDirectory()) {
          dirlist.add(tempfile.getName());
        }
        else {
          filelist.add(tempfile.getName() + split1 +
                       formattime(tempfile.lastModified()) + split1 +
                       formatsize(tempfile.length()) + "KB");
        }
      }
    }
    String filestr = "";
    for (int i = 0; i < filelist.size(); i++) {
      if (filestr.equals("")) {
        filestr = (String) filelist.get(i);
      }
      else {
        filestr += split2 + filelist.get(i);
      }
    }
    filelist = null;
    String dirstr = "";
    for (int i = 0; i < dirlist.size(); i++) {
      if (dirstr.equals("")) {
        dirstr = (String) dirlist.get(i);
      }
      else {
        dirstr += split2 + dirlist.get(i);
      }
    }
    dirlist = null;
    String split3 = String.valueOf("/003");
    pubrequest.setAttribute("filelist", filestr);
    pubrequest.setAttribute("dirlist", dirstr);
    return dirstr + split3 + filestr;
  }

  /**显示所有文件
   *
   */
  public String getfile(String path) {
    String page = pubrequest.getParameter("page");
    int rowcount = 10;
    int curpage = 0;
    if (null == page) {
      curpage = 0;
    }
    else {
      curpage = Integer.parseInt(page);
    }
    if (curpage < 0) {
      curpage = 0;
    }
    File file = new File(path);
    String[] fileNameList = file.list(); //得到该目录下的所有文件名,包括目录
    int indexfile = 0;
    String[] fileList;
    int filejia_num = 0;
    int file_num = 0;
    int maxpage = 0;
    ArrayList filelist = new ArrayList();
    String cur = "";
    if (fileNameList != null) {
      for (int i = 0; i < fileNameList.length; i++) {
        File tempfile = new File(path + "/" + fileNameList[i]);
        cur = path + "/" + fileNameList[i];
        if (!tempfile.isDirectory()) {
          filelist.add(tempfile.getName() + split1 +
                       formattime(tempfile.lastModified()) + split1 +
                       formatsize(tempfile.length()) + "KB");
        }
      }
      maxpage = (filelist.size() / rowcount) +
          (filelist.size() % rowcount > 0 ? 1 : 0);
      if (curpage > maxpage) {
        curpage = maxpage - 1;
      }
    }
    String filestr = "";
    for (int i = curpage * rowcount, k = 0; i < filelist.size() && k < rowcount;
         i++, k++) {
      if (filestr.equals("")) {
        filestr = (String) filelist.get(i);
      }
      else {
        filestr += split2 + filelist.get(i);
      }
    }
    String split4 = String.valueOf("/004");
    pubrequest.setAttribute("maxpage", maxpage + "");
    pubrequest.setAttribute("pages", curpage + "");
    pubrequest.setAttribute("filelist", filestr);
    long total = getUserTotal();
    long usertotal = getUserKJSize();
    long retotal = total * 1024 - usertotal;
    pubrequest.setAttribute("total", total + "");
    pubrequest.setAttribute("usertotal", usertotal + "");
    pubrequest.setAttribute("resize", retotal + "");
    if (filestr == null || filestr.equals("")) {
      filestr = split4;
    }
    filelist = null;
    return "/gxkj_main.jsp";
  }

  /**显示所有文件
   *
   */
  public String getdir(String path) {
    File file = new File(path);
    String[] fileNameList = file.list(); //得到该目录下的所有文件名,包括目录
    int indexfile = 0;
    String[] fileList;
    int filejia_num = 0;
    int file_num = 0;
//    ArrayList list = new ArrayList();
    ArrayList dirlist = new ArrayList();
    ArrayList filelist = new ArrayList();
    String cur = "";
    if (fileNameList != null) {
      for (int i = 0; i < fileNameList.length; i++) {
        File tempfile = new File(path + "/" + fileNameList[i]);
        cur = path + "/" + fileNameList[i];
        if (tempfile.isDirectory()) {
          dirlist.add(tempfile.getName());
        }
      }
    }

    String dirstr = "";
    String split4 = String.valueOf("/004");
    for (int i = 0; i < dirlist.size(); i++) {
      if (dirstr.equals("")) {
        dirstr = (String) dirlist.get(i);
      }
      else {
        dirstr += split2 + dirlist.get(i);
      }
    }
    dirlist = null;
    String split3 = String.valueOf("/003");
    if (dirstr == null || dirstr.equals("")) {
      dirstr = split4;
    }
    return "&" + dirstr + "&";
  }

  public String downloadFiles() {
    String files = pubrequest.getParameter("selectfile");
    String root = pubrequest.getParameter("root");
    String curpath = pubrequest.getParameter("curpath");
    try {
      if (root != null) {
        root = new String(root.getBytes("8859_1"), "GBK");
      }
      if (!root.endsWith("/")) {
        root += "/";
      }
      if (curpath != null) {
        curpath = new String(curpath.getBytes("8859_1"), "GBK");
      }

      if (!curpath.endsWith("/")) {
        curpath += "/";
      }
      if (files != null) {
        files = new String(files.getBytes("8859_1"), "GBK");
      }
    }
    catch (UnsupportedEncodingException ex) {
    }
    if (curpath.equals("/")) {
      curpath = "";
    }
    String[] file = files.split(split1);
    if (file.length == 1) {
      pubrequest.setAttribute("filename", root + curpath + file[0]);
      pubrequest.setAttribute("delflag", "false"); //下载后是否删除临时文件
      pubrequest.setAttribute("opefile", "false");
    }
    else {
      ArrayList filelist = new ArrayList();
      for (int i = 0; i < file.length; i++) {
        filelist.add(root + curpath + file[i]);
      }
      try {
        String filename = this.CreateZip(filelist);
        pubrequest.setAttribute("filename", filename);
        pubrequest.setAttribute("delflag", "true"); //下载后是否删除临时文件
        pubrequest.setAttribute("opefile", "false");
      }
      catch (IOException ex1) {
      }
    }
    return "/kjdownload.jsp";
  }

  public static void main(String[] args) throws IOException {
    file_class f = new file_class();
    f.Del_File("d:/temp");
  }

  private String formattime(long date) {
    String time = "";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    time = sdf.format(new java.util.Date(date));
    return time;
  }

  public long getUserTotal() { //取共享空间限制大小
    int k = 0;
    String sql = "select paravalue from syspara where paracode='003'";
    ResultSet rs = null;
    try {
      rs = st.executeQuery(sql);
      while (rs.next()) {
        k = rs.getInt(1);
      }
      rs.close();
      rs = null;
    }
    catch (SQLException ex) {
    }
    return k;
  }

  public long getUserKJSize() {
    int k = 0;
    String sql = "";
    ResultSet rs = null;
    File file = null;
    String root = pubrequest.getParameter("root");
    String usercode = pubrequest.getParameter("usercode");
    String dyjg = pubrequest.getParameter("dyjg");
    sql = "select wjwz,wjm from wjsc where dyjg='" +
        dyjg + "'";
    try {
      rs = st.executeQuery(sql);
      while (rs.next()) {
        file = new File(root + rs.getString(1) + rs.getString(2));
        if(file.exists())
        k += formatsize(file.length());
      }
      rs.close();
      rs = null;
    }
    catch (SQLException ex) {
    }
    return k;
  }

  public long formatsize(long filesize) {
    String size = "";
    int k = ( (int) filesize / 1024) + ( (int) filesize % 1024 > 0 ? 1 : 0);
    return k;
  }

  public String browseFile() {
    String files = pubrequest.getParameter("selectfile");
    String root = pubrequest.getParameter("root");
    String curpath = pubrequest.getParameter("curpath");
    try {
      if (root != null) {
        root = new String(root.getBytes("8859_1"), "GBK");
      }
      if (!root.endsWith("/")) {
        root += "/";
      }
      if (curpath != null) {
        curpath = new String(curpath.getBytes("8859_1"), "GBK");
      }
      if (!curpath.endsWith("/")) {
        curpath += "/";
      }
      if (files != null) {
        files = new String(files.getBytes("8859_1"), "GBK");
      }
    }
    catch (UnsupportedEncodingException ex) {
    }
    if (curpath.equals("/")) {
      curpath = "";
    }
    String filename = files.substring(files.lastIndexOf("/") + 1);
    String ext = files.substring(filename.lastIndexOf("."));
    String filetype = "";
    if (ext.toLowerCase().equals(".doc")) {
      filetype = "application/msword";
    }
    if (ext.toLowerCase().equals(".xls")) {
      filetype = "application/vnd.ms-ms-excel; charset=gb2312";
    }
    if (ext.equals(".ppt")) {
      filetype = "application/vnd.ms-powerpoint; charset=gb2312";
    }
    if (ext.toLowerCase().equals(".gif")) {
      filetype = "image/gif; charset=gb2312";
    }
    if (ext.toLowerCase().equals(".bmp")) {
      filetype = "image/bmp";
    }
    if (ext.toLowerCase().equals(".jpg")) {
      filetype = "image/jpeg; charset=gb2312";
    }
    if (ext.toLowerCase().equals(".jpeg")) {
      filetype = "image/jpeg; charset=gb2312";
    }
    pubrequest.setAttribute("name", filename);
    pubrequest.setAttribute("mineType", filetype);
    pubrequest.setAttribute("filename", root + curpath + files);
    pubrequest.setAttribute("opefile", "true");
    pubrequest.setAttribute("delflag", "false");
    return "/browfile.jsp";
  }

  public String CreateZip(ArrayList files) throws IOException {
    double k = Math.random();
    String root = pubrequest.getParameter("root");
    String curpath = pubrequest.getParameter("curpath");
    try {
      if (root != null) {
        root = new String(root.getBytes("8859_1"), "GBK");
      }
      if (!root.endsWith("/")) {
        root += "/";
      }
      if (curpath != null) {
        curpath = new String(curpath.getBytes("8859_1"), "GBK");
      }

      if (!curpath.endsWith("/")) {
        curpath += "/";
      }
//      if (files != null) {
//        files = new String(files.getBytes("8859_1"), "GBK");
//      }
    }
    catch (UnsupportedEncodingException ex) {
    }
    if (curpath.equals("/")) {
      curpath = "";
    }
    String filename = Double.toString(k) + ".zip";
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(root +
        curpath + filename));
    ZipEntry ze = null;
    byte[] buf = new byte[1024];
    int readLen = 0;
    for (int i = 0; i < files.size(); i++) {
      File f = new File( (String) files.get(i));
      //创建一个ZipEntry,并设置Name和其它的一些属性
      //String fn=new String(((String)files.get(i)).getBytes("GBK"),"8859_1");
      ze = new ZipEntry(f.getName());
      ze.setSize(f.length());
      ze.setTime(f.lastModified());
//将ZipEntry加到zos中,再写入实际的文件内容
      zos.putNextEntry(ze);
      InputStream is = new BufferedInputStream(new FileInputStream(f));
      while ( (readLen = is.read(buf, 0, 1024)) != -1) {
        zos.write(buf, 0, readLen);
      }
      is.close();
      //System.out.println("   done...");
    }
    zos.close();
//      for (int i = 0; i < files.size(); i++) {
//        File f = new File( (String) files.get(i));
//        if (f.exists()) {
//          f.delete();
//        }
//      }
    return root + curpath + filename;

  }

  public String CreateZip(String[] files) throws IOException {

    //List fileList = getSubFiles(new File(baseDir));
    double k = Math.random();

    String filename = Double.toString(k) + ".zip";
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(filename));
    ZipEntry ze = null;
    byte[] buf = new byte[1024];
    int readLen = 0;
    for (int i = 0; i < files.length; i++) {
      File f = new File( (String) files[i]);
      //创建一个ZipEntry,并设置Name和其它的一些属性
      ze = new ZipEntry(f.getName());
      ze.setSize(f.length());
      ze.setTime(f.lastModified());
//将ZipEntry加到zos中,再写入实际的文件内容
      zos.putNextEntry(ze);
      InputStream is = new BufferedInputStream(new FileInputStream(f));
      while ( (readLen = is.read(buf, 0, 1024)) != -1) {
        zos.write(buf, 0, readLen);
      }
      is.close();
      //System.out.println("   done...");
    }
    zos.close();
//      for (int i = 0; i < files.length; i++) {
//        File f = new File( (String) files[i]);
//        if (f.exists()) {
//          f.delete();
//        }
//      }
    return filename;
  }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值