import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Random; import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import sun.misc.BASE64Decoder; public class ImageUntil {
public boolean saveBase64File(String username,String imgStr) {
// 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return false;
imgStr = imgStr.replaceAll("data:image/jpeg;base64,", "");
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(imgStr);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
String fileName=username + ".jpg";
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
File f2=new File(System.getProperty("catalina.home")+ File.separator+"webapps"+File.separator+"uploadFile");
boolean exists2 = f2.exists();
if(!exists2){
f2.mkdirs();
}
//String realPath=request.getSession().getServletContext().getRealPath("uploadFile/touxiang")+ File.separator;
String tomcaturl=System.getProperty("catalina.home")+ File.separator+"webapps"+File.separator+"uploadFile"+File.separator+"touxiang"+ File.separator;
File f1=new File(System.getProperty("catalina.home")+ File.separator+"webapps"+File.separator+"uploadFile"+File.separator+"touxiang");
boolean exists = f1.exists();
if(!exists){
f1.mkdirs();
}
File file = new File(tomcaturl+fileName);
OutputStream out = new FileOutputStream(file);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
对字节数组字符串进行Base64解码并生成图片,imgData是从客户端发过来的,图片经过base64位处理的字符串,并将图片存在tomcat服务器根目录下

java存储图片的更多相关文章

  1. vue图片上传及java存储图片(亲测可用)

    1.前言 在使用elementui的upload组件时,我一直无法做到上传的图片和其他数据一起提交.单纯的上传文件,java的存储图片的方式也有局限性. 我知道的后端保存图片有两种方式:一种是直接存储 ...

  2. Oracle中存储图片的类型为BLOB类型,Java中如何将其读取并转为字符串?

    一,读取图片转为String类型: 需要使用Sun公司提供的Base64工具 String str = ((Map) list1.get(0)).get("EINVOICEFILE" ...

  3. android 存储图片到data目录和读取data目录下的图片

    , fos); } ); Bitmap.CompressFormat localCompressFormat = Bitmap.CompressFormat.PNG; bitmap.compress( ...

  4. 使用jdbc存储图片和大文本

    package cn.itcast.i_batch; import java.sql.Connection; import java.sql.PreparedStatement; import jav ...

  5. java多图片上传--前端实现预览--图片压缩 、图片缩放,区域裁剪,水印,旋转,保持比例。

    java多图片上传--前端实现预览 前端代码: https://pan.baidu.com/s/1cqKbmjBSXOhFX4HR1XGkyQ 解压后: java后台: <!--文件上传--&g ...

  6. Redis 存储图片 [base64/url/path]vs[object]

    一.base64图片编解码 基本流程:从网络获取下载一张图片.然后base64编码,再base64解码,存到本地E盘根文件夹下. import java.awt.image.BufferedImage ...

  7. 【hibernate】存储图片

    [hibernate]存储图片 转载: package cn.ycx.study.hibernate.entity; import javax.persistence.Entity; import j ...

  8. Java中图片压缩处理

    原文http://cuisuqiang.iteye.com/blog/2045855 整理文档,搜刮出一个Java做图片压缩的代码,稍微整理精简一下做下分享. 首先,要压缩的图片格式不能说动态图片,你 ...

  9. java获取图片原始尺寸

    java获取图片原始尺寸 URL url = null; InputStream is = null; BufferedImage img = null; try { url = new URL(pi ...

随机推荐

  1. Mysql -Linux系统下安装指南

    博客参考:  https://www.cnblogs.com/pyyu/p/9467289.html 1. Mysql安装 .首先在 RHEL/CentOS 和 Fedora 操作系统中添加 Mari ...

  2. webpack快速入门——配置文件:服务和热更新

    1.在终端安装 cnpm i webpack-dev-server --save-dev 2.配置好后执行 webpack-dev-server,这时候会报错 出现错误,只需要在pagejson里配置 ...

  3. php引用使用不恰当而产生问题的地方

    php变量的引用,如果使用的恰当,会带来效率的提升,相反,效率下降 $array = range(, ); $ref =& $array; var_dump(count($array)); / ...

  4. ubuntu sudo: pip:找不到命令

    编辑文件 /etc/sudoers sudo vi /etc/sudoers 将Defaults env_reset ,改为 Defaults    !env_reset 编辑文件-/.bashers ...

  5. MySQL权限管理(五)

    一.什么是MySQL权限 各大帖子及文章都会讲到数据库的权限按最小权限为原则,这句话本身没有错,但是却是一句空话.因为最小权限,这个东西太抽象,很多时候你并弄不清楚具体他需要哪些权限. 现在很多mys ...

  6. 用Python玩转数据第六周——高级数据处理与可视化

    1.matplotlib中有两个模块,pyplot和pylab import matplotlib.pyplot as plt  ///plt.plot(x,y) import pylab as pl ...

  7. RDLC_部署到不同的浏览器

    首先我用的是vs2015 的reportview插件 在数据库中应该配置报表的服务器地址,在项目中添加ReportViewer 插件,单独用一个页面显示接收报表 <form id="f ...

  8. 【DB2】How to resolve SQL20249N the statement was not processed with error

    相关链接 https://vinaysdb2blog.blogspot.com/2017/11/how-to-resolve-sql20249n-statement-was-not-processed ...

  9. Java之集合(十五)Set综述

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7427554.html 1.前言 原本按照顺序应该是list.queue然后就是set的讲解,但是因为set的实现 ...

  10. 视口(viewport)原理详解之第一部分

    在这篇文章中,我将解释viewports和元素width是如何工作的,比如html元素.  window和 screen. 这篇文章主要针对桌面浏览器,但它的部分和移动设备中的结论也比较类似,所以也是 ...