java 把上传截图到服务器
(1)从JTextArea或java swing的其他组件获取截图
/*** * convert JTextArea to image * @param ta * @param destFile : java.io.File or OutputStream * @param format * @param multiple : 分辨率倍数 */ public static BufferedImage generateImage(JComponent ta, Object destFile, String format, Integer specifiedHeight, Integer specifiedWidth, int multiple) { int height=ta.getHeight(); int width=ta.getWidth(); if(specifiedHeight!=null&&specifiedHeight!=SystemHWUtil.NEGATIVE_ONE){//如果指定了高度 height=specifiedHeight; } if(specifiedWidth!=null&&specifiedWidth!=SystemHWUtil.NEGATIVE_ONE){//如果指定了高度 width=specifiedWidth; } BufferedImage img = new BufferedImage(width * multiple, height * multiple, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = img.createGraphics(); g2d.scale(multiple, multiple); /*g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); Ellipse2D.Double e = new Ellipse2D.Double(300D - DOT_SIZE, 200D - DOT_SIZE, DOT_SIZE + DOT_SIZE, DOT_SIZE + DOT_SIZE); g2d.setColor(Color.YELLOW); g2d.fill(e);*/ ta.printAll(g2d); g2d.dispose(); if(!ValueWidget.isNullOrEmpty(destFile)){ if (destFile instanceof File) {//把截图保存到文件 File file = (File) destFile; try { ImageIO.write(img, format/*"jpg"*/, file); } catch (IOException ex) { ex.printStackTrace(); } } else if (destFile instanceof OutputStream) { OutputStream outputStream = (OutputStream) destFile; try { ImageIO.write(img, format/*"jpg"*/, outputStream); } catch (IOException ex) { ex.printStackTrace(); } } } return img; }
(2)把截图转化为字节数组
实际上是把BufferedImage转化为byte[]
/** * 把BufferedImage 转化为字节数组 * * @param img * @return */ public static ByteArrayOutputStream imageToByteArrayOutputStream(BufferedImage img, String formatName) { //创建储存图片二进制流的输出流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { //创建ImageOutputStream流 ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(baos); //将二进制数据写进ByteArrayOutputStream ImageIO.write(img, formatName, imageOutputStream); } catch (IOException e) { e.printStackTrace(); } return baos; }
(3)使用HttpURLConnection发送http请求
public static void uploadFile(String formatName, byte[] bytes, String uploadedFileName) { String size = FileUtils.formatSize(bytes.length); int resultOption = JOptionPane.showConfirmDialog(null, "Are you sure to Upload " + size + " to Server ?", "确认", JOptionPane.OK_CANCEL_OPTION); if (resultOption != JOptionPane.OK_OPTION) { ToastMessage.toast("取消上传", 2000, Color.red); return; } try { String result = HttpSocketUtil.uploadFile("http://blog.yhskyc.com/convention2/ajax_image/upload", bytes, uploadedFileName + "." + formatName, (Map) null); Map requestMap = null; try { requestMap = (Map) HWJacksonUtils.deSerialize(result, Map.class); StringBuffer stringBuffer = new StringBuffer(""); for (Object obj : requestMap.keySet()) { Object val = requestMap.get(obj); stringBuffer.append(" "); stringBuffer.append("").append(obj).append("").append(":").append(""); } stringBuffer.append(""); // System.out.println(stringBuffer); ToastMessage.toast("上传成功", 2000); CustomDefaultDialog customDefaultDialog = new CustomDefaultDialog(stringBuffer.toString(), "图片路径", true, null, 800); customDefaultDialog.setVisible(true); // ComponentUtil.appendResult(area2,result,true,false); } catch (JsonParseException e1) { e1.printStackTrace(); } catch (JsonMappingException e1) { e1.printStackTrace(); } System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } /*** * 上传BufferedImage 到远程服务器 * @param img */ public static void uploadBufferedImage(BufferedImage img) { String formatName = "jpg"; ByteArrayOutputStream baos = imageToByteArrayOutputStream(img, formatName); //输出数组 byte[] bytes = baos.toByteArray(); uploadFile(formatName, bytes, "Screenshots"); }
"); stringBuffer.append(val).append("