String newPath = new ClassPathResource("static/img").getFile().getPath();
ResultEntity<String> headerPictureResultEntity = CrowdUtil.uploadFileToLocaHost(headerPicture.getInputStream(),newPath,headerPicture.getOriginalFilename());
//上传文件的方法
inputstream 上传的文件流
path 保存的路径
originalFilename 上传文件的名字
public static ResultEntity<String> uploadFileToLocaHost(InputStream inputStream,String path,String originalFilename) {
String ext = originalFilename.substring( originalFilename.lastIndexOf(".") );
String fileName = System.currentTimeMillis()+ext;
String filePath =path+"/"+fileName;
try {
copyfile(inputStream,filePath);
} catch (IOException e) {
e.printStackTrace();
return ResultEntity.failed(e.getMessage());
}
return ResultEntity.successWithData(filePath);
}
// 拷贝文件的方法
private static void copyfile(InputStream fis, String filePath) throws IOException {
FileOutputStream fos = new FileOutputStream(filePath);
int b = 0;
byte[] bye = new byte[1024];
while ((b = fis.read(bye)) != -1) {
fos.write(bye, 0, b);
}
}