package com.casic.PgInterface.patroler.manager; import com.casic.PgInterface.core.hibernate.HibernateEntityDao; import com.casic.PgInterface.patroler.domain.PgPatrol; import com.casic.PgInterface.patroler.domain.PgPatrolDoc; import org.hibernate.Criteria; import org.hibernate.criterion.Restrictions; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import sun.misc.BASE64Decoder; import javax.servlet.http.HttpServletRequest; import java.io.*; import java.util.List; /** * Created by Administrator on 2018/4/24. */ @Service public class PgPatrolDocManager extends HibernateEntityDao<PgPatrolDoc> { /*上传文件*/ public String saveFiles(String fileBuffer, String file_name, String list_name){ try { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String strDirPath = request.getSession().getServletContext().getRealPath(""); FileOutputStream fos = null; String image_toDir = strDirPath + "\\"+list_name;//存储路径 String imageName = ""; imageName += list_name+"/"+file_name; byte[] buffer = new BASE64Decoder().decodeBuffer(fileBuffer);//对android传过来的图片字符串进行解码 File destDir = new File(image_toDir); if (!destDir.exists()) destDir.mkdirs(); File imageFile = new File(destDir, file_name); fos = new FileOutputStream(imageFile);//保存文件 fos.write(buffer); fos.flush(); fos.close(); return imageName; } catch(Exception ex) { ex.printStackTrace(); return ""; } } /*下载文件*/ public ResponseEntity<byte[]> downloadFile(PgPatrolDoc pgPatrolDoc) throws Exception { try { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String path = request.getSession().getServletContext().getRealPath(""); String filePath = path + "\\"+pgPatrolDoc.getList_name() + "\\" + pgPatrolDoc.getFile_name(); File file = new File(filePath);//找到具体的文件 byte[] body = null; InputStream inputStream = new FileInputStream(file); body = new byte[inputStream.available()]; inputStream.read(body); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition",file.getName()); HttpStatus status = HttpStatus.OK; ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, status); return entity; //inputStream.close(); } catch (IOException e) { e.printStackTrace(); return null; } } public List<PgPatrolDoc> getPgPatrolDocById(PgPatrol pgPatrol) { Criteria criteria = createCriteria(PgPatrolDoc.class); criteria.add(Restrictions.eq("pgpatrol_id", pgPatrol)); criteria.add(Restrictions.eq("active", 1)); List<PgPatrolDoc> pgPatrolDocList = criteria.list(); if (pgPatrolDocList == null) return null; else return pgPatrolDocList; } }