package com.goafanti.common.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.multipart.MultipartFile; import com.goafanti.common.bo.Result; public class FileUtils { @Value(value = "${upload.private.path}") private String uploadPrivatePath = null; /** * response 输出JSON * * @param hresponse * @param resultMap * @throws IOException */ public void out(HttpServletResponse response, String jsonStr) { PrintWriter out = null; try { response.setHeader("Content-Type", "application/json"); response.setCharacterEncoding("UTF-8"); out = response.getWriter(); out.println(jsonStr); } catch (Exception e) { LoggerUtils.fmtError(getClass(), e, "输出数据失败"); } finally { if (null != out) { out.flush(); out.close(); } } } /** * Excel报表导出 * * @param response * @param fileName * @param workbook */ public void downloadExcel(HttpServletResponse response, String fileName, HSSFWorkbook workbook) { String fn = null; try { fn = URLEncoder.encode(fileName, "UTF-8"); } catch (UnsupportedEncodingException e1) { fn = fileName; } response.setContentType("application/x-download;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fn + "\";"); OutputStream out = null; try { out = response.getOutputStream(); workbook.write(out); } catch (IOException e) { LoggerUtils.fmtError(getClass(), e, "输出Excel失败"); } finally { if (null != out) { try { out.flush(); out.close(); } catch (IOException e) { out = null; } } } } /** * 根据上传文件 拼接fileName * * @param mf * @param isPrivate * @param sign * @param path * @return */ public static String mosaicFileName(MultipartFile mf, boolean isPrivate, String sign, String path, String uid) { String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf(".")); boolean uniq = false; if (sign.indexOf("protocol") != -1 || sign.indexOf("achievement") != -1 || sign.indexOf("honor") != -1){ uniq = true; } String fileName = ""; if (isPrivate || sign != "") { if (uniq){ fileName = path + uid + "/" + System.nanoTime()+ "_" + sign + suffix; } else { fileName = path + uid + "/" + sign + suffix; } } else { fileName = path + System.nanoTime() + suffix; } return fileName; } /** * 获取下载文件名称 * * @param path * @return */ public static String getDownloadFileName(String path) { if (StringUtils.isBlank(path)) { return null; } String fileName = ""; String prefix = ""; String suffix = path.substring(path.lastIndexOf(".")); if (path.indexOf("patent_prory_statement") != -1) { prefix = "专利代理委托书"; } if (path.indexOf("patent_writing") != -1) { prefix = "专利稿件"; } if (path.indexOf("authorization_notice") != -1) { prefix = "授权通知书"; } if (path.indexOf("patent_certificate") != -1) { prefix = "专利证书"; } if (path.indexOf("roster") != -1) { prefix = subStr(path) + "花名册"; } if (path.indexOf("social_security") != -1) { prefix = subStr(path) + "社保情况"; } if (path.indexOf("honor") != -1) { prefix = "企业荣誉及材料证明"; } if (path.indexOf("achievement") != -1) { prefix = "科技成果"; } if (path.indexOf("institution") != -1) { prefix = "技术中心制度"; } if (path.indexOf("protocol") != -1) { prefix = "技术协议"; } if (path.indexOf("finance") != -1) { prefix = subStr(path) + "财务报表"; } if (path.indexOf("ratepay") != -1) { prefix = subStr(path) + "纳税申报表"; } fileName = prefix + suffix; return fileName; } /** * 下载文件 * @param res * @param fileName * @param response * @param path * @param request * @return */ public Result downloadFile(Result res, String fileName, HttpServletResponse response, String path) { String fileSaveRootPath = uploadPrivatePath + path; InputStream in = null; OutputStream out = null; byte[] buffer = new byte[8 * 1024]; try { File file = new File(fileSaveRootPath); in = new FileInputStream(file); out = response.getOutputStream(); // 设置文件MIME类型 response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); for (;;) { int bytes = in.read(buffer); if (bytes == -1) { break; } out.write(buffer, 0, bytes); } } catch (IOException e) { LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage()); } finally { try { in.close(); } catch (IOException e) { LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage()); } try { out.close(); } catch (IOException e) { LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage()); } } return res; } // year private static String subStr(String s) { return "企业" + s.substring(s.lastIndexOf("_") + 1, s.lastIndexOf(".")); } }