代码如下
public static String uploadAndReturnPath(MultipartFile file , String path , String fileName){
String filePath = "";
try {
File pathRoot = new File(ResourceUtils.getURL("classpath:").getPath());
System.out.println("保存的路径pathRoot:"+pathRoot.getAbsolutePath());
if(!pathRoot.exists()) {
pathRoot = new File("");
}
String saveFile = pathRoot.getAbsolutePath().replace("%20"," ").replace('/', '\\')+"\\static"+path;
File f = new File(saveFile);
if(!f.exists()) {
f.mkdirs();
}
FileOutputStream writer = new FileOutputStream(new File(f , fileName));
writer.write(file.getBytes());
writer.flush();
writer.close();
filePath = saveFile + "\\" + fileName;
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}