• Java把Base64编码格式的图片下载到本地指定文件夹下


    1. import com.alibaba.fastjson.JSONObject;
    2. import org.apache.commons.lang3.StringUtils;
    3. import org.slf4j.Logger;
    4. import org.slf4j.LoggerFactory;
    5. import sun.misc.BASE64Decoder;
    6. import java.io.*;
    7. import java.util.Collections;
    8. import java.util.List;
    9. import java.util.Properties;
    10. public class KafkaConsumer extends ShutdownableThread {
    11. private static final Logger LOG = LoggerFactory.getLogger(KafkaConsumer.class);
    12. public static void main(String[] args) {
    13. // 图片base64字符串
    14. String img = "";
    15. Properties properties = getProperties();
    16. String filePath = properties.getProperty("file.path");
    17. filePath = filePath.replace("\\", "\\\\");
    18. String xm = "张三";
    19. String idCard = "360111";
    20. try {
    21. boolean f = downloadImg(img, filePath, xm, idCard);
    22. if (f) {
    23. LOG.info("XM:" + xm + "图片下载成功");
    24. }
    25. } catch (IOException e) {
    26. LOG.error(e.getMessage(), e);
    27. LOG.info("XM:" + xm + "图片下载失败");
    28. }
    29. }
    30. /**
    31. * base64转图片
    32. *
    33. * @param base64str base64码
    34. * @param savePath 图片路径
    35. * @param xm 人员姓名
    36. * @param idCard 身份证明号码
    37. * @return boolean 判断是否成功下载到本地
    38. */
    39. private static boolean downloadImg(String base64str, String savePath, String xm, String idCard) throws IOException {
    40. //对字节数组字符串进行Base64解码并生成图片
    41. if (base64str == null) {
    42. return false;
    43. }
    44. BASE64Decoder decoder = new BASE64Decoder();
    45. //Base64解码
    46. byte[] b = decoder.decodeBuffer(base64str);
    47. for (int i = 0; i < b.length; ++i) {
    48. //调整异常数据
    49. if (b[i] < 0) {
    50. b[i] += 256;
    51. }
    52. }
    53. // 判断路径是否不存在,不存在就创建文件夹
    54. File fileDir = new File(savePath);
    55. if (!fileDir.exists() && !fileDir.isDirectory()) {
    56. fileDir.mkdirs();
    57. }
    58. // 生成一个空文件,自定义图片的名字
    59. File file = new File(savePath + "\\" + idCard + "_" + xm + ".jpg");
    60. if (!file.exists()) {
    61. file.createNewFile();
    62. }
    63. //生成jpg图片
    64. OutputStream out = new FileOutputStream(file.getPath());
    65. out.write(b);
    66. out.flush();
    67. out.close();
    68. return true;
    69. }
    70. // 注意,这个是根据配置文件拿值,这个配置文件同项目打包后的jar包在同一目录
    71. private static Properties getProperties() {
    72. Properties properties = new Properties();
    73. // 获取项目根目录的绝对路径
    74. String rootPath = System.getProperty("user.dir") + File.separator;
    75. FileInputStream fileInputStream = null;
    76. try {
    77. fileInputStream = new FileInputStream(new File(rootPath + "application.properties"));
    78. properties = new Properties();
    79. properties.load(fileInputStream);
    80. } catch (FileNotFoundException e) {
    81. LOG.error(e.getMessage(), e);
    82. } catch (IOException e) {
    83. LOG.error(e.getMessage(), e);
    84. } finally {
    85. if (fileInputStream != null) {
    86. try {
    87. fileInputStream.close();
    88. } catch (IOException e) {
    89. LOG.error(e.getMessage(), e);
    90. }
    91. }
    92. }
    93. return properties;
    94. }
    95. }

    jar包、配置文件和生成图片的目录        D:\\test

    application.properties

     

     把项目打成jar包

     

    本文参考:java把base64位编码转为File文件并存到本地_ekkcole的博客-CSDN博客_base64转file java

  • 相关阅读:
    Bellman-Ford算法与SPFA算法详解
    如何看待2023年大量劝入C++?
    信息系统项目管理师-进度管理论文提纲
    模版代码:Express 中间件快速入门
    Linux:深入文件系统
    ouster-32激光雷达使用---雷达输出数据分析
    Python 文本语义识别,文本转化为图数据库,文本摘要图展示,文本结构化为图谱
    Java工具类--http请求-post
    MCU复位时GPIO是什么状态?
    ssh登录时间久或登陆后报错
  • 原文地址:https://blog.csdn.net/qq_41816742/article/details/127090488