1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.example.fuzzControll.tools;
import com.example.fuzzControll.conf.SpringContextUtil;
import com.example.fuzzControll.conf.SeedProperties;
import com.example.fuzzControll.exception.FileException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
@Slf4j
public class FileTools {
SeedProperties properties = (SeedProperties) SpringContextUtil.getBean("seedProperties");
public void load(MultipartFile file) throws FileException {
if (file == null) {
throw new FileException("upload file is null !");
}
try (InputStream inputStream = file.getInputStream();
FileOutputStream outputStream = new FileOutputStream(properties.getSeedPath() + "/" + file.getOriginalFilename());) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
throw new FileException("write file error !");
}
}
}