将数据库文件压缩并上传到文件服务器
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();}