Search in sources :

Example 1 with FileBean

use of com.zyf.bean.FileBean in project HeartWatchdogJsp by Viczyf.

the class FileServlet method downloadFile.

public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    FileDao filedao = new FileDao();
    String strId = request.getParameter("id");
    int id = MyTools.strToint(strId);
    HttpSession session = request.getSession();
    User user = (User) session.getAttribute("user");
    int uid = user.getUid();
    FileBean fileSingle = filedao.queryFileSingle(id, uid);
    request.setAttribute("filesingle", fileSingle);
    RequestDispatcher rd = request.getRequestDispatcher("filedownload.jsp");
    rd.forward(request, response);
}
Also used : User(com.zyf.bean.User) HttpSession(javax.servlet.http.HttpSession) FileBean(com.zyf.bean.FileBean) FileDao(com.zyf.dao.FileDao) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 2 with FileBean

use of com.zyf.bean.FileBean in project HeartWatchdogJsp by Viczyf.

the class FileDao method queryFileSingle.

public FileBean queryFileSingle(int id, int uid) {
    FileBean filebean = null;
    String sql = "select * from file where id=" + id + " and uid=" + uid;
    ResultSet rs = this.connection.executeQuery(sql);
    try {
        if (rs.next()) {
            filebean = new FileBean();
            filebean.setId(rs.getInt(1));
            filebean.setUid(rs.getInt(2));
            filebean.setFileAddr(rs.getString(3));
            filebean.setFileDate(rs.getString(4));
            filebean.setFileInfo(rs.getString(5));
        }
    } catch (SQLException var6) {
        var6.printStackTrace();
    }
    return filebean;
}
Also used : SQLException(java.sql.SQLException) FileBean(com.zyf.bean.FileBean) ResultSet(java.sql.ResultSet)

Example 3 with FileBean

use of com.zyf.bean.FileBean in project HeartWatchdogJsp by Viczyf.

the class FileServlet method addFile.

public void addFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // String filePath = "upload\\\\file\\\\";
    String filePath = "upload/file/";
    String messages = "";
    String forward = "";
    String href = "";
    FileDao fileDao = new FileDao();
    FileBean fileBean = new FileBean();
    SmartUpload su = new SmartUpload();
    long maxsize = 2097152L;
    HttpSession session = request.getSession();
    User user = (User) session.getAttribute("user");
    int id = user.getUid();
    try {
        su.initialize(this.getServletConfig(), request, response);
        su.setMaxFileSize(maxsize);
        su.setAllowedFilesList("jpg,gif,png,txt,doc,pdf,docx");
        su.upload();
        String fileinfo = su.getRequest().getParameter("info");
        if (fileinfo != null && !fileinfo.equals("")) {
            com.jspsmart.upload.File file = su.getFiles().getFile(0);
            if (!file.isMissing()) {
                String fileAddr = filePath + id + "_" + fileDao.queryMaxId() + "." + file.getFileExt();
                System.out.println(fileAddr);
                String now = MyTools.changeTime(new Date());
                fileBean.setUid(id);
                fileBean.setFileAddr(fileAddr);
                fileBean.setFileDate(now);
                fileBean.setFileInfo(fileinfo);
                boolean mark = fileDao.operationFile("upload", fileBean);
                if (mark) {
                    try {
                        file.saveAs(fileAddr, 1);
                        messages = "上传文件成功!";
                        forward = "tishi.jsp";
                        href = "fileupload.jsp";
                    } catch (SmartUploadException var18) {
                        messages = "上传文件失败!";
                        forward = "tishi.jsp";
                        href = "fileupload.jsp";
                        var18.printStackTrace();
                    }
                } else {
                    messages = "保存文件信息失败!";
                    forward = "tishi.jsp";
                    href = "fileupload.jsp";
                }
            } else {
                messages = "请选择要上传的文件!";
                forward = "tishi.jsp";
                href = "fileupload.jsp";
            }
        } else {
            messages = "请输入照片描述信息!";
            forward = "tishi.jsp";
            href = "fileupload.jsp";
        }
    } catch (SecurityException var19) {
        messages = "<li>上传文件失败!上传的文件类型只允许为:jpg,gif,png,txt,doc,docx,pdf;且文件大小不超过2MB</li>";
        forward = "tishi.jsp";
        href = "fileupload.jsp";
    } catch (SmartUploadException var20) {
        messages = "上传文件失败!";
        forward = "tishi.jsp";
        href = "fileupload.jsp";
        var20.printStackTrace();
    }
    request.setAttribute("message", messages);
    request.setAttribute("href", href);
    RequestDispatcher requestDispatcher1 = request.getRequestDispatcher(forward);
    requestDispatcher1.forward(request, response);
}
Also used : User(com.zyf.bean.User) HttpSession(javax.servlet.http.HttpSession) Date(java.util.Date) RequestDispatcher(javax.servlet.RequestDispatcher) SmartUpload(com.jspsmart.upload.SmartUpload) SmartUploadException(com.jspsmart.upload.SmartUploadException) FileBean(com.zyf.bean.FileBean) FileDao(com.zyf.dao.FileDao)

Example 4 with FileBean

use of com.zyf.bean.FileBean in project HeartWatchdogJsp by Viczyf.

the class FileServlet method deleteFile.

public void deleteFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String messages = "";
    String forward = "";
    String href = "";
    RequestDispatcher rd = null;
    FileDao fileDao = new FileDao();
    int id = MyTools.strToint(request.getParameter("id"));
    HttpSession session = request.getSession();
    User user = (User) session.getAttribute("user");
    int uid = user.getUid();
    String fileAddr = fileDao.queryFileSingle(id, uid).getFileAddr();
    String fileDir = request.getSession().getServletContext().getRealPath("/");
    System.out.println(fileDir);
    String delFile = fileDir + fileAddr;
    System.out.println(delFile);
    File file = new File(delFile);
    FileBean fileBean = new FileBean();
    fileBean.setId(id);
    if (fileDao.operationFile("delete", fileBean)) {
        boolean result = file.delete();
        if (result) {
            messages = "<li>删除文件成功!</li>";
            forward = "tishi.jsp";
            href = "FileServlet?action=list";
        } else {
            messages = "<li>删除文件失败!</li>";
            forward = "tishi.jsp";
            href = "FileServlet?action=list";
        }
    } else {
        messages = "<li>删除文件信息失败!</li>";
        forward = "tishi.jsp";
        href = "FileServlet?action=list";
    }
    request.setAttribute("message", messages);
    request.setAttribute("href", href);
    rd = request.getRequestDispatcher(forward);
    rd.forward(request, response);
}
Also used : User(com.zyf.bean.User) HttpSession(javax.servlet.http.HttpSession) FileBean(com.zyf.bean.FileBean) FileDao(com.zyf.dao.FileDao) File(java.io.File) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 5 with FileBean

use of com.zyf.bean.FileBean in project HeartWatchdogJsp by Viczyf.

the class FileDao method queryFile.

public List queryFile(int uid) {
    String sql = "";
    sql = "select * from file where uid='" + uid + "' order by id desc";
    ArrayList list = null;
    FileBean filebean = null;
    ResultSet rs = this.connection.executeQuery(sql);
    if (rs != null) {
        list = new ArrayList();
        try {
            while (rs.next()) {
                filebean = new FileBean();
                filebean.setId(rs.getInt(1));
                filebean.setUid(rs.getInt(2));
                filebean.setFileAddr(rs.getString(3));
                filebean.setFileDate(rs.getString(4));
                filebean.setFileInfo(rs.getString(5));
                list.add(filebean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return list;
}
Also used : SQLException(java.sql.SQLException) FileBean(com.zyf.bean.FileBean) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet)

Aggregations

FileBean (com.zyf.bean.FileBean)5 User (com.zyf.bean.User)3 FileDao (com.zyf.dao.FileDao)3 RequestDispatcher (javax.servlet.RequestDispatcher)3 HttpSession (javax.servlet.http.HttpSession)3 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 SmartUpload (com.jspsmart.upload.SmartUpload)1 SmartUploadException (com.jspsmart.upload.SmartUploadException)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1