springboot集成mysql数据库实现⽂件上传下载引⼊依赖
引⼊swagger依赖 ⽅便接⼝调试
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
相关类
1、⽂件实体类
mysql下载要钱吗@Data
@Table(name = "upload_local_file")
@Entity
public class UploadLocalFile {
@ApiModelProperty
@Id
private Long id;
@ApiModelProperty("⽂件名称")
private String fileName;
@ApiModelProperty("原始⽂件名")
private String originalName;
@ApiModelProperty("扩展名")
private String extensionName;
@ApiModelProperty("访问地址相对地址")
private String url;
}
2、jpa接⼝类
public interface UploadLocalFileRepository extends JpaRepository<UploadLocalFile,Long>{
}
⽂件上传
controller
@ApiOperation(value = "⽂件上传", notes = "⽂件上传")
@PostMapping("/upload")
public ResponseEntity<UploadFile> fileUpload(@RequestParam("file") MultipartFile file) {
try {
UploadFile uploadFile = uploadFileService.fileUpload(file);
return ResponseEntity.ok().body(uploadFile);
} catch (Exception e) {
<(String.StackTrace()));
return ResponseEntity.badRequest().body(null);
}
}
service
@Autowired
UploadLocalFileRepository uploadLocalFileRepository;
@Value("${file.dir}")
private String dirUrl;
@Value("${file.base-url}")
private String baseUrl;
/**
* 上传⽂件
*
* @param file
* @return
*/
public UploadFile fileUpload(MultipartFile file) throws IOException {
InputStream inputStream = InputStream();
String originalFilename = OriginalFilename();
long id = System.currentTimeMillis();
String extensionName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1); String fileName = id + "." + extensionName;
saveToDisk(inputStream, originalFilename);
inputStream.close();
UploadLocalFile uploadLocalFile = new UploadLocalFile();
uploadLocalFile.setId(id);
uploadLocalFile.setExtensionName(extensionName);
uploadLocalFile.setOriginalName(originalFilename);
uploadLocalFile.setFileName(fileName);
uploadLocalFile.setUrl(baseUrl+"/"+id);
UploadLocalFile save = uploadLocalFileRepository.save(uploadLocalFile);
UploadFile uploadFile = new UploadFile();
return uploadFile;
}
/**
* 保存⽂件到本地
*
* @param inputStream
* @param fileName
* @throws IOException
*/
public void saveToDisk(InputStream inputStream, String fileName) throws IOException {
FileOutputStream outputStream = null;
try {
File file = new File(dirUrl);
if (!ists()) {
file.mkdirs();
}
outputStream = new FileOutputStream(dirUrl + "/" + fileName);
byte[] bytes = new byte[1024];
int b;
while ((b = ad(bytes)) != -1) {
outputStream.write(bytes, 0, b);
}
} catch (Exception e) {
<(e.getStackTrace().toString());
} finally {
if (outputStream != null) {
outputStream.close();
}
}
}
file:
dir: d:/
base-url: 127.0.0.1:8009/api/uploadFileController/getFileStreamById dir:上传的⽂件保存到服务器本地地址
base-url:http⽂件下载路径接⼝地址
swagger接⼝测试
数据库存储数据格式
同时本地指定路劲下也保存了⽂件
⽂件下载
controller
@ApiOperation(value = "根据⽂件id下载⽂件",notes = "根据⽂件id下载⽂件")
@GetMapping("/getFileStreamById/{fileId}")
public void getFileStreamById(@PathVariable Long fileId, HttpServletResponse httpServletResponse){
UploadLocalFile byId = uploadLocalFileRepository.findById(fileId).get();
File file = new File(dirUrl+"/"+FileName());
ServletOutputStream fileOutputStream = null;
FileInputStream inputStream = null;
httpServletResponse.setContentType("multipart/form-data");
try {
httpServletResponse.setHeader("Content-Disposition", "attachment;fileName=" + FileName(), "utf-8")); } catch (Exception ex) {
<(ex.getMessage(), ex);
}
try {
inputStream = new FileInputStream(file);
fileOutputStream = OutputStream();
byte[] bytes = new byte[1024];
int b;
while((ad(bytes))!= -1){
fileOutputStream.write(bytes,0,b);
}
fileOutputStream.flush();
}catch (IOException e){
<(e.getStackTrace().toString());
}finally {
try {
if(inputStream != null){
inputStream.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
}catch (IOException e){
<(e.getStackTrace().toString());
}
}
}
⽂件下载响应头的设置
content-type 指⽰响应内容的格式
content-disposition 指⽰如何处理响应内容。
⼀般有两种⽅式:
inline:直接在页⾯显⽰
attchment:以附件形式下载
根据⽂件id 下载⽂件 同理根据⽂件名称也可以匹配下载swagger测试
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论