main
黄海 2 years ago
parent 0350401e76
commit 38b3bbee1f

@ -0,0 +1,56 @@
package com.dsideal.FengHuang.Util;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Base64Util {
public static String imageToBase64(BufferedImage bufferedImage) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
try {
ImageIO.write(bufferedImage, "jpg", baos);//写入流中
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = baos.toByteArray();//转换成字节
BASE64Encoder encoder = new BASE64Encoder();
String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
return "data:image/jpg;base64," + png_base64;
}
/**
* FileBASE64
*
* @param file
* @return
*/
public static String fileToBase64(File file) {
return "data:image/png;base64," + Base64.encodeBase64String(fileToByte(file));
}
/**
* Filebyte[]
*
* @param file
* @return
*/
private static byte[] fileToByte(File file) {
byte[] fileBytes = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
fileBytes = new byte[(int) file.length()];
fis.read(fileBytes);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return fileBytes;
}
}

@ -3,10 +3,7 @@ package com.dsideal.FengHuang.Yp.Controller;
import com.alibaba.fastjson.JSONObject;
import com.dsideal.FengHuang.ExcelImportTemplate.StudentImportExcelUtil;
import com.dsideal.FengHuang.Interceptor.*;
import com.dsideal.FengHuang.Util.CommonUtil;
import com.dsideal.FengHuang.Util.ExcelExportUtil;
import com.dsideal.FengHuang.Util.FileUtil;
import com.dsideal.FengHuang.Util.IpUtil;
import com.dsideal.FengHuang.Util.*;
import com.dsideal.FengHuang.Yp.Model.YpModel;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
@ -20,8 +17,15 @@ import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -225,11 +229,15 @@ public class YpController extends Controller {
if (!file.exists()) {
file.mkdirs();// 创建文件夹
}
picFile.getFile().renameTo(new File(PathKit.getWebRootPath() + "/upload/" + sfzh + ".jpg"));
String finalPic = PathKit.getWebRootPath() + "/upload/" + sfzh + ".jpg";
picFile.getFile().renameTo(new File(finalPic));
//输出base64编码的jpg文件
String base64 = Base64Util.fileToBase64(new File(finalPic));
Kv kv = Kv.by("success", true);
kv.set("message", "上传成功!");
kv.set("base64", base64);
renderJson(kv);
//path : /FengHuang/upload/sfzh+".jpg"
}
}
Loading…
Cancel
Save