描述:
???文件上傳:通過element-ui的<el-upload>完成文件的上傳到服務(wù)端稍刀,并將文件的二進(jìn)制數(shù)據(jù)存儲(chǔ)到mysql柠新。
???文件下載:查詢mysql將文件二進(jìn)制數(shù)據(jù)以流形式輸出者春。
- 上傳
進(jìn)入element-ui官方腋舌,找到Upload 上傳組件
渣聚,找第一個(gè)案例帆离,點(diǎn)擊在線運(yùn)行蔬蕊,如下:
修改代碼,添加自己預(yù)先定義好的上傳接口哥谷,如下:
springboot后臺(tái)代碼岸夯,如下:
@RestController
@RequestMapping("/file")
@CrossOrigin //必須要加跨域請(qǐng)求注解
public class FileController {
@Autowired
private JdbcTemplate jdbc; //spring-jdbc
@RequestMapping("/upload")
public String upload(MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
long size = file.getSize();
String type = fileName.substring(fileName.lastIndexOf(".") + 1);
//讀取文件二進(jìn)制存儲(chǔ)到數(shù)據(jù)庫,mysql字段類型longblob
byte[] buffer = file.getBytes();
final Map map = new HashMap<String, Integer>();
String uuid = UUID.randomUUID().toString().replace("-","");
String sql = "insert into files(id,name,size,type,data) values(?,?,?,?,?)";
final LobHandler lobHandler = new DefaultLobHandler();
jdbc.execute(sql, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
@Override
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException, DataAccessException {
ps.setString(1,uuid);
ps.setString(2,fileName);
ps.setDouble(3,size);
ps.setString(4,type);
lobCreator.setBlobAsBytes(ps,5,buffer);
}
});
//常規(guī)的存儲(chǔ)到本項(xiàng)目files文件夾中
// OutputStreamWriter osw =
// new OutputStreamWriter(new FileOutputStream("./files/"+fileName));
// InputStreamReader isr =
// new InputStreamReader(file.getInputStream());
// char[] bytes = new char[12];
// while (isr.read(bytes) != -1){
// osw.write(bytes);
// }
// try{
// osw.close();
// isr.close();
// }catch (IOException e){
// e.printStackTrace();
// }
return null;
}
}
mysql表結(jié)構(gòu)如下:
- 下載
springboot后臺(tái)代碼如下:
@RestController
@RequestMapping("/file")
@CrossOrigin
public class FileController {
@Autowired
private JdbcTemplate jdbc;
@RequestMapping("/down")
public String downLoad(HttpServletResponse response) throws Exception {
//第一種常規(guī)的從本地讀取文件輸出到response
// String filePath = "D:/圖片資料/綦江人民政府.png";
// File file = new File(filePath);
// if (file.exists()){
// response.setContentType("application/vnd.ms-excel;charset=UTF-8");
// response.setCharacterEncoding("UTF-8");
// response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode("綦江人民政府.png","UTF-8"));
// byte[] buffer = new byte[1024];
//
// FileInputStream fis = null;
// BufferedInputStream bis = null;
// OutputStream os = null;
// try{
// os = response.getOutputStream();
// fis = new FileInputStream(file);
// bis = new BufferedInputStream(fis);
// int i = bis.read(buffer);
// while (i != -1){
// os.write(buffer);
// i = bis.read(buffer);
// }
// }catch (Exception e){
// e.printStackTrace();
// }
// try {
// bis.close();
// fis.close();
// }catch (IOException e){
// e.printStackTrace();
// }
// }
// 第二種讀取數(shù)據(jù)庫二進(jìn)制數(shù)據(jù)輸出
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
OutputStream os = response.getOutputStream();
response.setCharacterEncoding("UTF-8");
final LobHandler lobHandler = new DefaultLobHandler();
String sql ="select name,data from files where id = '21d2caa89e1e4e2f8d3e422741f0d29d'";
jdbc.query(sql, new AbstractLobStreamingResultSetExtractor() {
@Override
protected void streamData(ResultSet resultSet) throws SQLException, IOException, DataAccessException {
String fileName = lobHandler.getClobAsString(resultSet,1);
response.setHeader("Content-Disposition", "attachment;fileName=" + java.net.URLEncoder.encode(fileName,"UTF-8"));
InputStream is = lobHandler.getBlobAsBinaryStream(resultSet, 2);
if (is != null) FileCopyUtils.copy(is,os);
}
});
try {
os.close();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
參考:
SpringBoot實(shí)現(xiàn)文件的上傳和下載
SpringBoot+element-ui進(jìn)行文件上傳
使用jdbcTemplate 保存圖片 至數(shù)據(jù)庫 以及 從數(shù)據(jù)庫讀取 保存到本地