第一种方式
* 上传文件
* fileUrl 文件url
*
* @param files 资源
* @return Result 返回统一结果model
*/
@ApiOperation(value = "上传文件", notes = "上传文件", response = Result.class)
@PostMapping("/uploadFiles")
public Result uploadFiles(
@ApiParam(name = "files", value = "文件", required = true)
@RequestParam("files") MultipartFile[] files,String lng,String lat,String address,String time) {
List fileDatas = new ArrayList<>();
FileData fileData;
try {
if(files != null && files.length > 0) {
for(MultipartFile file : files) {
// 读取原图片信息
// File srcImgFile = new File(srcImgPath);//得到文件
Image srcImg = ImageIO.read(file.getInputStream());//文件转化为图片
int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
int srcImgHeight = srcImg.getHeight(null);//获取图片的高
// 加水印
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
Color color = Color.white;
g.setColor(color); //根据图片的背景设置水印颜色
Font font = new Font("黑体", Font.PLAIN, 32);
g.setFont(font); //设置字体
g.drawString("经度", 30, 50); // 画文字
g.drawString(lng, 30, 90); // 画文字
g.drawString("纬度", 30, 130); // 画文字
g.drawString(lat, 30, 170); // 画文字
g.drawString(address, 30, 210); // 画文字
g.drawString(time, 30, 250); // 画文字
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
encoder.encode(bufImg);
byte[] resultByte = jpegOutputStream.toByteArray();
/* 上传文件 */
String fileUrl = fastDFSClient.uploadBytes(resultByte,"png");
System.out.println(fileUrl);
fileData = new FileData();
fileData.setFilePath(fileUrl);
fileDatas.add(fileData);
g.dispose();
}
}
} catch (IOException e) {
logger.error("上传文件异常:{}", ExceptionUtils.getStackTrace(e));
return Result.error(ExceptionUtils.getMessage(e));
}
return Result.success(fileDatas);
}
org.apache.maven.plugins
maven-compiler-plugin
1.7
1.7
UTF-8
${java.home}/lib/rt.jar;${java.home}/lib/jce.jar
优化后代码
@ApiOperation(value = "上传文件", notes = "上传文件", response = Result.class)
@PostMapping("/uploadFiles")
public Result uploadFiles(
@ApiParam(name = "files", value = "文件", required = true)
@RequestParam("files") MultipartFile[] files,String lng,String lat,String address) {
List fileDatas = new ArrayList<>();
FileData fileData;
try {
if(files != null && files.length > 0) {
for(MultipartFile file : files) {
// 读取原图片信息
// File srcImgFile = new File(srcImgPath);//得到文件
Image srcImg = ImageIO.read(file.getInputStream());//文件转化为图片
int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
int srcImgHeight = srcImg.getHeight(null);//获取图片的高
// 加水印
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
Color color = Color.white;
g.setColor(color); //根据图片的背景设置水印颜色
Font font = new Font("黑体", Font.PLAIN, 32);
g.setFont(font); //设置字体
g.drawString("经度", 30, 50); // 画文字
g.drawString(lng, 30, 90); // 画文字
g.drawString("纬度", 30, 130); // 画文字
g.drawString(lat, 30, 170); // 画文字
g.drawString(address, 30, 210); // 画文字
g.drawString(DateUtil.getCurrentTime().toString(), 30, 250); // 画文字
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
ImageWriter writer = null;
writer = ImageIO.getImageWritersByFormatName("jpg").next();
writer.setOutput(ImageIO.createImageOutputStream(jpegOutputStream));
ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()) {
// 指定压缩方式为MODE_EXPLICIT
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
// 压缩程度,参数qality是取值0~1范围内
param.setCompressionQuality(0.3f);
}
// 调用write方法,向输入流写图片
writer.write(null, new IIOImage(bufImg, null, null), param);
// ImageIO.write(bufImg, "png", jpegOutputStream);
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
// encoder.encode(bufImg);
byte[] resultByte = jpegOutputStream.toByteArray();
/* 上传文件 */
String fileUrl = fastDFSClient.uploadBytes(resultByte,"png");
System.out.println(fileUrl);
fileData = new FileData();
fileData.setFilePath(fileUrl);
fileDatas.add(fileData);
g.dispose();
}
}
} catch (IOException e) {
logger.error("上传文件异常:{}", ExceptionUtils.getStackTrace(e));
return Result.error(ExceptionUtils.getMessage(e));
}
return Result.success(fileDatas);
}
测试用例
public static boolean addWaterMark(String srcImgPath, String tarImgPath, String waterMarkContent) {
try {
// 读取原图片信息
File srcImgFile = new File(srcImgPath);//得到文件
Image srcImg = ImageIO.read(srcImgFile);//文件转化为图片
int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
int srcImgHeight = srcImg.getHeight(null);//获取图片的高
// 加水印
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
Color color = Color.white;
g.setColor(color); //根据图片的背景设置水印颜色
Font font = new Font("黑体", Font.PLAIN, 32);
g.setFont(font); //设置字体
// 一行14个位置,超出部分进行换行
// int page = (int) Math.ceil((double)waterMarkContent.length()/14);
// for (int i = 0; i < page; i++) {
// String sub = null;
// if ((i+1)*14 < waterMarkContent.length()) {
// sub = waterMarkContent.substring(i*14, (i+1)*14);
// } else {
// sub = waterMarkContent.substring(i*14, waterMarkContent.length());
// }
// }
g.drawString("经度", 30, 50); // 画文字
g.drawString("38.412342134", 30, 90); // 画文字
g.drawString("维度", 30, 130); // 画文字
g.drawString("108.1234214412", 30, 170); // 画文字
g.drawString("陕西省西安市雁塔区宝德云谷", 30, 210); // 画文字
g.drawString("2021-08-24 12:12:12", 30, 250); // 画文字
g.dispose();
// 输出图片
FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
ImageIO.write(bufImg, "png", outImgStream);
outImgStream.flush();
outImgStream.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
System.out.println("添加水印完成");
return true;
}
public static void main(String[] args) {
String srcImgPath="E:/111.png"; //源图片地址
String tarImgPath="e:/114.png"; //待存储的地址
String waterMarkContent="今"; //水印内容
addWaterMark(srcImgPath, tarImgPath, waterMarkContent);
}