private String manageFilePath; private String manageFileName; private String manageUserName; private String managePassword; public void downFile(String date, HttpServletResponse response) { FileResponse fileResponse = new FileResponse(); SmbFileInputStream in = null; try { manageFilePath = URLDecoder.decode(manageFilePath, String.valueOf(StandardCharsets.UTF_8)); //获取前一天 String beforeDay = DateRiskUtil.getBeforeDay(date, 1); if (!manageFilePath.contains("smb:")) { manageFilePath = "smb:" + manageFilePath + "\\"; } String filePath = manageFilePath.replace("\\", "/").replace("yyyymmdd", beforeDay); String fileName = manageFileName.replace("yyyymmdd", beforeDay); String newFilePath = filePath + fileName; NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("", manageUserName, managePassword); SmbFile smbFile = new SmbFile(newFilePath, auth); if (!smbFile.exists()) { throw new Exception("没有找到对应的文件"); } long maxFileSize = parseSize("5M"); long fileSize = smbFile.length(); if (fileSize > maxFileSize) { throw new Exception("文件大小超过最大的限制"); } String smbFileName = smbFile.getName(); if (smbFileName.contains(beforeDay)) { in = new SmbFileInputStream(smbFile); byte[] fileContent = new byte[(int) fileSize]; int bytesRead = in.read(fileContent); if (bytesRead != fileSize) { throw new Exception("读取文件内容到字节数组的大小和原文件内容大小不一致"); } fileResponse.setStatus("1"); fileResponse.setFileContent(fileContent); fileResponse.setFailureReason("成功"); } } catch (Exception e) { e.printStackTrace(); } } private long parseSize(String sizeStr) { String newSizeStr = sizeStr.toUpperCase().replace("[^0-9.]", ""); double sizeValue = Double.parseDouble(newSizeStr); char lastChar = sizeStr.charAt(sizeStr.length() - 1); switch (lastChar) { case 'M': case 'B': return Math.round(sizeValue * 1024 * 1024); case 'K': return Math.round(sizeValue * 1024); case 'G': return Math.round(sizeValue * 1024 * 1024 * 1024); default: return Math.round(sizeValue); } }
public class FileResponse {
private String status;
private String failureReason;
private byte[] fileContent;
}