上传文件

package net.hlj.chOA.action;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.hlj.chOA.dao.DangAnDao;
import net.hlj.chOA.dao.LogDao;
import net.hlj.chOA.dao.ZiYuanDao;
import net.hlj.chOA.model.DangAn;
import net.hlj.chOA.model.User;
import net.hlj.chOA.model.ZiYuan;
import net.hlj.chOA.util.GetId;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
* @author lcy
*
*/
public class ZiYuanAction extends DispatchAction {

public ActionForward addZiYuan(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  String title = "";
  String fileupload="";
  SimpleDateFormat fmt = new SimpleDateFormat("yyMMddhhssmmSSS");
  Date now = new Date();
  String date = fmt.format(now);
  String filePath = request.getSession().getServletContext().getRealPath(
    "/");
  String uploadPath = filePath + "upload//uploadZiYuan//";
  String tempPath = filePath + "upload//uploadZiYuan//" + "uploadTemp";
  String suffix = null;
  if (!new File(uploadPath).isDirectory()) {
   new File(uploadPath).mkdirs();
  }
  DiskFileItemFactory factory = new DiskFileItemFactory();
  factory.setSizeThreshold(4194304);// 设置初始化内存,如果上传的文件超过该大小,将不保存到内存,而且硬盘中(单位:byte)
  File fileTemp = new File(tempPath);// 建立临时目录
  fileTemp.mkdir();
  factory.setRepository(fileTemp);
  ServletFileUpload upload = new ServletFileUpload(factory);
  upload.setSizeMax(4194304);// 设置客户端最大上传,-1为无限大(单位:byte)
  try {
   List<FileItem> items = upload.parseRequest(request);
   Iterator<FileItem> i = items.iterator();
   String[] rightType = {".gif", ".jpeg", ".doc", ".xls",
     ".pdf", ".txt", ".rar" };
   while (i.hasNext()) {
    FileItem fi = (FileItem) i.next();
    if (fi.isFormField()) {
     try {
      if (fi.getFieldName().equals("title")) {
       title = fi.getString("UTF-8");
      }
     } catch (UnsupportedEncodingException e) {
      log.error(e.getMessage(), e);
     }
    } else {
     String fileName = fi.getName();
     int l = fileName.length();
     if (!fileName.equals("")) {
      int pos = fileName.lastIndexOf(".");
      // suffix = fileName.substring(l - 4, l);
      suffix = fileName.substring(pos, l);

      boolean result = false;
      String ext = fileName.substring(fileName
        .lastIndexOf("."));
      for (int j = 0; j < rightType.length; j++) {
       if (ext.toLowerCase().equals(rightType[j])) {
        result = true;
        break;
       }
      }
      if (!result) {
       request.setAttribute("error", "上传文件类型有误!");
       return mapping.findForward("addZiyuan");
      }

      // if (!suffix.equalsIgnoreCase(".jpg") &&
      // !suffix.equalsIgnoreCase(".gif")
      // && !suffix.equalsIgnoreCase(".png") &&
      // !suffix.equalsIgnoreCase(".bmp")) {
      // request.setAttribute("message", "上传文件类型有误!");
      // return mapping.findForward("danganList");
      // }
      if (fileName != null) {
       File savedFile = new File(uploadPath, date + suffix);
       try {
        fi.write(savedFile);
        fileupload = "upload/uploadZiYuan/" + date
          + suffix;
       } catch (Exception e) {
        log.error(e.getMessage(), e);
       }
      }
     }
    }
   }
  } catch (FileUploadException e) {
   log.error(e.getMessage(), e);
  }
  ZiYuan zy=new ZiYuan();
  zy.setTitle(title);
  zy.setUpload(fileupload);
  ZiYuanDao zyDao=new ZiYuanDao();
  try {
   zyDao.addZiYuan(zy, this.servlet.getServletContext());
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  LogDao lDao=new LogDao();
  //删除操做添加日志 isDel 0是增加,1是删除,2是修改,3是审批
  User user1 =(User)request.getSession().getAttribute("userBean");
  String ip=request.getLocalAddr();
  int id=GetId.getId("ziyuan", this.servlet.getServletContext());
  try {
   lDao.addLogMe("ziyuan", id , ip, user1.getName(), user1.getId(), 0, this.servlet.getServletContext());
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return mapping.findForward("ziyuanList");
}

public ActionForward deleteZiYuan(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  String[] ids= request.getParameterValues("id");
  ZiYuanDao zyDao=new ZiYuanDao();
  LogDao lDao=new LogDao();
  for(int i=0;i<ids.length;i++){
   try {
    zyDao.deleteZiYuan(Integer.parseInt(ids[i]), this.servlet.getServletContext());
   } catch (NumberFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   User user =(User)request.getSession().getAttribute("userBean");
   String ip=request.getLocalAddr();
   try {
    lDao.addLogMe("tbl_users",Integer.parseInt(ids[i]), ip, user.getName(), user.getId(), 1, this.servlet.getServletContext());
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

  }
  return mapping.findForward("ziyuanList");
}

public ActionForward updateZiYuan(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, SQLException {
  String id="";
  String title = "";
  String fileupload="";
  SimpleDateFormat fmt = new SimpleDateFormat("yyMMddhhssmmSSS");
  Date now = new Date();
  String date = fmt.format(now);
  String filePath = request.getSession().getServletContext().getRealPath(
    "/");
  String uploadPath = filePath + "upload//uploadZiYuan//";
  String tempPath = filePath + "upload//uploadZiYuan//" + "uploadTemp";
  String suffix = null;
  if (!new File(uploadPath).isDirectory()) {
   new File(uploadPath).mkdirs();
  }
  DiskFileItemFactory factory = new DiskFileItemFactory();
  factory.setSizeThreshold(4194304);// 设置初始化内存,如果上传的文件超过该大小,将不保存到内存,而且硬盘中(单位:byte)
  File fileTemp = new File(tempPath);// 建立临时目录
  fileTemp.mkdir();
  factory.setRepository(fileTemp);
  ServletFileUpload upload = new ServletFileUpload(factory);
  upload.setSizeMax(4194304);// 设置客户端最大上传,-1为无限大(单位:byte)
  try {
   List<FileItem> items = upload.parseRequest(request);
   Iterator<FileItem> i = items.iterator();
   String[] rightType = {".gif", ".jpeg", ".doc", ".xls",
     ".pdf", ".txt", ".rar" };
   while (i.hasNext()) {
    FileItem fi = (FileItem) i.next();
    if (fi.isFormField()) {
     try {
      if (fi.getFieldName().equals("title")) {
       title = fi.getString("UTF-8");
      }
      if (fi.getFieldName().equals("id")) {
       id = fi.getString("UTF-8");
      }
     } catch (UnsupportedEncodingException e) {
      log.error(e.getMessage(), e);
     }
    } else {
     String fileName = fi.getName();
     int l = fileName.length();
     if (!fileName.equals("")) {
      int pos = fileName.lastIndexOf(".");
      // suffix = fileName.substring(l - 4, l);
      suffix = fileName.substring(pos, l);

      boolean result = false;
      String ext = fileName.substring(fileName
        .lastIndexOf("."));
      for (int j = 0; j < rightType.length; j++) {
       if (ext.toLowerCase().equals(rightType[j])) {
        result = true;
        break;
       }
      }
      if (!result) {
       request.setAttribute("error", "上传文件类型有误!");
       request.setAttribute("id", id);
       return mapping.findForward("selectZiyuan");
      }

      // if (!suffix.equalsIgnoreCase(".jpg") &&
      // !suffix.equalsIgnoreCase(".gif")
      // && !suffix.equalsIgnoreCase(".png") &&
      // !suffix.equalsIgnoreCase(".bmp")) {
      // request.setAttribute("message", "上传文件类型有误!");
      // return mapping.findForward("danganList");
      // }
      if (fileName != null) {
       File savedFile = new File(uploadPath, date + suffix);
       try {
        fi.write(savedFile);
        fileupload = "upload/uploadZiYuan/" + date
          + suffix;
       } catch (Exception e) {
        log.error(e.getMessage(), e);
       }
      }
     }else{
      //这里写如果用户没有重写添加附件则保持原来的附件
      ZiYuanDao zyDao = new ZiYuanDao();
      ZiYuan zy=(ZiYuan)zyDao.selectZiYuanById(Integer.parseInt(id), this.servlet.getServletContext()).get(0);
      fileupload=zy.getUpload();
     }
    }
   }
  } catch (FileUploadException e) {
   log.error(e.getMessage(), e);
  }
  ZiYuan zy=new ZiYuan();
  zy.setId(Integer.parseInt(id));
  zy.setTitle(title);
  zy.setUpload(fileupload);
  ZiYuanDao zyDao=new ZiYuanDao();
  try {
   zyDao.updateZiYuan(zy, this.servlet.getServletContext());
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  LogDao lDao=new LogDao();
  //删除操做添加日志 isDel 0是增加,1是删除,2是修改,3是审批
  User user1 =(User)request.getSession().getAttribute("userBean");
  String ip=request.getLocalAddr();
  try {
   lDao.addLogMe("ziyuan", Integer.parseInt(id) , ip, user1.getName(), user1.getId(), 2, this.servlet.getServletContext());
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  return mapping.findForward("ziyuanList");
}

}

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值