当前位置: 首页 > news >正文

将数据库文件压缩并上传到文件服务器

1.引入上传工具和压缩包工具

<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId>
</dependency>
<dependency><groupId>com.zlpay</groupId><artifactId>zl-util-fastdfs</artifactId><version>0.0.1-SNAPSHOT</version>
</dependency>

2.组装需要上传的数据并压缩

@Autowired
private FastDFSClient fastDFSClient;
//新增报文
Document document = DocumentHelper.createDocument();
//从数据库组装数据
//报文为XML格式,大小控制在5M以内
//将数据组成一个压缩包并且将数据包写入表中
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
//生成XML文件
ByteArrayOutputStream out = new ByteArrayOutputStream();
//生成名称
String name = addName(i + 1,wrapType,".XML");
srcFile[i] = new File(name);
try {XMLWriter writer = new XMLWriter(out,format);writer.write(document);writer.close();
} catch (IOException e) {throw new AmlException(ErrorCode.AML_DEFAULT_ERROR_CODE, "生成XML文件失败。文件名" + packetEntity.getPacketName());}//利用文件输出流输出到项目根目录下
try(FileOutputStream fos = new FileOutputStream(srcFile[i])) {fos.write(out.toByteArray());System.out.println(srcFile[i].getName());
}catch (IOException e){throw new AmlException(ErrorCode.AML_DEFAULT_ERROR_CODE, "输出文件到根目录失败。文件名" + packetEntity.getPacketName());
}String reportName = suspicionReportEntities.get(i).getReportName();File zipFile = new File("D:\\" + packetEntity.getPacketName());// 调用压缩方法zipFiles(srcFile, zipFile);FileInputStream inputStream = null;MultipartFile file = null;String packetID = null;try {inputStream = new FileInputStream(zipFile);file = new MockMultipartFile(zipFile.getName(),zipFile.getName(),null, IOUtils.toByteArray(inputStream));String extension = FilenameUtils.getExtension(zipFile.getName());packetID = fastDFSClient.uploadFile(file.getBytes(), extension);} catch (IOException e) {e.printStackTrace();}
public void zipFiles(File[] srcFiles, File zipFile) {// 判断压缩后的文件存在不,不存在则创建if (!zipFile.exists()) {try {zipFile.createNewFile();} catch (IOException e) {e.printStackTrace();}}// 创建 FileOutputStream 对象FileOutputStream fileOutputStream = null;// 创建 ZipOutputStreamZipOutputStream zipOutputStream = null;// 创建 FileInputStream 对象FileInputStream fileInputStream = null;try {// 实例化 FileOutputStream 对象fileOutputStream = new FileOutputStream(zipFile);// 实例化 ZipOutputStream 对象zipOutputStream = new ZipOutputStream(fileOutputStream);// 创建 ZipEntry 对象ZipEntry zipEntry = null;// 遍历源文件数组for (int i = 0; i < srcFiles.length; i++) {// 将源文件数组中的当前文件读入 FileInputStream 流中fileInputStream = new FileInputStream(srcFiles[i]);// 实例化 ZipEntry 对象,源文件数组中的当前文件zipEntry = new ZipEntry(srcFiles[i].getName());zipOutputStream.putNextEntry(zipEntry);// 该变量记录每次真正读的字节个数int len;// 定义每次读取的字节数组byte[] buffer = new byte[1024];while ((len = fileInputStream.read(buffer)) > 0) {zipOutputStream.write(buffer, 0, len);}}zipOutputStream.closeEntry();zipOutputStream.close();fileInputStream.close();fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}

关于File和MultipartFile的区别

MultipartFile和File都是Java中处理文件上传的类,但它们有一些区别。
MultipartFile是Spring框架中的一个接口,用于处理文件上传。它可以处理多个文件上传,并且可以获取文件的名称大小、类型等信息。MultipartFile还可以直接保存到磁盘或者数据库中。
File是Java中的一个类,用于处理文件操作。它可以创建、读取、写入、删除文件等操作。但是,File不能直接处理文件上传,需要结合其他类库或框架来实现。
因此,MultipartFile和File的主要区别在于它们的用途和功能。MultipartFile用于处理文件上传,而File用于文件操作。

关于File转MultipartFile的方法

可以使用Spring中的FileCopyUtils类的copy()方法将MultipartFile转换为File类型。

File file = new File("文件路径"); 
FileCopyUtils.copy(multipartFile.getBytes(), file);


可以使用File类中的toPath()方法将File转换为MultipartFile类型
MockMultipartFile是MultipartFile的一个实现类,我们可以把File转成流,通过MockMultipartFile进行转换,
MockMultipartFile参数:
第一个参数:需要处理的文件名字(不包含后缀名)
第二个参数:需要处理的文件名字(包含后缀名)
第三个参数:content-type(需要处理的文件类型)
第四个参数:文件流


例如:

FilelnputStream input = new FilelnputStream(fle);
MultipartFile multipartFile = new MockMultipartFile("fle", fle.getName(),"text/plain", lOUtils.toByteArray(input));
FileInputStream inputStream = null;MultipartFile file = null;String packetID = null;try {inputStream = new FileInputStream(zipFile);file = new MockMultipartFile(zipFile.getName(),zipFile.getName(),null, IOUtils.toByteArray(inputStream));String extension = FilenameUtils.getExtension(zipFile.getName());packetID = fastDFSClient.uploadFile(file.getBytes(), extension);} catch (IOException e) {e.printStackTrace();}


 

http://www.lryc.cn/news/122758.html

相关文章:

  • docker — 容器网络
  • 腾讯面试题:使用Redis分布式锁可能会出现哪些问题?
  • 直接在html中引入Vue.js的cdn来实现Vue3的组合式API
  • YAPi在线接口文档简单案例(结合Vue前端Demo)
  • Java基础篇--Runtime类
  • 数字后端笔试题(1)DCG后congestion问题
  • 数据结构:交换排序
  • SpringBoot复习:(42)WebServerCustomizer的customize方法是在哪里被调用的?
  • 年至年的选择仿elementui的样式
  • 分类过程中的一种遮挡现象
  • 下一代服务架构:单体架构-->分布式架构-->微服务(DDD)-->软件定义架构(SDF with GraphEngine)
  • excel 之 VBA
  • 【数学建模】--聚类模型
  • css3新增选择器总结
  • 0基础学C#笔记10:归并排序法
  • nlohmann json:通过for遍历object和array
  • 适配器模式:将不兼容的接口转换为可兼容的接口
  • 【量化课程】07_量化回测
  • 竞赛项目 深度学习花卉识别 - python 机器视觉 opencv
  • 用对角线去遍历矩阵
  • 【vue】点击按钮弹出卡片,点击卡片中的取消按钮取消弹出的卡片(附代码)
  • 【K8S】pod 基础概念讲解
  • ASP.NET Core中间件记录管道图和内置中间件
  • [系统安全] 五十二.DataCon竞赛 (1)2020年Coremail钓鱼邮件识别及分类详解
  • Android学习之路(3) 布局
  • Python实现GA遗传算法优化XGBoost回归模型(XGBRegressor算法)项目实战
  • C#软件外包开发流程
  • 队列的实现
  • Node + Express 后台开发 —— 起步
  • Python学习笔记第五十七天(Pandas 数据清洗)