如何在项目位置创建的新文件夹中上载文件?

时间:2018-10-25 10:00:35

标签: java java-ee file-upload

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

http://localhost:8080/xxxx.jpg

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

谢谢!

1 个答案:

答案 0 :(得分:0)

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);