UserFileAction.java
资源名称:(J2EE)oa.rar [点击查看]
上传用户:lm2018
上传日期:2015-12-12
资源大小:30449k
文件大小:9k
源码类别:
Jsp/Servlet
开发平台:
Java
- //Created by MyEclipse Struts
- // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.0/xslt/JavaClass.xsl
- package com.oa.module.folder.action;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.apache.struts.actions.DispatchAction;
- import org.apache.struts.upload.FormFile;
- import com.oa.module.folder.bean.FileBean;
- import com.oa.module.folder.bean.FolderBean;
- import com.oa.module.folder.dao.FileDao;
- import com.oa.module.folder.dao.FolderDao;
- import com.oa.module.folder.form.FileForm;
- import com.oa.module.office.user.Tuser;
- import com.oa.util.PubUtil;
- /**
- * MyEclipse Struts Creation date: 01-28-2008
- *
- * XDoclet definition:
- *
- * @struts.action path="/userFile" name="fileForm" parameter="method"
- * scope="request" validate="true"
- */
- public class UserFileAction extends DispatchAction {
- // --------------------------------------------------------- Instance
- // Variables
- private FileDao dao;
- private FolderDao folderdao;
- public FolderDao getFolderdao() {
- return folderdao;
- }
- public void setFolderdao(FolderDao folderdao) {
- this.folderdao = folderdao;
- }
- /**
- * 进入文件夹
- *
- * @param mppaing
- * @param form
- * @param request
- * @param response
- */
- public ActionForward enterForder(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- FileForm fileForm = (FileForm) form;
- String id = fileForm.getForderid();
- List list = dao.getFileListByForderId(id);
- HttpSession session = request.getSession();
- Tuser user = (Tuser)session.getAttribute("user");
- List forderList = folderdao.getUserFolder(user.getUno());
- request.setAttribute("myfileList", list);
- request.setAttribute("userforderList", forderList);
- return mapping.findForward("myfileList");
- }
- /**
- * 上传文件
- *
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward uploadfile(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- FileForm fileForm = (FileForm) form;
- FormFile formfile = fileForm.getFormfile();
- FileBean filebean = new FileBean();
- filebean.setForderid(fileForm.getForderid());
- boolean flag = false;
- int size = formfile.getFileSize();
- if ( size > 0) {
- size = formfile.getFileSize();
- if(size > 5 * 1024 * 1024) {
- try {
- PrintWriter out = response.getWriter();
- out.print("<script>");
- out.print("alert('文件不能大于5M');");
- out.print("history.back();");
- out.print("</script>");
- return null;
- } catch (IOException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- }
- filebean.setFilesize(size+"");
- FolderBean bean = folderdao.getForderByForderId(fileForm.getForderid());
- String filename = formfile.getFileName();
- filebean.setFilename(filename);
- HttpSession session = request.getSession();
- Tuser user = (Tuser) session.getAttribute("user");
- String path = session.getServletContext().getRealPath("");
- path = path.replace('\', '/');
- String filepath = "/forder/"+user.getUname() + "," + user.getUno()+ "/"+bean.getFordername();
- String realpath = path+filepath+"/"+filename;
- filebean.setRealpath(filepath);
- try {
- InputStream input = formfile.getInputStream();
- OutputStream output = new FileOutputStream(realpath);
- byte[] b = new byte[1024];
- int i = 0;
- while( (i=input.read(b, 0, b.length )) != -1 ){
- output.write(b, 0, i);
- }
- output.close();
- output.close();
- input.close();
- } catch (FileNotFoundException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (IOException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- filebean.setAddtime(PubUtil.getNowDate());
- flag = dao.addFile(filebean);
- }else{
- PrintWriter out;
- try {
- out = response.getWriter();
- out.print("<script>");
- out.print("alert('文件不存在');");
- out.print("history.back();");
- out.print("</script>");
- return null;
- } catch (IOException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- }
- if(flag ==true){
- //成功
- request.setAttribute("task","addfilesuc");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","addfileerror");
- return mapping.findForward("error");
- }
- }
- /**
- * 删除文件
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward delfile(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- FileForm fileForm = (FileForm) form;
- String id = fileForm.getFileid();
- //获得数据以便删除文件
- FileBean bean = dao.getFileById(id);
- //从数据库删除
- boolean flag = dao.delFileById(id);
- String path = this.getServlet().getServletContext().getRealPath("");
- path = path.replace('\', '/');
- if(flag ==true){
- //成功
- String systempath = bean.getRealpath();
- path += systempath;
- File file = new File(path+"/"+bean.getFilename());
- file.delete();
- request.setAttribute("task","delfilesuc");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","delfileerror");
- return mapping.findForward("error");
- }
- }
- /**
- * 移动文件
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return
- */
- public ActionForward moveto(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- FileForm fileForm = (FileForm) form;
- String forderid = fileForm.getForderid();
- String fileid = fileForm.getFileid();
- String movetofiledid = request.getParameter("movetofiledid");
- //要用到的地方
- FolderBean movetoforderbean = folderdao.getForderByForderId(movetofiledid);
- String path = this.getServlet().getServletContext().getRealPath(""); //d:webroot
- path = path.replace('\','/');
- //原来的
- FolderBean forderbean = folderdao.getForderByForderId(forderid);
- FileBean filebean = dao.getFileById(fileid);
- String filename = filebean.getFilename(); //aa.txt
- String realname = forderbean.getFordername(); //asdf
- //旧文件路径
- String oldfilepath = path+filebean.getRealpath()+"/"+filename;
- //新文件路径
- HttpSession session = request.getSession();
- Tuser user = (Tuser)session.getAttribute("user");
- String newfilepath = path+"/forder/"+user.getUname()+","+user.getUno()+"/"+movetoforderbean.getFordername()+"/"+filename;
- System.out.println("newfilepath:"+newfilepath);
- System.out.println("oldfilepath:"+oldfilepath);
- InputStream input = null;
- OutputStream output = null;
- boolean flag = false;
- try {
- input = new FileInputStream(oldfilepath);
- output = new FileOutputStream(newfilepath);
- byte[] b = new byte[1024];
- int i = 0;
- while( (i= input.read(b, 0 , b.length)) != -1){
- System.out.println(i);
- output.write(b, 0, i);
- }
- output.flush();
- output.close();
- boolean t = false;
- try {
- File file = new File(oldfilepath);
- t=file.delete();
- System.out.println(t);
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- //数据库操作
- filebean.setForderid(movetofiledid);
- filebean.setRealpath("/forder/"+user.getUname()+","+user.getUno()+"/"+movetoforderbean.getFordername()+"/"+filename);
- flag =dao.addFile(filebean);
- flag =dao.delFileById(fileid);
- } catch (FileNotFoundException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- } catch (IOException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }finally{
- try {
- input.close();
- output.close();
- } catch (IOException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- }
- if(flag ==true){
- //成功
- request.setAttribute("task","movefilesuc");
- return mapping.findForward("suc");
- }else{
- request.setAttribute("task","movefileerror");
- return mapping.findForward("error");
- }
- }
- public FileDao getDao() {
- return dao;
- }
- public void setDao(FileDao dao) {
- this.dao = dao;
- }
- }