上传到文件夹下的类

来源:互联网 发布:淘宝网购流程图 编辑:程序博客网 时间:2024/05/21 22:22

package com;

import com.jspsmart.upload.*;
import com.jspsmart.*;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.jsp.*;
import java.io.File;

public class UploadFile {
  public UploadFile() {

  }

//上传函数
  public void upLoadFile(PageContext pageContext1) {
    SmartUpload su = new SmartUpload();
    //得到系统时间
    Calendar cl = Calendar.getInstance();
    int year = cl.get(Calendar.YEAR);
    int month = cl.get(Calendar.MONTH);
    int day = cl.get(Calendar.DAY_OF_MONTH);
    int hour = cl.get(Calendar.HOUR_OF_DAY);
    int minute = cl.get(Calendar.MINUTE);
    int second = cl.get(Calendar.SECOND);
    String sql = null;
    month = month + 1;
    //文件名字
    String fileTime = "" + year + month + day + hour + minute + second;
    fileTime = fileTime.trim();
    try {
      su.initialize(pageContext1);
      su.upload();
      for (int i = 0; i < su.getFiles().getCount(); i++) {
        com.jspsmart.upload.File file = su.getFiles().getFile(i);
        if (file.isMissing())
          continue;
        String extFileName = file.getFileExt(); //提取文件的后缀名
        String filename = file.getFileName(); //提取文件的名字
        String allName = fileTime + "." + extFileName; //重新对文件进行命名
        file.saveAs("upload//" + allName, su.SAVE_VIRTUAL);
        sql = "insert into picture(picname,picallname) values('" + allName +
            "','" + filename + "')";
        System.out.println(sql);
      }
      DBConnection conn = new DBConnection();
      conn.executeInsertsql(sql);
      conn.close();
    }
    catch (Exception e1) {
      System.out.println("Exception:" + e1);
    }
  }

  //删除文件
  public void deletefile(String filename,int fileid) {
    String root="E:/工程备份/jb9/shangchuan/defaultroot/upload/";
 String filepath = root+filename;
  File f = new File(filepath);
  f.delete();
   DBConnection db = new DBConnection();
    String sql = "delete from picture where id="+fileid;
   db.executeUpdate(sql);
   db.close();
  }

}