关闭

上传到文件夹下的类

524人阅读 评论(0) 收藏 举报

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();
  }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:93758次
    • 积分:1107
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:8篇
    • 译文:0篇
    • 评论:29条
    最新评论
    java基础知识