首页 > 解决方案 > 如何在项目位置创建的新文件夹中上传文件?

问题描述

我正在使用 API,我需要将 Base 64 转换为图像,并且需要将该图像存储在服务器中的某个位置,以便移动应用程序可以读取路径并从那里显示图像?如何上传文件并返回文件路径,如

http://localhost:8080/xxxx.jpg

注意:我没有使用任何 JSP 或 servlet。

提前致谢 !!

标签: javajakarta-eefile-upload

解决方案


import java.io.ByteArrayInputStream;
import sun.misc.BASE64Decoder;


def sourceData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAADwCAYAAAA+VemSAAAgAEl...==';

// tokenize the data
def parts = sourceData.tokenize(",");
def imageString = parts[1];

// create a buffered image
BufferedImage image = null;
byte[] imageByte;

BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(imageString);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
File f = new File("D:\\Image\\Output.jpg");  //output file path
  ImageIO.write(image, "jpg", f);

推荐阅读