UploadServiceImpl 是上传图片的业务类。比如存到阿里等。 public static void main(String[] args) { UploadServiceImpl uploadService = new UploadServiceImpl(); InputStream input = new ByteArrayInputStream(QRCodeUtil.createImg().toByteArray()); //转换为MultipartFile MultipartFile multipartFile = null; try { multipartFile = new MockMultipartFile("file", "file.png", "text/plain", input); } catch (IOException e) { e.printStackTrace(); } Map<String, Object> map = uploadService.uploadImg("", multipartFile); String url = String.valueOf(map.get("Url")); System.out.println("url=" + url); }
/** * 合成纯色背景图 * 默认白色 255 255 255 * * @return */ public static ByteArrayOutputStream createImg() { BufferedImage bi = new BufferedImage(380, 560, BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); g.setColor(new Color(255, 255, 255, 255)); for (int i = 0; i < bi.getWidth(); i++) { for (int j = 0; j < bi.getHeight(); j++) { g.drawLine(i, j, bi.getWidth(), bi.getHeight()); } } g.dispose(); bi.createGraphics(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ImageIO.write(bi, "png", baos); } catch (IOException e) { e.printStackTrace(); } return baos; }