java api 接收保存图片到本地
2021-01-04 08:29
标签:工具 sts bre encoder set jpg ror pat url 工具类: java api 接收保存图片到本地 标签:工具 sts bre encoder set jpg ror pat url 原文地址:https://www.cnblogs.com/zhangxuetao/p/12985461.html /**
* 保存图片
*/
@CrossOrigin(origins = {"*", "null"})
@RequestMapping(value = "uploadPic", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@ResponseBody
public String uploadPic(MultipartFile photo) {
AppRootBean appRootBean = new AppRootBean();
String resultString = "";
MDC.put("requestType","uploadPic");
logger.info("接收前端data:{}", photo);
String s1="";
try {
if (null!=photo&&!photo.isEmpty())
{
MakeOrderNum makeOrderNum = new MakeOrderNum();
String wx_photo = makeOrderNum.makeOrderNum("wx_photo");
s1 = PicUtil.singleFileUpload(photo,wx_photo);
}
if (StringUtils.isEmpty(s1))
{
resultString = appRootBean.sendRoot(new Object(), ErrorCode.MSG_UPPHONE_CODE_ERROR.getResponseCode(),
ErrorCode.MSG_UPPHONE_CODE_ERROR.getResponseMsg());
}else {
resultString = appRootBean.sendRoot(s1, ErrorCode.MSG_OK.getResponseCode(),
ErrorCode.MSG_OK.getResponseMsg());
}
} catch (IOException e) {
e.printStackTrace();
}
logger.info("上送图片" + resultString);
MDC.clear();
return resultString;
}
package com.weixiu.jingwei.utils;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
/**
* @Author: 张学涛
* @Date: 2019-08-23 10:25
* @Version 1.0
*/
public class PicUtil {
private static String UPLOAD_FOLDER = "/opt/wxPhoto/";
public static String singleFileUpload(MultipartFile pc1,String picName) throws IOException {
// logger.debug("传入的文件参数:{}", JSON.toJSONString(file, true));
if (Objects.isNull(pc1) || pc1.isEmpty()) {
return "";
}
try {
byte[] bytes = pc1.getBytes();
Path path = Paths.get(UPLOAD_FOLDER + "/");
if (!Files.isWritable(path)) {
Files.createDirectories(path);
}
String extension = getFileExtension(pc1); //获取文件后缀
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
String picname = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
String relativeAddr = picname+picName + extension; //唯一的名字接上后缀
Thumbnails.of(pc1.getInputStream()).size(1920, 1200) //写入
.outputQuality(1.0f).toFile(path + "/" + relativeAddr);
// Thumbnails.of(pc1.getInputStream()) //写入
// .outputQuality(1.0f).toFile(path + "/" + relativeAddr);
//logger.debug("文件写入成功...");
return Paths.get(path + "/" + relativeAddr).toString();
} catch (IOException e) {
// e.printStackTrace();
return "null";
}
}
//写一个删除方法,你们可以看情况修改
public static String deFile(String path) {
String resultInfo = null;
Map
下一篇:c++ 枚举型
文章标题:java api 接收保存图片到本地
文章链接:http://soscw.com/index.php/essay/40006.html