日常代码记录一下,方便以后使用,不多说直接上的代码~~~~~~~~
/*** *七牛云参数 */ //域名 public static final String url = ""; //ask private static final String accessKey = "; //sek private static final String accessSecretKey = ""; //空间名 private static final String bucket = "";
/***
* base64位图片上传
* @param base64Image
* @return
*/
@RequestMapping(value="/uploadImg", method= RequestMethod.POST)
public Object uploadImg(@RequestParam("base64Image") String base64Image) {
if (StringUtils.isEmpty(base64Image)) {
return R.error("文件数据为空,请重新上传");
}
//文件路径
String imageName = "";
try {
//根据日期生成路径 2022/1/15/
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
String datePath = sdf.format(new Date());
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
imageName = new StringBuilder().append(datePath).append(uuid).toString();
//文件根目录
String domain = "mindMap";
imageName = domain + "/" + imageName + ".png";
// 解密
java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
// 去掉base64前缀 data:image/jpeg;base64,
base64Image = base64Image.substring(base64Image.indexOf(",", 1) + base64Image.length());
byte[] b = decoder.decode(base64Image);
//保存到七牛云
boolean stream = copyWritingServiceImpl.uploadToQiniu(imageName, b);
if (stream){
// 返回图片的路径
return R.success(url+imageName);
}
/*// 处理数据 本地保存使用
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
// 保存图片
OutputStream out = new FileOutputStream(imageName);
out.write(b);
out.flush();
out.close();*/
} catch (Exception e) {
throw new BusinessException(e.getMessage());
}
return null;
}
/***
* 图片对象上传
* @param file
* @return
*/
@PostMapping("/image")
public R<String> upload(@RequestParam("file") MultipartFile file){
// 判断文件是否为空
if (file.isEmpty()) {
return R.error("上传失败");
}
//根据日期生成路径 2022/1/15/
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
String datePath = sdf.format(new Date());
//uuid作为文件名
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
//后缀和文件后缀一致
String originalFilename = file.getOriginalFilename();
int index = originalFilename.lastIndexOf(".");
// test.jpg -> .jpg
String fileType = originalFilename.substring(index);
String imageName = new StringBuilder().append(datePath).append(uuid).append(fileType).toString();
//文件根目录
String domain = "mindMap";
imageName = domain+"/"+imageName;
//保存到七牛云
boolean upload = copyWritingServiceImpl.upload(file, imageName);
if(upload){
//返回数据
return R.success(url + imageName);
}else {
return R.success("图片上传失败!!!");
}
}
public boolean upload(MultipartFile file, String fileName){
Configuration cfg = new Configuration(Region.huanan()); //改成你设置的区域,我是华南huanan
UploadManager uploadManager = new UploadManager(cfg);
//默认不指定key的情况下,以文件内容的hash值作为文件名
try {
byte[] uploadBytes = file.getBytes();
Auth auth = Auth.create(accessKey, accessSecretKey);
String upToken = auth.uploadToken(bucket);
Response response = uploadManager.put(uploadBytes, fileName, upToken);
//解析上传成功的结果
DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
return true;
} catch (Exception ex) {
throw new BusinessException(ex.getMessage());
}
}
public boolean uploadToQiniu(String fileName, byte[] imageData) {
// 构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Region.huanan());
UploadManager uploadManager = new UploadManager(cfg);
Auth auth = Auth.create(accessKey, accessSecretKey);
String upToken = auth.uploadToken(bucket);
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageData);
Response response = uploadManager.put(inputStream, fileName, upToken, null, null);
// 解析上传成功的结果
DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
return true;
} catch (Exception ex) {
throw new BusinessException(ex.getMessage());
}
}
就这样,下次再见了.............