| | |
| | | if (fileNameNonSuffix == null) |
| | | return null; |
| | | // 获取文件加密名称 1 保证文件唯一 不存在覆盖问题 2 目录下保存文件加密名称 去除文件关键信息 |
| | | String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()); |
| | | String encodeFileName = SHA256Util.getSHA256Str(fileNameNonSuffix + System.currentTimeMillis()) + "." + suffix; |
| | | Long fileSize = file.getSize(); |
| | | boolean b = uploadFile(file, absolutePath, encodeFileName); |
| | | if (!b) |
| | |
| | | FileUploadResult dto = new FileUploadResult(); |
| | | dto.setFileName(fileNameNonSuffix); |
| | | dto.setFileEncodeName(encodeFileName); |
| | | dto.setFilePath(relativePath); |
| | | dto.setFilePath(relativePath + encodeFileName); |
| | | dto.setFileSize(fileSize); |
| | | dto.setFileSuffix(suffix); |
| | | return dto; |
| | |
| | | public static void deleteFile(String filePath, String fileName) { |
| | | String absolutePath = fileUploadFolder + "/" + filePath + "/"; |
| | | File targetFile = new File(absolutePath, fileName); |
| | | if (null != targetFile && targetFile.isFile()) { |
| | | targetFile.delete(); |
| | | } |
| | | targetFile.deleteOnExit(); |
| | | } |
| | | |
| | | public static void downLoadFile(HttpServletResponse response, String fileName, String filePath, String toFileName) { |
| | |
| | | } |
| | | } |
| | | |
| | | public static void downLoadFile(HttpServletResponse response, String filePath, String toFileName) { |
| | | String absolutePath = fileUploadFolder + filePath; |
| | | File file = new File(absolutePath); |
| | | if (file.exists()) { |
| | | byte[] buffer = new byte[1024]; |
| | | FileInputStream fis = null; |
| | | BufferedInputStream bis = null; |
| | | try { |
| | | response.setHeader("Content-Type", "application/octet-stream;charset=utf-8"); // 告诉浏览器输出内容为流 |
| | | // response.setHeader("Content-Disposition", "attachment;fileName="+ new |
| | | // String(toFileName.getBytes("UTF-8"),"ISO-8859-1")); |
| | | response.setHeader("Content-Disposition", |
| | | "attachment;fileName=" + URLEncoder.encode(toFileName, "UTF-8")); |
| | | fis = new FileInputStream(file); |
| | | bis = new BufferedInputStream(fis); |
| | | OutputStream os = response.getOutputStream(); |
| | | int i = bis.read(buffer); |
| | | while (i != -1) { |
| | | os.write(buffer, 0, i); |
| | | i = bis.read(buffer); |
| | | } |
| | | os.flush(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (bis != null) { |
| | | try { |
| | | bis.close(); |
| | | } catch (IOException e) { |
| | | } |
| | | } |
| | | if (fis != null) { |
| | | try { |
| | | fis.close(); |
| | | } catch (IOException e) { |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | ExceptionCast.cast("文件不存在"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 上传文件工具类 |
| | | * |