视频前缀
data:video/mp4;base64,
图片前缀
data:image/jpeg;base64,
图片
<img src="">
H5视频
<video controls muted webkit-playsinline="true" playsinline="true" poster="" src="">
后台
@Controller
@RequestMapping("/imgInfo")
public class ImgInfoController extends BaseController {
@Resource
private ImgInfoService imgInfoService;
@RequestMapping("/add")
public void add(@RequestParam("img") MultipartFile file, @RequestParam("id") Double id) throws IOException {
if (id == null || file == null) {
return;
}
Imginfo imginfo = new Imginfo();
imginfo.setId(id);
InputStream in = file.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int rc;
while ((rc = in.read(buff)) != -1) {
out.write(buff, 0, rc);
}
byte[] img = out.toByteArray();
imginfo.setImg(img);
imgInfoService.add(imginfo);
logger.info("=====================end");
}
@ResponseBody
@RequestMapping("/getById")
public void getById(Double id) throws IOException {
if (id == null) {
return;
}
Imginfo imginfo = imgInfoService.getById(id);
if (imginfo == null){
return;
}
// DataOutputStream out =
// new DataOutputStream(new FileOutputStream("C:\\Users\\KRIC\\Desktop\\"+id+".jpg"));
//
////得到图片二进制流,给输入流
// InputStream is = new ByteArrayInputStream(imginfo.getImg());
// byte[] buff = new byte[1024];
//
// int len = 0;
// while((len=is.read(buff))!=-1){
// out.write(buff, 0, len);
// }
////将流和buffer关闭
// is.close();
// out.flush();
// out.close();
// BASE64Encoder encoder = new BASE64Encoder();
// String imgStr = encoder.encode(imginfo.getImg());
// JSONObject json =new JSONObject();
// String imgStr = Base64.getEncoder().encodeToString(imginfo.getImg());
// String type = "data:image/jpeg;base64,";
// imginfo.setBase64(type+imgStr);
// json.put("aa",imginfo);
String rtnStr = JSON.toJSONString(imginfo);
toClient(rtnStr);
}
}
public class Imginfo {
private Double id;
private byte[] img;
public Double getId() {
return id;
}
public void setId(Double id) {
this.id = id;
}
public byte[] getImg() {
return img;
}
public void setImg(byte[] img) {
this.img = img;
}
}
网友评论