文件本地存储
文件上传代码
@PostMapping("/upload")
public Result upload( MultipartFile file) throws Exception {
//将文件存储到本地磁盘中
//获取文件扩展名
String originalFilename = file.getOriginalFilename();
int index = originalFilename.lastIndexOf('.');
String extname = originalFilename.substring(index);
String newFileName = UUID.randomUUID().toString() + extname;
log.info("新的文件名:{}",newFileName);
//存储地址
file.transferTo(new File("I:\\file\\" + newFileName));
return Result.success();
}
文件回显代码
@GetMapping("/download")
public void download(String name, HttpServletResponse response){
try {
//输入流,通过输入流读取文件内容
FileInputStream fileInputStream = new FileInputStream(new File("I:\\file\\" + name));
//输出流,通过输出流将文件写回浏览器
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fileInputStream.read(bytes)) != -1){
outputStream.write(bytes,0,len);
outputStream.flush();
}
//关闭资源
outputStream.close();
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
图片回显的前端使用
此处为vue2 且引入了element-ui组件
<template>
<div>
<el-image :src="getImage(orderInfo.bookPhoto)"></el-image>
</div>
</template>
<script>
export default {
data() {
return {
};
},
methods: {
getImage(url) {
return `/api/download?name=${url}`;
}
}
};
</script>
<style>
</style>
使用阿里云OSS存储
在pom文件中引入依赖
<!--阿里云OSS服务依赖-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
配置文件
aliyun:
oss:
endpoint: https://oss-cn-beijing.aliyuncs.com
access-key-id: # 填写自己阿里云OSS的信息
access-key-secret: # 填写自己阿里云OSS的信息
bucket-name: # 填写自己创建的桶名
创建工具类
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.UUID;
@Component
public class AliOSSUtils {
@Value("${aliyun.oss.endpoint}")
private String endpoint ;
@Value("${aliyun.oss.accessKeyId}")
private String accessKeyId ;
@Value("${aliyun.oss.accessKeySecret}")
private String accessKeySecret ;
@Value("${aliyun.oss.bucketName}")
private String bucketName ;
/**
* 实现上传图片到OSS
*/
public String upload(MultipartFile file) throws IOException {
// 获取上传的文件的输入流
InputStream inputStream = file.getInputStream();
// 避免文件覆盖
String originalFilename = file.getOriginalFilename();
String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
//上传文件到 OSS
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
ossClient.putObject(bucketName, fileName, inputStream);
//文件访问路径
String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
// 关闭ossClient
ossClient.shutdown();
return url;// 把上传到oss的路径返回
}
}
编写上传方法
@Autowired
private AliOSSUtils aliOSSUtils;
@ApiOperation(value = "上传文件")
@PostMapping({"/admin/upload","/user/upload"})
public Result upload(MultipartFile file) throws Exception {
//进行文件上传
String url = aliOSSUtils.upload(file);
return Result.success(url);
}