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

}

ServletFileUpload

package cn.itcast.estore.web.servlet; import java.io.BufferedInputStream; import java.io.Buffere...
  • u010509143
  • u010509143
  • 2013年06月26日 10:46
  • 1314

SpringMVC上传组件Servlet3.0和Commons-fileupload优缺点

servlet3.0支持的StandardServletMultipartResolver 是原生支持的,没有其他的依赖,使用起来方便。 commons-fileupload的CommonsMult...
  • qq_1017097573
  • qq_1017097573
  • 2017年06月02日 00:02
  • 388

servlet upload

servlet upload form Upload' enctype='multipart/form-data'> servlet InputStream inputStrea...
  • BlueDimension
  • BlueDimension
  • 2016年10月06日 23:07
  • 73

servlet upload

package com.zy.oa.util;import java.io.*;import java.util.*;import java.sql.*;import javax.servlet.ht...
  • hoton
  • hoton
  • 2005年04月18日 11:30
  • 461

Spring Boot 上传文件(spring boot upload file)

本篇文章将说明如何利用Spring Boot上传文件 。
  • gisboygogogo
  • gisboygogogo
  • 2017年08月05日 16:47
  • 829

Servlet利用Commons FileUpload实现文件上传

详情请参考Commons FileUpload官网 The Commons FileUpload package makes it easy to add robust, high-pe...
  • heirenhua
  • heirenhua
  • 2012年11月07日 20:26
  • 6438

spring boot 上传文件出错:java.io.IOException: The temporary upload location

现象: 上传excel,出现报错: [Request processing failed; nested exception is org.springframework.web.mult...
  • m386084855
  • m386084855
  • 2018年01月12日 10:55
  • 358

Spring boot(16) spring boot 线上故障 上传文件出错:org.springframework.web.multipart.MultipartException: Could

上线后,过了一段时间上传文件的时候 出现错误 org.springframework.web.multipart.MultipartException: Could not parse multipa...
  • kkgbn
  • kkgbn
  • 2017年03月09日 15:20
  • 5341

servlet3.0文件上传

要上传文件,必须利用 multipart/form-data 设置HTML表单的enctype属性值,旨在告诉服务器,向服务器发送二进制数据。 这个表单中必须包含类型file的一个输入...
  • magi1201
  • magi1201
  • 2014年11月21日 22:49
  • 3699

JSP/servlet实现文件上传下载和删除

由于存储空间,对一般用户而言,就是用来操作存储文件的,所以这两天,特意看了一下windows下用servlet实现文件上传、下载和删除,下面是详细代码说明上传:用的是commons-fileuploa...
  • aidayei
  • aidayei
  • 2011年05月26日 22:58
  • 8160
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Upload servlet
举报原因:
原因补充:

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