场景:
1.做一个在线pdf验真,基本流程就是在上传的时候限制文件上传大小,然后上传成功以后拿到fileKey之后能够拿到这个文件的下载地址。根据下载地址拿到这个文件,通过在线地址解析成base64。
2.验证pdf是否加密?如果加密输入密码
<repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <!--pdf验证加密--> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>3.4.2</version> </dependency>
@PostMapping(value = "/checkPdfIsEncryption") @ApiOperation(value = "检验pdf是否加密", notes = "检验pdf是否加密") public BaseResponse checkFileType(@RequestParam("file") MultipartFile multipartFile) { // 创建PdfDocument实例 PdfDocument doc = new PdfDocument(); // 加载PDF文件 try { doc.loadFromBytes(multipartFile.getBytes()); } catch (Exception e) { throw new SuperRuntimeException(ErrorCodeEnum.FILE_ENCRYPTION); } return BaseResponse.ok(); }
public class FileUtils { /** * @param :multipartFile:上传的文件 * @param size: 限制大小 * @param unit:限制单位(B,K,M,G) * @return boolean:是否大于 * @author * @dateTime 2020-10-21 14:42:17 * <p> * 判断文件大小 */ public static boolean checkFileSize(MultipartFile multipartFile, int size, String unit) { long len = multipartFile.getSize(); // 上传文件的大小, 单位为字节. // 准备接收换算后文件大小的容器 double fileSize = 0; if ("B".equals(unit.toUpperCase())) { fileSize = (double) len; } else if ("K".equals(unit.toUpperCase())) { fileSize = (double) len / 1024; } else if ("M".equals(unit.toUpperCase())) { fileSize = (double) len / 1048576; } else if ("G".equals(unit.toUpperCase())) { fileSize = (double) len / 1073741824; } // 如果上传文件大于限定的容量 if (fileSize > size) { return false; } return true; } /** * @param url 文件在线下载地址 * @return * @author * @dateTime 2020-10-21 * <p> * 将在线连接转换为Base64编码方法 */ public static String downloadImg(String url) { try { //System.out.print("开始下载" + url + "资源\n"); URL uploadUrl = new URL(url); HttpURLConnection httpUrl = (HttpURLConnection) uploadUrl.openConnection(); httpUrl.connect(); httpUrl.getInputStream(); InputStream is = httpUrl.getInputStream(); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // 创建一个Buffer字符串 byte[] buffer = new byte[1024]; // 每次读取的字符串长度,如果为-1,代表全部读取完毕 int len = 0; while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); } // 对字节数组Base64编码 BASE64Encoder decoder = new BASE64Encoder(); return decoder.encode(outStream.toByteArray()); } catch (Exception ex) { //System.out.print("文件下载出错:" + ex.getMessage() + "\n"); throw new SystemException(ErrorCodeEnum.GET_FILE_DOWNLOAD_URL_FAIL); } } /** * @param base64Str base64字符串 * @param fileName 文件名称 * @param parentPath 图片存放路径 * @return * @author * @dateTime 2020-10-21 * <p> * base64字符串转换成图片 */ public static File base64StrToFile(String base64Str, String fileName, String parentPath) { File file = new File(parentPath, fileName); FileOutputStream out = null; try { //byte[] bytes = Base64Utils.decodeFromString(base64Str); byte[] bytes1 = new BASE64Decoder().decodeBuffer(base64Str); ByteArrayInputStream in = new ByteArrayInputStream(bytes1); byte[] buffer = new byte[1024]; out = new FileOutputStream(file); int byteSum = 0; int byteRead = 0; while ((byteRead = in.read(buffer)) != -1) { byteSum += byteRead; out.write(buffer, 0, byteRead); } } catch (Exception ex) { throw new RuntimeException("transform base64 String into file 出错", ex); } finally { try { if (null != out) { out.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return file; }
/**
* 根据在线url,获取文件名称
*
* @param urlStr
* @return
*/
public String getFileName(String urlStr) {
String fileName = null;
try {
URL url = new URL(urlStr);
URLConnection uc = url.openConnection();
fileName = uc.getHeaderField("Content-Disposition");
fileName = new String(fileName.getBytes("ISO-8859-1"), "GBK");
fileName = URLDecoder.decode(fileName.substring(fileName.indexOf("filename=") + 10), "UTF-8");
//log.info("文件名为:" + fileName + " 大小" + (uc.getContentLength()/1024)+"KB")
} catch (Exception e) {
e.printStackTrace();
}
return fileName;
}
/**
* 截取字符串
*
* @param str 待截取的字符串
* @param start 截取起始位置 ( 1 表示第一位 -1表示倒数第1位)
* @param end 截取结束位置 (如上index)
* @return
*/
public static String sub(String str, int start, int end) {
String result = null;
if (str == null || str.equals("")) {
return "";
}
int len = str.length();
start = start < 0 ? len + start : start - 1;
end = end < 0 ? len + end + 1 : end;
return str.substring(start, end);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118758.html