小编Pad*_*rus的帖子

使用正则表达式从字符串中提取IP地址

在一个应用程序中,我得到包含IP地址的字符串,但这些字符串没有精确的格式.我们所知道的是这些字符串可能包含IP地址.

这是一个字符串可以是什么样子的例子:

  • "XPSPort"
  • "IP_10.29.167.187"
  • "10.29.166.193"

我希望得到一个Java代码,如果有一个字符串,则提取字符串的ip地址,如果字符串不包含ip地址,则返回"".

我试过这段代码,但它不起作用:

String IPADDRESS_PATTERN = 
        "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
        "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
Matcher matcher = pattern.matcher(ipString);
        if (matcher.find()) {
            return matcher.group();
        }
        else{
            return "0.0.0.0";
        }
Run Code Online (Sandbox Code Playgroud)

我很确定使用RegExp是实现这一目标的最佳方式,但我对这些并不是很好,所以有人能帮我找到好的RegExp吗?

提前致谢.

java regex ip

13
推荐指数
3
解决办法
6万
查看次数

如何在Java中获得打印机的品牌和型号?

我实际上正在开发一个将打印机共享到服务器的Java应用程序,我需要这个应用程序来获取它共享的打印机的品牌和型号.

我知道这个问题已被问过三四次,但似乎没有人找到答案.

我试过这段代码:

PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);

        for (PrintService printer : printServices){           
            System.out.println(printer.getDefaultAttributeValue(PrinterMakeAndModel.class));
            System.out.println(printer.getAttribute(PrinterURI.class));
        }
Run Code Online (Sandbox Code Playgroud)

第一个打印始终返回空字符串,第二个打印输出NullPointerException.

一些研究引导我访问此页面:http://bugs.sun.com/bugdatabase/view_bug.do?video_id = 4673400

这似乎是一个知道"错误",我真的不理解评估.

我想解决方法是通过向打印机发送SNMP请求来获取make和model,但我不知道有关SNMP的事情,我不确定是否有一个SNMP命令来获取make和model任何打印机.

如果有人知道如何实现这一点,无论是通过使用Java方法还是通过发送SNMP命令或任何可以在任何操作系统上完成的任何其他操作,您的帮助将不胜感激.

编辑:

以下是指向同一问题的主题的链接:

编辑2:

方案:

正如我在评论中所说,我试图通过将OID"1.3.6.1.2.1.25.3.2.1.3.1"发送到打印机来通过SNMP获取make和model.它似乎工作,但我不确定它是否适用于任何使用相同OID的打印机,如果在目标打印机上停用SNMP,它可能会崩溃.

所以我最终选择使用JNA和Winspool.drv获取驱动程序名称.其中一部分已在JNA中实现,但我必须添加一些结构和功能.

这是JNA中现有WinspoolUtil.java和Winspool.java类的链接.

以下是我对这两个类的个人更新的代码.

温斯普尔:

import java.util.Arrays;
import java.util.List;

import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.INT_PTR;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public class WinspoolUpdate {
    public interface WinspoolLib extends StdCallLibrary { …
Run Code Online (Sandbox Code Playgroud)

java printing snmp driver jna

10
推荐指数
1
解决办法
4187
查看次数

Newtonsoft.Json依赖问题

我的项目使用Newtonsoft.json V4.5.现在我需要将twitter oAuth集成到我的项目中.

我决定使用Tweetsharp.dll进行集成.想知道Newtonsoft.Json的依赖冲突问题,Tweetsharp需要Newtonsoft.json 3.5.

问题是我不能从我的项目中替换我的新版本的Newtonsoft.Json,因为它会影响其他功能.

请帮我解决这个问题.

Server Error in '/' Application.

 Could not load file or assembly 'Newtonsoft.Json, Version=3.5.0.0, Culture=neutral,         PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

  Exception Details: System.IO.FileLoadException: Could not load file …
Run Code Online (Sandbox Code Playgroud)

twitter json.net tweetsharp

5
推荐指数
1
解决办法
903
查看次数

如何让Java的TrayIcon消息淡出?

我有一个Java应用程序,当按下红叉时,它会缩小到系统托盘中.

每当发生这种情况时,我会显示一条消息,通知用户应用程序仍在系统托盘中运行.

这是代码:

...

    @Override
    public void windowClosing(WindowEvent e) {
       try {
            tray.add(trayIcon);
            trayIcon.displayMessage("", "The application has been reduced
                 in the system tray, to stop it, press the \"Quit\" button", 
                 TrayIcon.MessageType.WARNING);
            setVisible(false);
        } catch (AWTException ex) {
            System.out.println("unable to add to tray");
        }
    }
...
Run Code Online (Sandbox Code Playgroud)

目前,如果用户点击该消息,该消息将消失.

我想设置一个计数器,这样即使没有做任何事情,消息也会在几秒后淡出.

有没有人知道一个很好的方法,因为我似乎无法找到任何现有的方法.

编辑

我必须纠正我的问题,事实上,经过一段时间的测试后,它会在7-8秒之后自动消失,但我觉得它有点太长了.此外,它只会在用户执行操作(移动鼠标或在键盘上键入)时消失

所以新的问题是:有没有办法减少淡出时间并通过配置Java应用程序来改变消息行为,还是Windows固有的?

java swing trayicon system-tray

5
推荐指数
1
解决办法
1390
查看次数

使用yii2-widget-depdrop扩展名找不到Yii2 app\controllers\response

我是yii2和php web app开发的新手.我正在使用dep-drop来根据父选择选择输入.现在我在yii2调试器中收到一条错误消息,

exception 'yii\base\ErrorException' with message 'Class 'app\controllers\Response' not found' in G:\xampp\htdocs\project\controllers\SalesOrderController.php:129 Stack trace: #0 [internal function]: yii\base\ErrorHandler->handleFatalError() #1 {main}
Run Code Online (Sandbox Code Playgroud)

php json yii2

3
推荐指数
1
解决办法
6059
查看次数

C#heritage:从child调用的父方法调用child方法

我在这里面临一个问题,让我们假设我有一个父类:

class ParentClass 
{
    public void MethodA()
    {
        //Do stuff
        MethodB();
    }

    public void MethodB()
    {
        //Do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

继承ParentClass并重写MethodB()的子类:

class ChildClass : ParentClass
{
    public new void MethodB()
    {
        //Do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我从ChildClass对象调用MethodA()

public static void Main()
{
    ChildClass childObject = new ChildClass();
    childObject.MethodA();
}
Run Code Online (Sandbox Code Playgroud)

我怎么能确定将调用MethodB()帽子来自子类?

c# inheritance

0
推荐指数
1
解决办法
685
查看次数

标签 统计

java ×3

c# ×1

driver ×1

inheritance ×1

ip ×1

jna ×1

json ×1

json.net ×1

php ×1

printing ×1

regex ×1

snmp ×1

swing ×1

system-tray ×1

trayicon ×1

tweetsharp ×1

twitter ×1

yii2 ×1