jsp
Controller.java
/**
*文件压缩下载
*billname:文件名
*filename:文件存放路径
*/
@RequestMapping("/zipfileDownload_workerCard")
public void zipfileDownload_workerCard(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException{
//响应头的设置
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
//设置压缩包的名字
//解决不同浏览器压缩包名字含有中文时乱码的问题
HttpSession session = request.getSession();
String billname ="workerCard-"+session.getAttribute("projectWorktypeId");
String downloadName = billname+".zip";
//返回客户端浏览器的版本号、类型
String agent = request.getHeader("USER-AGENT");
try {
//针对IE或者以IE为内核的浏览器:
if (agent.contains("MSIE")||agent.contains("Trident")) {
downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8");
} else {
//非IE浏览器的处理:
downloadName = new String(downloadName.getBytes("UTF-8"),"ISO-8859-1");
}
} catch (Exception e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\"");
//设置压缩流:直接写入response,实现边压缩边下载
ZipOutputStream zipos = null;
try {
zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
zipos.setMethod(ZipOutputStream.DEFLATED); //设置压缩方法
} catch (Exception e) {
e.printStackTrace();
}
//循环将文件写入压缩流
DataOutputStream os = null;
Worker worker = new Worker();
worker.setProjectWorktypeId((Integer)session.getAttribute("projectWorktypeId"));
//从数据库中取出要下载的图片路径、并循环写入压缩
List workerList = workerService.findAllWorkerByWhere(worker);
for (Worker worker2 : workerList) {
String filename =worker2.getWorkerScanFront();
String removeStr = "/upload/workerCard";
filename = filename.replace(removeStr, "");//去掉相对路径中的两个目录路径
String modipath = request.getSession().getServletContext().getRealPath("/upload/workerCard"+filename);
File file = new File(modipath);
if(file.exists()){
try {
//添加ZipEntry,并ZipEntry中写入文件流
//这里,加上i是防止要下载的文件有重名的导致下载失败
zipos.putNextEntry(new ZipEntry(filename));
os = new DataOutputStream(zipos);
InputStream is = new FileInputStream(file);
byte[] b = new byte[100];
int length = 0;
while((length = is.read(b))!= -1){
os.write(b, 0, length);
}
is.close();
zipos.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//关闭流
try {
os.flush();
os.close();
zipos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Worker.java
package com.huahang.model;
import java.util.Date;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
public class Worker {
private Integer id;
private Integer projectWorktypeId;
private String workerName;
private Integer workerAge;
private String workerPhone;
private String workerCard;
private String workerScanFront;
private String workerScanBack;
private String bankType;
private String bankNumber;
private Integer status;
private String createBy;
private Date createTime;
private String modifyBy;
private Date modifyTime;
private Integer leader;
private String corpCode;
private Integer deleted;
private Integer version;
private Integer count;
private String image;
private MultipartFile file;
private String templeteName;
private String contractContent;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getProjectWorktypeId() {
return projectWorktypeId;
}
public void setProjectWorktypeId(Integer projectWorktypeId) {
this.projectWorktypeId = projectWorktypeId;
}
public String getWorkerName() {
return workerName;
}
public void setWorkerName(String workerName) {
this.workerName = workerName;
}
public Integer getWorkerAge() {
return workerAge;
}
public void setWorkerAge(Integer workerAge) {
this.workerAge = workerAge;
}
public String getWorkerPhone() {
return workerPhone;
}
public void setWorkerPhone(String workerPhone) {
this.workerPhone = workerPhone;
}
public String getWorkerCard() {
return workerCard;
}
public void setWorkerCard(String workerCard) {
this.workerCard = workerCard;
}
public String getWorkerScanFront() {
return workerScanFront;
}
public void setWorkerScanFront(String workerScanFront) {
this.workerScanFront = workerScanFront;
}
public String getWorkerScanBack() {
return workerScanBack;
}
public void setWorkerScanBack(String workerScanBack) {
this.workerScanBack = workerScanBack;
}
public String getBankType() {
return bankType;
}
public void setBankType(String bankType) {
this.bankType = bankType;
}
public String getBankNumber() {
return bankNumber;
}
public void setBankNumber(String bankNumber) {
this.bankNumber = bankNumber;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getModifyBy() {
return modifyBy;
}
public void setModifyBy(String modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
public Integer getLeader() {
return leader;
}
public void setLeader(Integer leader) {
this.leader = leader;
}
public String getCorpCode() {
return corpCode;
}
public void setCorpCode(String corpCode) {
this.corpCode = corpCode;
}
public Integer getDeleted() {
return deleted;
}
public void setDeleted(Integer deleted) {
this.deleted = deleted;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
public String getTempleteName() {
return templeteName;
}
public void setTempleteName(String templeteName) {
this.templeteName = templeteName;
}
public String getContractContent() {
return contractContent;
}
public void setContractContent(String contractContent) {
this.contractContent = contractContent;
}
@Override
public String toString() {
return "Worker [id=" + id + ", projectWorktypeId=" + projectWorktypeId
+ ", workerName=" + workerName + ", workerAge=" + workerAge
+ ", workerPhone=" + workerPhone + ", workerCard=" + workerCard
+ ", workerScanFront=" + workerScanFront + ", workerScanBack="
+ workerScanBack + ", bankType=" + bankType + ", bankNumber="
+ bankNumber + ", status=" + status + ", createBy=" + createBy
+ ", createTime=" + createTime + ", modifyBy=" + modifyBy
+ ", modifyTime=" + modifyTime + ", leader=" + leader
+ ", corpCode=" + corpCode + ", deleted=" + deleted
+ ", version=" + version + ", count=" + count + ", image="
+ image + ", file=" + file + ", templeteName=" + templeteName
+ ", contractContent=" + contractContent + "]";
}
}
WorkerMapper.java
package com.huahang.dao;
import java.util.List;
import com.huahang.model.Worker;
public interface WorkerMapper {
public List findAllWorkerByWhere(Worker worker);
public Worker findWorkerById(Integer id);
public List findLabContractById(Integer id);
public List findPageData(Worker worker);
public int addWorker(Worker worker);
public int updateWorkerById(Worker worker);
public int deleteWorkerById(Integer id);
public int deleteContractContentById(Integer id);
public int deleteMenuBatch(List ids);
public int banWorker(Integer id);//
public int arouseWorker(Integer id);
public int updateSqlpath(Worker worker);
public int updateContractSqlpath(Worker worker);
public List findAllBankType();
}
WorkerMapper.xml
select * ,(select count(*) from m_worker_project where 1=1
and worker_name like '%${workerName}%'
and project_worktype_id=#{projectWorktypeId}
and id=#{id}
and worker_phone=#{workerPhone}
and worker_card=#{workerCard}
and deleted=0
) as 'count'
from m_worker_project where 1=1
and worker_name like '%${workerName}%'
and project_worktype_id=#{projectWorktypeId}
and id=#{id}
and worker_phone=#{workerPhone}
and deleted=0
order by CASE WHEN leader = 1 THEN 100 ELSE 1000 END
select * from m_worker_project where id=#{id} and deleted=0
select id,templete_name from m_templete where deleted=0
select worker_scan_front,worker_scan_back from m_worker_project where delete=0
and project_worktype_id=#{projectWorktypeId}
and id in
#{item}
update m_worker_project set deleted=1 where id in
#{item}
update m_worker_project set status=1 where id =#{id}
update m_worker_project set status=0 where id =#{id}
insert into m_worker_project (project_worktype_id,
worker_name,worker_age,worker_phone,worker_card,
worker_scan_front,worker_scan_back,bank_type,
bank_number,status,create_by,create_time,modify_by,
modify_time,leader,corp_code,deleted,version)
values (#{projectWorktypeId},#{workerName},
#{workerAge},#{workerPhone},#{workerCard},
#{workerScanFront},#{workerScanBack},#{bankType},
#{bankNumber},#{status},#{createBy},#{createTime},
#{modifyBy},#{modifyTime},#{leader},#{corpCode},
#{deleted},#{version})
update m_worker_project set project_worktype_id=#{projectWorktypeId},
worker_name=#{workerName},worker_age=#{workerAge},
worker_phone=#{workerPhone},worker_card=#{workerCard},
bank_type=#{bankType},bank_number=#{bankNumber},status=#{status},
modify_by=#{modifyBy},modify_time=#{modifyTime},leader=#{leader},
corp_code=#{corpCode},deleted=#{deleted},version=#{version}
where id = #{id}
update m_worker_project set worker_scan_front=#{workerScanFront},worker_scan_back=#{workerScanBack} where id=#{id}
update m_worker_project set deleted =1 where id = #{id}
update m_labor_contract set deleted =1 where id = #{id}
insert into m_labor_contract (contract_content,
worker_project_id,create_by,create_time,modify_by,
modify_time,corp_code,deleted,version)
values (#{contractContent},#{id},#{createBy},#{createTime},
#{modifyBy},#{modifyTime},#{corpCode},
#{deleted},#{version})
select * from m_labor_contract where worker_project_id=#{id} and deleted =0