Upload servlet

原创 2012年03月27日 23:05:25

package com.fendou.archive.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.fendou.archive.dao.ArchiveDao;
import com.fendou.archive.dao.UserDao;
import com.fendou.archive.po.Archive;
import com.fendou.archive.po.CArchive;
import com.fendou.archive.po.User;
import com.fendou.archive.service.vo.ArchiveVo;
import com.fendou.authority.dao.CommonDao;
import com.fendou.authority.dao.DepartmentDao;
import com.fendou.authority.dao.MajorDao;
import com.fendou.authority.po.Common;
import com.fendou.authority.po.Department;
import com.fendou.authority.po.Major;
import com.fendou.common.ConvertUtils;
import com.fendou.engage.dao.IResumeDao;
import com.fendou.platform.PageUtil;
import com.fendou.salary.dao.ISalaryStandardDao;
import com.fendou.salary.po.SalaryStandard;

public class ArchiveServiceDaoImpl implements ArchiveServiceDao {
   
 private ArchiveDao archivedao;
 private DepartmentDao deptDao;
 private MajorDao majorDao;
 private CommonDao commondao;
 private ISalaryStandardDao iSalaryStandardDao;
 private UserDao userdao;
    private Department department;   //作为全局,为了,审核时,要user要set部门。
 public void setUserdao(UserDao userdao) {
  this.userdao = userdao;
 }
 public void setiSalaryStandardDao(ISalaryStandardDao iSalaryStandardDao) {
  this.iSalaryStandardDao = iSalaryStandardDao;
 }
 public void setArchivedao(ArchiveDao archivedao) {
  this.archivedao = archivedao;
 }
 public void setDeptDao(DepartmentDao deptDao) {
  this.deptDao = deptDao;
 }

 public void setMajorDao(MajorDao majorDao) {
  this.majorDao = majorDao;
 }

 public void setCommondao(CommonDao commondao) {
  this.commondao = commondao;
 }

 
     /*
     * (non-Javadoc)
     * @see com.fendou.archive.service.impl.ArchiveServiceDao#addArchive(com.fendou.archive.service.vo.ArchiveVo, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
 
 public void addArchive(ArchiveVo archivevo,String path,String IpTimeStamp,String name1,String name2) {
  Archive archive=new Archive();
  ConvertUtils.convertVoToPo(archivevo, archive);
  try {
   /*
    * 转换日期
    */
   Date date = new Date();
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   archive.setBirthday(sdf.parse(archivevo.getBirth()));
   
   /*
    * 转换部门
    */
           int deptId = archivevo.getDepartmentId();
           department = (Department) deptDao.get(deptId);
           archive.setDepartment(department);
          
           /*
    * 职位转换
    */
   int majorId = archivevo.getMajorId();
   Major major = (Major) majorDao.get(Major.class,majorId);
   archive.setMajor(major);
   
   /*
    *  公共属性
    */
   int nationalityId = archivevo.getNationalityId();
   Common nationality = (Common) commondao.get(nationalityId);
   archive.setNationality(nationality);
   
   int partyId  =archivevo.getPartyId();
   Common party = (Common) commondao.get(partyId);
   archive.setParty(party);
   
   int educatedDegreeId = archivevo.getEducatedDegreeId();
   Common educatedDegree = (Common)commondao.get(educatedDegreeId);
   archive.setEducatedDegree(educatedDegree);
   
   int raceId = archivevo.getRaceId();
   Common race =(Common)commondao.get(raceId);
   archive.setRace(race);
   
   int religionId=archivevo.getReligionId();
   Common religion=(Common)commondao.get(religionId);
   archive.setReligion(religion);
   
   int sexId = archivevo.getSexId();
   Common sex=(Common)commondao.get(sexId);
   archive.setSex(sex);
   
   int salaryId =archivevo.getSalaryStandardId();
   SalaryStandard  s =iSalaryStandardDao.isSalaryStandard(salaryId);
   archive.setSalaryStandard(s);
   if(!archivevo.getUid().equals("") &&archivevo.getUid()!=null){
       archive.setRegister(userdao.get(Integer.parseInt(archivevo.getUid())));
   }   
   
     
     
   /**
    * 档案附件和照片上传
    */
   Upload(archivevo, path, IpTimeStamp, name1,name2);
   
   archive.setRegistTime(new Date());
   archive.setState("0");//表示未审核。
   archive.setFileStatus("1");//1表示一个状态,
   archive.setPicture("../images/uploadpicture/"+IpTimeStamp+name1);
   archive.setAttachment("../images/uploadpicture/"+IpTimeStamp+name1);
            archivedao.addArchive(archive);
      
  
  } catch (ParseException e) {
   e.printStackTrace();
  } catch (FileNotFoundException e) { 
   e.printStackTrace();
  } catch (IOException e) { 
   e.printStackTrace();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
  
 }
 /**
  * upload
  * @param archivevo
  * @param path
  * @param IpTimeStamp
  * @param ext
  * @throws FileNotFoundException
  * @throws IOException
  */
 public void Upload(ArchiveVo archivevo, String path, String IpTimeStamp,
   String name1,String name2) throws FileNotFoundException, IOException {
  
  if(archivevo.getAttachments()!=null && archivevo.getPictures()!=null){
   
   InputStream contentStram = archivevo.getPictures().getInputStream();
     File file = new File(path.concat("images/uploadpicture/"),IpTimeStamp+name1);
     FileOutputStream fos = new FileOutputStream(file);
     byte[] buf = new byte[1024];
     int length=0;
    while((length=contentStram.read(buf))!=-1){
     fos.write(buf,0,length); 
    }
    contentStram.close();
    fos.flush();
    fos.close();
   
    InputStream contentStram2 = archivevo.getAttachments().getInputStream();
    File file2 = new File(path.concat("images/uploadpicture/"),IpTimeStamp+name2);
    FileOutputStream fos2 = new FileOutputStream(file2);
    byte[] buf2 = new byte[1024];
     int length2=0;
    while((length2=contentStram2.read(buf2))!=-1){
      fos2.write(buf2,0,length2); 
     }
    contentStram2.close();
    fos2.flush();
    fos2.close();
  }if(archivevo.getAttachments()!=null && archivevo.getPictures()==null){
   
   InputStream contentStram2 = archivevo.getAttachments().getInputStream();
   File file2 = new File(path.concat("images/uploadpicture/"),IpTimeStamp+name2);
   FileOutputStream fos2 = new FileOutputStream(file2);
   byte[] buf2 = new byte[1024];
    int length2=0;
   while((length2=contentStram2.read(buf2))!=-1){
     fos2.write(buf2,0,length2); 
    }
   contentStram2.close();
   fos2.flush();
   fos2.close(); 
  }if(archivevo.getAttachments()==null && archivevo.getPictures()!=null){
    InputStream contentStram = archivevo.getPictures().getInputStream();
    File file = new File(path.concat("images/uploadpicture/"),IpTimeStamp+name1);
    FileOutputStream fos = new FileOutputStream(file);
    byte[] buf = new byte[1024];
    int length=0;
   while((length=contentStram.read(buf))!=-1){
    fos.write(buf,0,length); 
   }
   contentStram.close();
   fos.flush();
   fos.close();
  }
   
  }
  
  
 
 public void deleteArchive(int id) {
  archivedao.deleteArchive(id);
 }
 
 public void deleteCArchive(int cid) {
  archivedao.deleteCArchive(cid); 
 }
 
 public void updateArchive(ArchiveVo archivevo, Archive archive,String path, String IpTimeStamp,
   String name1,String name2) {
  ConvertUtils.convertVoToPo(archivevo, archive);
   /*
    * 转换日期
    */
   Date date = new Date();
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   try {
    archive.setBirthday(sdf.parse(archivevo.getBirth()));
   
   
   /*
    * 转换部门
    */
           int deptId = archivevo.getDepartmentId();
           Department department = (Department) deptDao.get(deptId);
           archive.setDepartment(department);
          
           /*
    * 职位转换
    */
   int majorId = archivevo.getMajorId();
   Major major = (Major) majorDao.get(Major.class,majorId);
   archive.setMajor(major);
   
   /*
    *  公共属性
    */
   int nationalityId = archivevo.getNationalityId();
   Common nationality = (Common) commondao.get(nationalityId);
   archive.setNationality(nationality);
   
   int partyId  =archivevo.getPartyId();
   Common party = (Common) commondao.get(partyId);
   archive.setParty(party);
   
   int educatedDegreeId = archivevo.getEducatedDegreeId();
   Common educatedDegree = (Common)commondao.get(educatedDegreeId);
   archive.setEducatedDegree(educatedDegree);
   
   int raceId = archivevo.getRaceId();
   Common race =(Common)commondao.get(raceId);
   archive.setRace(race);
   
   int religionId=archivevo.getReligionId();
   Common religion=(Common)commondao.get(religionId);
   archive.setReligion(religion);
   
   int salaryId =archivevo.getSalaryStandardId();
   SalaryStandard  s =iSalaryStandardDao.isSalaryStandard(salaryId);
   archive.setSalaryStandard(s);
   if(!archivevo.getUid().equals("") && archivevo.getUid()!=null){
      archive.setChanger(userdao.get(Integer.parseInt(archivevo.getUid())));
   }
   archive.setChangeTime(new Date());
   
   
   /**
    * 档案
             */
            Upload(archivevo, path, IpTimeStamp, name1,name2);
   archive.setChangeTime(new Date());
   archive.setPicture("../images/uploadpicture/"+IpTimeStamp+name1);
   archive.setAttachment("../images/uploadpicture/"+IpTimeStamp+name1);
   archivedao.updateArchive(archive);
   
   
   } catch (ParseException e) {
    e.printStackTrace();
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
 }

 public void updateState(int id ,int userId,String info) {
  Archive archive = new Archive();
  archive=archivedao.get(id);
  
  if("1".equals(info)){
     archive.setState("1");
       if(userId!=0){
          archive.setChecker(userdao.get(userId));
       }
    
  }else if("2".equals(info)){
      archive.setState("2"); //通过
      if(userId!=0){
          archive.setChecker(userdao.get(userId));
       }
      User user = new User();
      user.setDepartment(department);
      user.setArchive(archive);
      user.setCreateTime(new Date());
      user.setUpdateTime(new Date());
      user.setStatus("0");
  
      //把此对象放入档案中,建立关系。
         userdao.addUser(user);
  }
  archivedao.updateState(archive);

 }
 
 public List<?> getAll() {
  return archivedao.getAll();
 }
 public List<?> searchPage(PageUtil page, String hql) {
  return archivedao.searchPage(page, hql);
 }
 public int totalcount(String hql) {
    return archivedao.totalcount(hql);
 }
 
 public Archive get(int id) {
  return archivedao.get(id);
 }
 
 public CArchive getC(int cid) {
  return archivedao.getC(cid);
 }
 public void updateFileState(int id,String info) {
  Archive archive = new Archive();
  archive=archivedao.get(id);
  if("0".equals(info)){
   archive.setFileStatus("0");//表示删除
  }else if("1".equals(info)){
   archive.setFileStatus("1");//表示恢复
  }
  archivedao.updateArchive(archive);
 }
 
 public void addCarchive(CArchive carchive,int uid) {
  
  
  //carchive.setCheckStatus(checkStatus)
  carchive.setChanger(userdao.get(uid));
  carchive.setChangeTime(new Date());
  archivedao.addCArchive(carchive);
 }
 public void addresumetoArchive(Archive archive) {
  archivedao.addArchive(archive);
  
 }
 //QBC
 public List<Archive> selectByQBC(HashMap<Object, Object> filter,
   String Currentpage) {
  return archivedao.selectByQBC(filter, Currentpage);
 }
 public PageUtil getPageUtil() {
  return archivedao.getPageUtil();
 }

}

相关文章推荐

Servlet - Upload、Download、Async、动态注册

Upload、Download、Async标签 : Java与WebUpload-上传 随着3.0版本的发布,文件上传终于成为Servlet规范的一项内置特性,不再依赖于像Commons File...

Upload file to servlet without using HTML form

It’s trivial to use a web form which is made by HTML code for uploading files from web browser to a ...

servlet3 upload上传文件实践

一、javax.servlet.http中Part接口public interface Part 要上传文件必须使用multipart/form-data作为request body。版本: Se...

七牛云图片上传:使用element-ui的upload组件

昨天问了一下element团队,他们还没有试过用element-ui的upload组件传图片到七牛云 我试了一天发现没问题,同时觉得七牛并没有一个前端后端流程都演示到的demo,于是再次记录一波...

django之对FileField字段的upload_to的设定

用django开发,经常要处理用户上传的文件, 比如user模型里面如果又个人头像的字段 ImageField等等,而django在FielField字段(包括ImageField)的支持和扩展是做的...

基于session.upload_progress 的文件上传进度显示

介绍session.upload_progress 是PHP5.4的新特征。 当 session.upload_progress.enabled INI 选项开启时,PHP 能够在每一个文件上传时...
  • koastal
  • koastal
  • 2016年10月31日 11:05
  • 1395

修改php文件上传的大小限制upload_max_filesize

Question当php脚本上传的文件大小超过php.ini中post_max_size,upload_max_filesize的限制的时候页面不会给出提醒,文件也上传失败。 php.ini 配置对...
  • yi_an
  • yi_an
  • 2016年11月23日 16:29
  • 4989

Angular2 ng2-file-upload使用FileReader在网页中显示用户选择图片

ng2-file-upload是一个angular2的文件上传指令插件,可支持拖拽上传、单选上传、多选上传等功能,具体使用方法可去github查看。地址:https://github.com/valo...

Nginx Upload Module 上传模块测试例子

今天学习Nginx Upload Module模块的应用,下面我就在学习过程中对(www.111cn.net)于Nginx Upload Module模块的一些笔记,希望此文章对你会有所帮助。from...

WordPress Import 上传的文件尺寸超过php.ini中定义的upload_max_filesize值-->解决方法。

参考一: WordPress Importer上传导入备份文件时遇到这样一个错误,提示“上传的文件尺寸超过 php.ini 中定义的 upload_max_filesize 值”。问题原因是ph...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Upload servlet
举报原因:
原因补充:

(最多只允许输入30个字)