一、保存在springboot项目的static目录下
示例代码:
@PostMapping
@ApiOperation(value = "文件上传")
public Map uploadFile(MultipartFile file) throws IOException {
Map map=new HashMap();
map.put("url",BudgetService.updateFilePath(file));
return map;
}
updateFilePath方法,写入并返回文件地址
@Override
public String updateFilePath(MultipartFile file) throws IOException {
// 获取文件名
String fileOrigName = file.getOriginalFilename();
// 如果同一张图片怕覆盖,也可以加个时间戳
//String fileOrigName = System.currentTimeMillis() + file.getOriginalFilename();
String filepath = "/img/avatars/" + fileOrigName;//文件存入当前项目的resources/satic/目录下
try {
File targetFile = new File(filepath);
if (targetFile.exists()) {
return filepath;
}
//第一次上传时,如果目录不存在,创建目录
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
//将文件输出到本地
File files = new File(filepath);
try {
if (!files.exists()) { //如果文件不存在则新建文件
files.createNewFile();
}
FileOutputStream output = new FileOutputStream(files);
byte[] bytes = file.getBytes();
output.write(bytes); //将数组的信息写入文件中
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// file.transferTo(targetFile);
return pathname;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
二、上传到阿里云oss的云空间
引入阿里云相关依赖
<!-- 阿里云oss依赖 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.5.0</version>
</dependency>
示例代码:
@RequestMapping(value = "/subFile.do", produces = "text/plain;charset=utf-8")
@ResponseBody
public String subFile(@RequestParam("project") MultipartFile file) {
try {
if (file.isEmpty()){
return "文件为空";
}
//上传文件到阿里云
String path = utilService.uploadFile(file);
return path;
} catch (Exception e) {
e.printStackTrace();
}
return "上传失败";
}
uploadFile方法:
public String uploadFile(MultipartFile file) {
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret); //查看自己的阿里云的账号获取
// 上传文件流。
InputStream inputStream = paperfile.getInputStream();
//获取文件名称
String fileOriginalFilename = paperfile.getOriginalFilename();
String datePach = new DateTime().toString("yyyy/MM/dd");
fileOriginalFilename = datePach + "/" + fileOriginalFilename;
//调用oss进行上传
//第一个参数BucketName,第二个参数上传到oss文件的路径和名称,第三个文件输入流
ossClient.putObject(bucketName, fileOriginalFilename, inputStream);
// 关闭OSSClient。
String url = "http://" + bucketName + "." + endPoint + "/" + fileOriginalFilename;
ossClient.shutdown();
return url;
} catch (Exception e) {
e.printStackTrace();
return null;
}
三、NIO方式上传文件
@Override
public String upload(MultipartFile file) throws IOException {
String filePath = null;
String fileName = file.getOriginalFilename();
// 获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
//检查文件格式是否合法
if(file.getContentType().toLowerCase().contains("image/") && !StringUtils.isBlank(suffixName)
&& (suffixName.equalsIgnoreCase("BMP")
|| suffixName.equalsIgnoreCase("JPG") || suffixName.equalsIgnoreCase("JPEG")
|| suffixName.equalsIgnoreCase("PNG"))) {
// 设置文件存储路径
filePath = "C:\\Users\\Administrator.SC-202001070920\\Desktop\\Img\\" + System.currentTimeMillis() + "." + suffixName;
//上传到云服务器
// filePath = "www.xxxxx.com/img/" + System.currentTimeMillis() + "." + suffixName;
byte[] bytes = file.getBytes();
Path path = Paths.get(filePath);
//保存在本地
Files.write(path, bytes);
return filePath;
} else {
return "图片格式错误";
}
}