<!-- JSON依赖包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* @version 1.0
* @name
* @date 2020/11/6 16:12
* @function 图片编解码工具类
*/
public class EncodeImgUtil
{
/**
* @author
* 将图片内容处理成Base64编码格式
* @version 1.0
*/
public static String encoderImg(MultipartFile file ){
String imgString = null;
byte[] imgBytes = null;
try{
//将图片转化为字节数组
imgBytes = file.getBytes();
}catch (Exception e){
e.printStackTrace();
}
//将图片字节数组进行Base64编码
BASE64Encoder encoder = new BASE64Encoder();
imgString = encoder.encode(imgBytes);
return imgString;
}
/**
* @author 将图片内容处理成Base64解码格式
* 将图片解码 imgString 图片Base64编码格式
* @version 1.0
*/
public static byte[] decoderImg(String imgString)throws IOException
{
byte[] imgBytes = null;
BASE64Decoder base64Decoder = new BASE64Decoder();
imgBytes = base64Decoder.decodeBuffer(imgString);
return imgBytes;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片上传</title>
<script src="../js/jquery.js"></script>
<script>
$(function () {
$("#btn").click(function () {
var formData = new FormData();
var file = $("#file")[0].files[0];
formData.append("file",file);
$.ajax({
url:"/user/upload",
type:"post",
data:formData,
processData: false, //使数据不做处理
contentType:false, //不要设置请求头
success:function (result) {
if(result == "OK"){
alert("上传成功")
}else{
alert("上传失败")
}
}
})
})
$("#btn2").click(function () {
$.ajax({
url:"/user/download",
type:"post",
dataType:"json",
data: {"name":$("#name").val()},
success:function (data) {
// alert(data);
if(data =="fail"){
alert("下载图片失败")
}else{
//将图片显示在img中
$("#ImgePic").attr("src","data:image/png;base64,"+data)
}
}
})
})
})
</script>
</head>
<body>
<!--上传图片-->
<input type="file" name="file" id="file"/><br/>
<input type="button" id="btn" value="上传图片"/><br/>
<!--下载图片-->
<input type="text" id="name" name="name"/><br/>
<input type="button" id="btn2" value="下载图片"/><br/>
<img id="ImgePic" src="ImgePic" width="800" height="600"/><br/>
</body>
</html>
4:controller
import com.alibaba.fastjson.JSON;
import com.dtone.ssm.redis.RedisUtil;
import com.dtone.ssm.util.EncodeImgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;
/**
* @version 1.0
* @name
* @date 2020/11/6 16:38
* @function 图片上传/下载页面请求控制器类
*/
@RestController
@Slf4j // Lombok 插件创建 Logger 日志对象
public class ImgController
{
@Autowired
private RedisUtil redisUtil;
@RequestMapping("user/upload")
public String upload(MultipartFile file){
log.info("收到upload请求");
//获取文件名
String fileName = file.getOriginalFilename();
//随机生成唯一ID
// String uuid = UUID.randomUUID().toString();
//将文件转化为Base64编码格式
String imgString = EncodeImgUtil.encoderImg(file);
//将文件存储到redis
if(redisUtil.exists(fileName)){
return "fail";
}
redisUtil.set(fileName,imgString);
return "OK";
}
@RequestMapping("user/download")
public String download(String name) throws IOException {
log.info("收到user/dpwnload请求"+name);
if(name !=null){
//从redis获取Base64为编码格式
String imgString = (String) redisUtil.get(name);
//将Base64编码格式图片转化为字节数组
byte[] imgBytes = EncodeImgUtil.decoderImg(imgString);
//将图片字节数组转化为JSON格式发送给客户端
return JSON.toJSONString(imgBytes);
}
return "fail";
}
}
原文:https://www.cnblogs.com/liangqianlz/p/13974317.html