mysql文件上传下载 vue下载
南江烂柯人 JAVA劝退师

JAVA文件以流的形式存在Mysql(blob)数据库,mysql最大支持4G文件(longblb)

直接将流转换为byte存入数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

public Response uploadBlob(MultipartFile file) {
//数据库实体类 改成自己的
FileUpload fileUpload = new FileUpload();
fileUpload.setFileName(file.getOriginalFilename());
try {
fileUpload.setContent(FileTypeUtils.input2byte(file.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
save(fileUpload);
return Response.ok("成功", fileUpload.getId());
}


/**
* 将 流 转换为byte
*
* @param inStream
* @return
* @throws IOException
*/
public static byte[] input2byte(InputStream inStream) throws IOException {
try (ByteArrayOutputStream swapStream = new ByteArrayOutputStream()) {
byte[] buff = new byte[inStream.available()];
while (inStream.read(buff) != -1) {
swapStream.write(buff);
}
return swapStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

下载文件输出文件流到前端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

public void getFile(String id, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
// 获取数据库文件信息
FileUpload fileUpload = getById(id);
if (fileUpload.getContent() != null) {
//获取存储的文件名称
String filename = fileUpload.getFileName();
//获取浏览器版本
String userAgent = request.getHeader("USER-AGENT");
//IE浏览器
if (StringUtils.contains(userAgent, "MSIE")) {
filename = URLEncoder.encode(filename, "UTF8");
}
//google,火狐浏览器
else if (StringUtils.contains(userAgent, "Mozilla")) {
filename = new String(filename.getBytes(), "ISO8859-1");
}
//其他浏览器
else {
filename = URLEncoder.encode(filename, "UTF8");
}
try {
byte[] fileStream = fileUpload.getContent();
response.setHeader("content-type", "application/octet-stream");
//这边可以设置文件下载时的名字,我这边用的是文件原本的名字,可以根据实际场景设置
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(fileStream);
toClient.flush();
toClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

流的方式前端要指定 responseType: ‘blob’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
methods: {
downLoad() {
if (this.fileId) {
request({
url: "localhost:8080/file/getFile",
method: "get",
params: { id: this.fileId },
//指定 responseType
responseType: 'blob'
}).then((resp) => {
let blob = new Blob([resp],{
type:'application/vnd.ms-excel'
});
let objectUrl = URL.createObjectURL(blob); //生成一个url
this.downloadFile(objectUrl,this.form.fileName)
});
}
}
}

转成文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public File getFile(String id, HttpServletRequest request, HttpServletResponse response) {
// 获取数据库文件信息
FileUpload fileUpload = getById(id);
if (fileUpload.getContent() != null) {
File file = null;
try {
file = new File(fileUpload.getFileName());
if (!file.exists()) {
file.createNewFile();
}
OutputStream os = new FileOutputStream(file);
os.write(fileUpload.getContent());
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
}
  • 本文标题:mysql文件上传下载 vue下载
  • 本文作者:南江烂柯人
  • 创建时间:2021-09-06 11:20:12
  • 本文链接:https://www.mattjia.com/2021/09/06/java/文件存储在mysql数据库,vue下载/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论