// 必須 @Aoutowired 使用new創建實體無法掃描到配置文件導致為null
@Value("${outpath}")
private String outpath;
@Value("${pdfpath}")
private String pdfpath;
public void upload(MultipartFile filecontent) throws Exception {
OutputStream os = null;
InputStream inputStream = null;
String fileName = null;
try {
inputStream = filecontent.getInputStream();
fileName = filecontent.getOriginalFilename();
} catch (IOException e) {
e.printStackTrace();
}
try {
// 2、保存到臨時文件
// 1K的數據緩沖
byte[] bs = new byte[1024];
// 讀取到的數據長度
int len;
// 輸出的文件流保存到本地文件
File tempFile = new File(pdfpath);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
// 開始讀取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 完畢,關閉所有鏈接
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}