为什么我不能在java中这样做?
Object[] o = (Object[])(new int[]{0,1,2,3.14,4});
Run Code Online (Sandbox Code Playgroud)
我有一个方法接收一个对象,然后将其表示为一个字符串,但取决于他的类型(原始,原始包装,数组等).当我创建一个Unit测试时,我将一个数组作为Object传递,这是好的,但当我执行该对象的转换为Object []时,我得到了ClassCastException.这只发生在原始类型数组中.有什么方法可以避免这种行为吗?如果没有,有人可以解释在Java虚拟机上这种行为的原因是什么.
非常感谢任何帮助.
我可以在类.h文件或实现文件.cpp中定义类构造函数的主体.就编译器在特定项目中而言,这两种样式可能是相同的(项目对我来说意味着DLL).同样适用于任何成员函数:它们可以在头文件中定义,也可以在那里声明,然后在cpp文件中定义.
但是,我发现,如果我需要包括在不同的项目,例如类的头文件(一个或多个)(这意味着最终使用该头文件中的代码不同的最终DLL),则具有在头文件中的实际的实现会导致一些头痛在编译时(不是在链接...我甚至没有达到这一点).为什么?好吧,我不会详细说明,但编译器显然试图解决其他头文件等中可能定义的所有函数,迫使可怜的开发人员开始引入各种头文件等.
是不是总是最好保持头文件没有任何实现,只是将它们用于'声明'?这样可以更容易地将它们包含在多个项目中,而不必携带大量额外的垃圾.
你对此有何看法?
在使用Spring MVC提供Web内容的指导下,我正在创建一个Spring Boot Web应用程序,我可以使用嵌入式Tomcat实例以及独立的Tomcat 8服务器运行它.
执行时应用程序按预期工作java -jar adminpage.war
,我在访问时看到了预期的结果http://localhost:8080/table
.但是,当我部署到Tomcat 8服务器(通过adminpage.war
放入webapps
目录)时,我访问时出现404错误https://myserver/adminpage/table
.
在catelina.log
和localhost.log
文件包含在Tomcat服务器上没有什么帮助.
任何人都可以建议我在哪里做了错误的配置?我在过去使用Spring Boot部署RESTful服务时遇到了同样的问题,但这是我第一次涉足Web应用程序.
我的申请文件:
src/main/java/com/.../Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
src/main/java/com/.../MainController.java
@Controller
public class MainController {
@RequestMapping("/table")
public String greeting(Model model) {
model.addAttribute("name", "Fooballs");
return "table";
}
}
Run Code Online (Sandbox Code Playgroud)
src/main/resources/templates/table.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + …
Run Code Online (Sandbox Code Playgroud) 我们有一个实用工具,将在周一至周五之间的任何一天运行.它将更新内容管理工具中的一些文件.与该文件关联的最后修改日期应该是该周的星期一日期.我编写了以下程序来检索当前周的星期一日期.但我仍然不确定这是否适用于所有情况.有没有人有更好的解决方案?
Calendar c = Calendar.getInstance();
c.setTime(new Date());
System.out.println(c.get(Calendar.DAY_OF_MONTH));
System.out.println(c.get(Calendar.DAY_OF_WEEK));
int mondayNo = c.get(Calendar.DAY_OF_MONTH)-c.get(Calendar.DAY_OF_WEEK)+2;
c.set(Calendar.DAY_OF_MONTH,mondayNo);
System.out.println("Date "+c.getTime());
Run Code Online (Sandbox Code Playgroud) 我回顾了很多论坛和例子,但没有人帮助我.我需要从任何Web服务验证签名.我有带有公钥的test.crt文件进行验证.
static bool Verify(string text, string signature)
{
X509Certificate2 cert = new X509Certificate2(
HttpContext.Current.Server.MapPath("test-server.cert"));
RSACryptoServiceProvider csp = (RSACryptoServiceProvider) cert.PublicKey.Key;
// Hash the data
SHA1Managed sha1 = new SHA1Managed();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] data = encoding.GetBytes(text);
byte[] sign = Convert.FromBase64String(signature);
byte[] hash = sha1.ComputeHash(data);
return csp.VerifyHash(hash, CryptoConfig.MapNameToOID("SHA1"), sign);
}
Run Code Online (Sandbox Code Playgroud)
但结果总是假的:(
我有一个OpenSSL示例:
openssl base64 -d -in signature -out signature.bin
openssl dgst -sha1 -verify test-server.pub -signature signature.bin from_gateway
Run Code Online (Sandbox Code Playgroud) c# cryptography digital-signature public-key-encryption x509
我此刻有点困惑.我试过了:
String test = "KP 175.105";
test.replace("KP", "");
System.out.println(test);
Run Code Online (Sandbox Code Playgroud)
得到了:
KP 175.105
Run Code Online (Sandbox Code Playgroud)
但是,我想:
175.105
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?
简单来说,为这样的特定类添加Log4j配置additivity="true"
或additivity="false"
添加Log4j配置的结果是什么?
<Logger name="com.mypage.glass.TryWindow" level="INFO" additivity="true">
<AppenderRef ref="console"/>
<AppenderRef ref="file"/>
</Logger>
Run Code Online (Sandbox Code Playgroud) 我花了两天时间尝试为Paramiko模块安装pyCrypto.
所以,我遇到的第一个问题是:
>>> import paramiko
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python\lib\site-packages\paramiko\__init__.py", line 31
, in <module>
from paramiko.transport import SecurityOptions, Transport
File "C:\Program Files\Python\lib\site-packages\paramiko\transport.py", line 4
7, in <module>
from paramiko.dsskey import DSSKey
File "C:\Program Files\Python\lib\site-packages\paramiko\dsskey.py", line 26,
in <module>
from Crypto.PublicKey import DSA
ImportError: No module named 'Crypto'
Run Code Online (Sandbox Code Playgroud)
实际上它非常有趣,因为我使用Windows并且它不关心大写.我将文件夹名称从crypto更改为Crypto,这个特殊问题消失了.
现在它想要winrandom:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python\lib\site-packages\paramiko\__init__.py", line 31
, in <module>
from paramiko.transport …
Run Code Online (Sandbox Code Playgroud) 这是pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-js-resources-thin</artifactId>
<version>2.2.1.RELEASE</version>
<name>Spring js lib</name>
<description>Spring javascript library without dojo</description>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
这是 assembly.xml
<assembly>
<id>js-jar</id>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources/</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>META-INF/**/*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Run Code Online (Sandbox Code Playgroud)
问题是,每当我打开生成的jar文件(spring-js-resources-thin-2.2.1.RELEASE-js-jar.jar)时,根文件夹总是命名为artifactid-version
(spring-js-resources-thin-2.2) .1.RELEASE),然后是META-INF.
我想知道无论如何我都可以使用文件名构建jar文件artifactid-version.jar
,但是artifactid-version
在类路径中没有,就像maven存储库中的每个jar一样.我认为应该有一个选项或方法来命名<outputDirectory>
.
在Java 8中,方法引用是使用::
运算符完成的.
例如
// Class that provides the functionality via it's static method
public class AddableUtil {
public static int addThemUp(int i1, int i2){
return i1+i2;
}
}
// Test class
public class AddableTest {
// Lambda expression using static method on a separate class
IAddable addableViaMethodReference = AddableUtil::addThemUp;
...
}
Run Code Online (Sandbox Code Playgroud)
你可以看到addableViaMethodReference
现在的行为就像是一个别名AddableUtil::addThemUp
.因此addableViaMethodReference()
将执行相同的操作 AddableUtil.addThemUp()
并返回相同的值.
他们为什么选择引入新的运营商而不是现有运营商?我的意思是,当函数名称结束时执行函数,()
并在没有尾随时返回函数引用()
.
方法执行
AddableUtil.addThemUp();
Run Code Online (Sandbox Code Playgroud)
方法参考
AddableUtil.addThemUp;
Run Code Online (Sandbox Code Playgroud)
这不是更简单直观吗?AFAIK,AddableUtil.addThemUp
当前不(Java 7)用于任何其他目的并抛出编译错误.为什么不利用这个机会而不是创建一个全新的运营商呢?