文件上传大家都是知道怎么写的,前几天用到了上传zip文件的方法,在这里记录一下,希望可以帮助到大家!
/****
* 上传解析zip
* @param file
*/
@RequestMapping("uploadZip")
public void uploadZip(MultipartFile file) {
String zipFileName;
String teachId;
String fileName;
try {
ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("GBK"));
BufferedInputStream bs = new BufferedInputStream(zipInputStream);
ZipEntry zipEntry;
byte[] bytes = null;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
zipFileName = zipEntry.getName();
// 只读取docx的文件
String docx = zipFileName.substring(zipFileName.length() - 4, zipFileName.length());
if (zipEntry.getSize() > 0 && docx != null && "docx".equals(docx)) {
bytes = new byte[(int) zipEntry.getSize()];
bs.read(bytes, 0, (int) zipEntry.getSize());
MultipartFile multipartFile = new MockMultipartFile(fileName, new ByteArrayInputStream(bytes));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
很简单,没有很多代码,拿到MultipartFile 就可以做我们的业务逻辑了
pom文件引入
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
好了,到这里,上传zip文件的代码就没有了,希望可以帮助到大家!如果有用或者错误的话,希望大家多多评论,关注。