/**
* 上传图片
*
* @param imgString
* 图片转换的Base64类的字符串
* @return
* @throws Exception
*/
public static String uploadImg(String imgString) throws Exception {
String path = null;
if (BasicTool.isnotBlank(imgString)) {
BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] b = decoder.decodeBuffer(imgString);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 自定义的文件名称
String trueFileName = String.valueOf(System.currentTimeMillis())
+ ".png";
// 设置存放图片文件的路径
File filePath = new File(File.separator + "opt" + File.separator
+ "images" + File.separator + "uploadImg" + File.separator
+ trueFileName);
path = filePath.toString();
System.out.println("存放图片文件的路径:" + path);
Files.write(filePath.toPath(),b );
OutputStream out = new FileOutputStream(path);
out.write(b);
out.flush();
out.close();
}
return path;
}
/**
* 将图片转为Base64字字符串
*
* @param pathString
* 文件存放路径
* @return
* @throws Exception
*/
public static String showImg(String pathString) throws Exception {
String imgString = null;
if (BasicTool.isnotBlank(pathString)) {
BASE64Encoder encoder = new BASE64Encoder();
FileInputStream inputStream = new FileInputStream(new File(
pathString));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] by = new byte[1024];
int len = 0;
while ((len = inputStream.read(by)) != -1) {
output.write(by, 0, len);
}
output.flush();
output.close();
imgString = encoder.encode(output.toByteArray());
}
return imgString;
}
* 上传图片
*
* @param imgString
* 图片转换的Base64类的字符串
* @return
* @throws Exception
*/
public static String uploadImg(String imgString) throws Exception {
String path = null;
if (BasicTool.isnotBlank(imgString)) {
BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] b = decoder.decodeBuffer(imgString);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 自定义的文件名称
String trueFileName = String.valueOf(System.currentTimeMillis())
+ ".png";
// 设置存放图片文件的路径
File filePath = new File(File.separator + "opt" + File.separator
+ "images" + File.separator + "uploadImg" + File.separator
+ trueFileName);
path = filePath.toString();
System.out.println("存放图片文件的路径:" + path);
Files.write(filePath.toPath(),b );
OutputStream out = new FileOutputStream(path);
out.write(b);
out.flush();
out.close();
}
return path;
}
/**
* 将图片转为Base64字字符串
*
* @param pathString
* 文件存放路径
* @return
* @throws Exception
*/
public static String showImg(String pathString) throws Exception {
String imgString = null;
if (BasicTool.isnotBlank(pathString)) {
BASE64Encoder encoder = new BASE64Encoder();
FileInputStream inputStream = new FileInputStream(new File(
pathString));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] by = new byte[1024];
int len = 0;
while ((len = inputStream.read(by)) != -1) {
output.write(by, 0, len);
}
output.flush();
output.close();
imgString = encoder.encode(output.toByteArray());
}
return imgString;
}